import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; public class graphicalMap extends JFrame implements MouseListener { int xsize=700, ysize=700; //JFrame size values int x,y; //Used to draw small circle String message; //Used to write message when user clicks boolean nearAsda = ((x-50 >0) && (x-50 <40)) && ((y-50 > 0) && (y-50 < 40)); public graphicalMap() { setSize(xsize, ysize); //Set JFrame size setVisible(true); //Set visiility to true show(); //Show addMouseListener(this); //Add mouse listener events } public void mouseClicked(MouseEvent e) { x=e.getX(); //Get x co-ordinate from mouse click y=e.getY(); //Get y co-ordinate from mouse click if( nearAsda ) { //If click is in Asda message = "Asda: The best place to shop"; } else { if( ((x-50 > 0) && (x-50 < 40)) && ((y-100 > 0) && (y-100 < 40)) ) { //If click is in Tesco message = "Tesco: The second best place to shop"; } else { if( ((x-500 > 0) && (x-500 < 40)) && ((y-50 > 0) && (y-50 < 40)) ) { //If click is in Morrisons message = "Morrisons: The third best place to shop"; } else { if( ((x-500 > 0) && (x-500 < 40)) && ((y-100 > 0) && (y-100 < 40)) ) { //If click is in Sainsburys message = "Sainsburys: The fourth best place to shop"; } else { message = ""; } } } } repaint(); } public void mouseExited(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void paint(Graphics g) { super.paint(g); //SHOPS g.setColor(Color.black); g.drawRect(50,50,40,40); //Asda g.drawRect(65,70,10,20); //Asda door g.drawRect(50,100,40,40); //Tesco g.drawRect(65,120,10,20); //Tesco door g.drawRect(500,50,40,40); //Morrisons g.drawRect(515,70,10,20); //Morrisons door g.drawRect(500,100,40,40); //Sainsburys g.drawRect(515,120,10,20); //Sainsburys door //SMALL CIRCLE (MOUSE CLICK) g.drawOval(x,y,3,3); //ROADS g.setColor(Color.blue); g.drawLine(90,63,500,63); //First road (Top) g.drawLine(90,77,150,77); //First road (Bottom before first intersection) g.drawLine(164,77,440,77); //First road (Bottom after first interesction) g.drawLine(454,77,500,77); //First road (Bottom after second intersection) g.drawLine(90,113,150,113); //Second road (Top before first intersection) g.drawLine(164,113,440,113); //Second road (Top after first intersection) g.drawLine(454,113,500,113); //Second road (Top after second intersection) g.drawLine(90,127,150,127); //Second road (Bottom before first intersection) g.drawLine(164,127,440,127); //Second road (Bottom after first intersection) g.drawLine(454,127,500,127); //Second road (Bottom after second intersection) g.drawLine(150,77,150,113); //Third road (Left before intersection) g.drawLine(150,127,150,500); //Third road (Left after intersection) g.drawLine(164,77,164,113); //Third road (Right before intersection) g.drawLine(164,127,164,486); //Third road (Right after intersection) g.drawLine(440,77,440,113); //Fourth road (Left before intersection) g.drawLine(440,127,440,486); //Fourth road (Left after intersection) g.drawLine(454,77,454,113); //Fourth road (Right before intersection) g.drawLine(454,127,454,500); //Fourth road (Right after intersection) g.drawLine(164,486,440,486); //Fifth road (Top) g.drawLine(150,500,454,500); //Fifth road (Bottom) //MESSAGE (MOUSE CLICK ON OBJECT) g.setColor(Color.red); g.drawString(message,40,40); //String drawn by mouse click on object } }