// ImageMap.java (V7.0) // // Written by Chris Carter, 2nd October 1998 // Changes an image when the mouse enters or exits its area. Uses a different, simpler method to // change the image. Overrides the 'update' method to eliminate flicker. Also uses a media tracker // to check when images are loaded, and will go to a URL when clicked! // JDK 1.1.6 import java.awt.*; // Graphics import java.util.*; // General stuff import java.net.*; // Networking import java.awt.event.*; // Event handlers import java.lang.*; // Parses Strings to other things public class ImageMap extends java.applet.Applet implements MouseListener { Image untouchedImage, touchedImage, drawnImage; // Declare instances of Image MediaTracker tracker; // MediaTracker monitors the load-progress of the images String imageBaseLocation, defaultImage, alternateImage, destinationURL, showURL, changeCursor, debug; boolean ENABLE_DEBUG, ENABLE_SHOW_URL, ENABLE_CHANGE_CURSOR; boolean ENTERED = false; // ENTERED is false unless the mouse is over the applet area URL loadThisURL; // URL to load when the applet is clicked URL defaultImageURL, alternateImageURL; // ---------------------------------------------------------------------------------------- public void init() { // Get the applet parameters that the user has defined imageBaseLocation = getParameter("imageBaseLocation"); defaultImage = getParameter("defaultImage"); alternateImage = getParameter("alternateImage"); defaultImageURL = makeURL(defaultImage); alternateImageURL = makeURL(alternateImage); destinationURL = getParameter("destinationURL"); loadThisURL = makeURL(destinationURL); showURL = getParameter("showURL"); ENABLE_SHOW_URL = (Boolean.valueOf(showURL)).booleanValue(); changeCursor = getParameter("changeCursor"); ENABLE_CHANGE_CURSOR = (Boolean.valueOf(changeCursor)).booleanValue(); debug = getParameter("debug"); ENABLE_DEBUG = (Boolean.valueOf(debug)).booleanValue(); tracker = new MediaTracker(this); // Add a MediaTracker to this applet untouchedImage = getImage(defaultImageURL); // Get the 'default' imagemap image touchedImage = getImage(alternateImageURL); // Get the 'moused' imagemap image tracker.addImage(untouchedImage, 0); // Add the default image to the tracker, highest priority tracker.addImage(touchedImage, 1); // Add the 'moused' image to the tracker, lower priority try { tracker.waitForAll(); // Try downloading the images & wait until they are all in } catch (InterruptedException e) {} // while (!tracker.checkAll()) { // showStatus("Loading images..."); // } drawnImage = untouchedImage; // The first image to draw is the 'default' - untouchedImage this.addMouseListener(this); // Add a MouseListener to this applet // Allows the cursor to change to the 'hand' style when over the applet if (ENABLE_CHANGE_CURSOR) { this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); } if (ENABLE_DEBUG) { System.out.println("Init: "); System.out.println("getCodeBase: " + getCodeBase()); } } public void mouseClicked(MouseEvent e) { getAppletContext().showDocument(this.loadThisURL); } public void mouseEntered(MouseEvent e) { if (!ENTERED) { ENTERED = true; drawnImage = touchedImage; if (ENABLE_SHOW_URL) { showStatus(destinationURL); } repaint(); } if (ENABLE_DEBUG) { System.out.println("Entered applet area!"); } } public void mouseExited(MouseEvent e) { if (ENTERED) { ENTERED = false; drawnImage = untouchedImage; showStatus(""); repaint(); } if (ENABLE_DEBUG) { System.out.println("Exited applet area!"); } } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void update(Graphics g) { if (tracker.checkAll() == false) { showStatus("Loading images..."); } g.drawImage(drawnImage, 0, 0, this); } public void paint(Graphics g) { update(g); } // Called when the applet is destroyed. Ensures all AWT components are removed. public void destroy() { if (ENABLE_DEBUG) { System.out.println("Applet ImageMap destroyed."); } this.removeAll(); } // Implementing this method is recommended, it provides info about the applet. public String getAppletInfo() { return "ImageMap applet by Chris Carter (chris.carter@iee.org)"; } public URL makeURL(String URL_string) { // Turns a String description of a URL into a proper URL object URL URLobject; URLobject = null; try { URLobject = new URL(URL_string); } catch (MalformedURLException e) { System.out.println("The URL " + URL_string + " is malformed!"); } return URLobject; } }