/* MapExplorer.java Author: peter snowdon Description: Note the view does not center on the location, although the location is highlighted by a red square. */ import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; public class MapExplorer extends Applet { ImagePanel ip; //new class see below ScrollPane scr; Choice locations; Place[] places = { new Place("Accomodation Office", 449, 117), new Place("Adam Smith Building - D8", 395,218), new Place("Anderson College - C1", 10, 35), new Place("Bower Building - B7", 123, 123), new Place("Boyd Orr Building - D1", 345,124) }; //more places at bottom of file //start of the applet public void start(){ init(); locations = new Choice(); locations.addItemListener(new MyItemListener());//eventlistener added fillLocations(places);//fills the locations with the places above //an image can be retreived by an applet using getImage(location,file); Image i = this.getImage(getCodeBase(), "map.GIF");//could use a parameter //see the new class below ip = new ImagePanel(i); scr= new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS); scr.add(ip); setLayout(new BorderLayout()); add(scr,BorderLayout.CENTER); add(locations,BorderLayout.NORTH); } //contrast this implements method of eventing with the adapter/extend //method of eventing below. class MyItemListener implements ItemListener{ public void itemStateChanged(ItemEvent e){ if(e.getSource() == locations){ scr.setScrollPosition(getLocation(locations.getSelectedIndex()).getLocation()); ip.setMarker(getLocation(locations.getSelectedIndex()).getLocation()); } } } public Place getLocation(int index){ return places[index]; } public void fillLocations(Place[] places){ for (int i =0; i < places.length; i++){ locations.add(places[i].getName()); } } }//eofc class ImagePanel extends Canvas { Image image; private Dimension minimumsize, preferredsize = new Dimension(800,700); public Dimension getPreferredSize(){ return preferredsize; } public Dimension getMinimumSize(){ return minimumsize; } //you must specify this ... public void setPreferredSize(Dimension d){ preferredsize = d; } //and this public void setMinimumSize(Dimension d){ minimumsize = d; } public ImagePanel(Image i){ this.image = i; this.setSize(new Dimension(i.getHeight(this),i.getWidth(this))); setMinimumSize(new Dimension(i.getHeight(this),i.getWidth(this))); this.addMouseListener(new mouse()); repaint(); } //you caould try making the marker a user option //e.g. a TickBox selects marker/no marker Point marker; public void setMarker(Point p){ marker = p; repaint(); } public void paint(Graphics g){ g.drawImage(image,0,0,this); g.setColor(Color.red); if(marker != null) g.fillRect(marker.x,marker.y,20,20); } }//eofc /* Note the use of an event adapter. Adapters are convenience classes that provide abstract implementations of normal listeners. Listeners are interfaces, meaning you have to implement all of the methods. Using an adpter means we can simply extend the behaviour of the methods we need and leave the rest. */ class mouse extends MouseAdapter { public void mouseClicked(MouseEvent e){ System.out.println("x = " + e.getX() + " y = " + e.getY()); } }//eofc /* alternative is longer class mouse implements MouseListener { public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} } */ class Place { String name; Point location; public Place(String name, Point loc){ this.name = name; this.location = loc; } public Place(String name, int x, int y){ this.name = name; this.location = new Point(x,y); } public String getName(){ return this.name; } public Point getLocation(){ return this.location; } }//eofc //extra places for the keen /*new Place("Bute Hall - A13", 415, 200), new Place("Careers Service", 695, 335), new Place("Central Admissions - A16", 350, 190), new Place("Chapel - A8", 500, 195), new Place("College Club - A9", 560, 280), new Place("Computing Science", 955, 370), new Place("Computing Service - A2", 265, 385), new Place("Concert Hall - A12", 445, 285), new Place("Continuing Education - E10", 375, 770), new Place("Davidson Building B1", 610, 115), new Place("Dining Rooms - A9", 555, 280), new Place("East Quadrangle - A5", 340, 245), new Place("Estates and Buildings - B6", 815, 255), new Place("Florentine House - E4", 690, 450), new Place("Gardiner Institute - C6", 1005, 110), new Place("George Service House D7", 790, 300), new Place("Gimorehill Centre - E9", 80, 535), new Place("Graham Kerr Building - B3", 730, 120), new Place("Gregory Building - D2", 970, 300), new Place("Hetherington Building - D10", 870, 500), new Place("Hetherington House - D6", 830, 300), new Place("Hub - E2", 630, 405), new Place("Human Anatomy - A3", 315, 320), new Place("Hunter Halls - A14", 390, 310), new Place("Hunterian Art Gallery - D12", 710, 385), new Place("Hunterian Museum - A15", 380, 245), new Place("John McIntyre Building - A22", 520, 300), new Place("Joseph Black Building - B4", 825, 170), new Place("Kelvin Building - B8", 710, 190), new Place("Library - D11", 730, 350), new Place("Lilybank House - D9", 940, 395), new Place("Macintosh House - D13", 655, 380), new Place("Main Building - A10", 405, 155), new Place("Main Gatehouse - A21", 590, 305), new Place("Mathematics - D4", 880, 310), new Place("Modern Languages - D5", 865, 350), new Place("OTC - B5", 835, 230), new Place("Pearce Lodge - A4", 250, 435), new Place("Portecorvo Building - C3", 900, 10), new Place("Principal's Lodging - A18", 555, 145), new Place("Queen Margaret Union - D3", 920, 350), new Place("Rankine Building - E7", 265, 495), new Place("Reading Room - E1", 565, 375), new Place("Refectory - E3", 565, 425), new Place("Registry - A7", 400, 220), new Place("Robertson Building - C2", 890, 60), new Place("Senate Room - A16", 445, 180), new Place("Southpark House - E5", 675, 575), new Place("The Square - A20", 600, 245), new Place("Stair Building - A19", 630, 200), new Place("Stevenson Building - E6", 315, 620), new Place("Student Recruitment Office - A20", 600, 245), new Place("Tennent Institute - C5", 990, 75), new Place("University Offices - A16", 350, 195), new Place("University Union - E8", 225, 550), new Place("Virology Institute - C4", 920, 40), new Place("Visitor Centre - A11", 445, 315), new Place("James Watt Building - A1", 195, 205), new Place("West Medical Building - B2", 670, 70), new Place("West Quadrangle - A6", 470, 220), new Place("Western Infirmary - B9", 900, 100), new Place("Wolfson Building", 635, 95)*///uncomment for more!