public class ImageMap extends JApplet
1: // Fig. 21.4: ImageMap.java
2: // Demonstrating an image map.
3: import java.awt.event.MouseAdapter;
4: import java.awt.event.MouseEvent;
5: import java.awt.event.MouseMotionAdapter;
6: import java.awt.Graphics;
7: import javax.swing.ImageIcon;
8: import javax.swing.JApplet;
9:
10: public class ImageMap extends JApplet
11: {
12: private ImageIcon mapImage;
13:
14: private static final String captions[] = { "Common Programming Error",
15: "Good Programming Practice", "Graphical User Interface Tip",
16: "Performance Tip", "Portability Tip",
17: "Software Engineering Observation", "Error-Prevention Tip" };
18:
19: // sets up mouse listeners
20: public void init()
21: {
22: addMouseListener(
23:
24: new MouseAdapter() // anonymous inner class
25: {
26: // indicate when mouse pointer exits applet area
27: public void mouseExited( MouseEvent event )
28: {
29: showStatus( "Pointer outside applet" );
30: } // end method mouseExited
31: } // end anonymous inner class
32: ); // end call to addMouseListener
33:
34: addMouseMotionListener(
35:
36: new MouseMotionAdapter() // anonymous inner class
37: {
38: // determine icon over which mouse appears
39: public void mouseMoved( MouseEvent event )
40: {
41: showStatus( translateLocation(
42: event.getX(), event.getY() ) );
43: } // end method mouseMoved
44: } // end anonymous inner class
45: ); // end call to addMouseMotionListener
46:
47: mapImage = new ImageIcon( "icons.png" ); // get image
48: } // end method init
49:
50: // display mapImage
51: public void paint( Graphics g )
52: {
53: super.paint( g );
54: mapImage.paintIcon( this, g, 0, 0 );
55: } // end method paint
56:
57: // return tip caption based on mouse coordinates
58: public String translateLocation( int x, int y )
59: {
60: // if coordinates outside image, return immediately
61: if ( x >= mapImage.getIconWidth() || y >= mapImage.getIconHeight() )
62: return "";
63:
64: // determine icon number (0 - 6)
65: double iconWidth = ( double ) mapImage.getIconWidth() / 7.0;
66: int iconNumber = ( int )( ( double ) x / iconWidth );
67:
68: return captions[ iconNumber ]; // return appropriate icon caption
69: } // end method translateLocation
70: } // end class ImageMap
71:
72: /**************************************************************************
73: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
74: * Pearson Education, Inc. All Rights Reserved. *
75: * *
76: * DISCLAIMER: The authors and publisher of this book have used their *
77: * best efforts in preparing the book. These efforts include the *
78: * development, research, and testing of the theories and programs *
79: * to determine their effectiveness. The authors and publisher make *
80: * no warranty of any kind, expressed or implied, with regard to these *
81: * programs or to the documentation contained in these books. The authors *
82: * and publisher shall not be liable in any event for incidental or *
83: * consequential damages in connection with, or arising out of, the *
84: * furnishing, performance, or use of these programs. *
85: *************************************************************************/