// Fig. 14.8: ImageMap.java // Demonstrating an image map. import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class ImageMap extends Applet implements MouseListener, MouseMotionListener { Image mapImage; MediaTracker trackImage; int width, height; public void init() { addMouseListener( this ); addMouseMotionListener( this ); trackImage = new MediaTracker( this ); mapImage = getImage( getDocumentBase(), "icons2.gif" ); trackImage.addImage( mapImage, 0 ); try { trackImage.waitForAll(); } catch( InterruptedException e ) { } width = mapImage.getWidth( this ); height = mapImage.getHeight( this ); resize( width, height ); } public void paint( Graphics g ) { g.drawImage( mapImage, 0, 0, this ); } public void mouseMoved( MouseEvent e ) { showStatus( translateLocation( e.getX() ) ); } public void mouseExited( MouseEvent e ) { showStatus( "Pointer outside ImageMap applet" ); } public void mouseDragged( MouseEvent e ) {} public void mousePressed( MouseEvent e ) {} public void mouseReleased( MouseEvent e ) {} public void mouseEntered( MouseEvent e ) {} public void mouseClicked( MouseEvent e ) {} public String translateLocation( int x ) { // determine width of each icon (there are 6) int iconWidth = width / 6; if ( x >= 0 && x <= iconWidth) return "Common Programming Error"; else if ( x > iconWidth && x <= iconWidth * 2 ) return "Good Programming Practice"; else if ( x > iconWidth * 2 && x <= iconWidth * 3 ) return "Performance Tip"; else if ( x > iconWidth * 3 && x <= iconWidth * 4 ) return "Portability Tip"; else if ( x > iconWidth * 4 && x <= iconWidth * 5 ) return "Software Engineering Observation"; else if ( x > iconWidth * 5 && x <= iconWidth * 6 ) return "Testing and Debugging Tip"; return ""; } }