FileDocCategorySizeDatePackage
Imagemap.javaAPI DocExample1784Sat Jun 02 03:10:14 BST 2001None

Imagemap.java

// This example is from the book _Java in a Nutshell_ by David Flanagan.
// Written by David Flanagan.  Copyright (c) 1996 O'Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or implied.

import java.applet.*;
import java.awt.*;
import java.net.*;
import java.util.*;

public class Imagemap extends Applet {
    protected Image image;      // image to display.
    protected Vector rects;     // list of rectangles in it.
    
    public void init() {
        // load the image to be displayed.
        image = this.getImage(this.getDocumentBase(), 
                      this.getParameter("image"));
        // lookup a list of rectangular areas and the URLs they map to.
        rects = new Vector();
        ImagemapRectangle r;
        int i = 0;
        while((r = getRectangleParameter("rect" + i)) != null) {
            rects.addElement(r);
            i++;
        }
    }
    
    // Called when the applet is being unloaded from the system.
    // We use it here to "flush" the image. This may result in memory 
    // and other resources being freed quicker than they otherwise would.
    public void destroy() { image.flush(); }
    
    // Display the image.
    public void paint(Graphics g) {
        g.drawImage(image, 0, 0, this);
    }
    
    // We override this method so that it doesn't clear the background
    // before calling paint().  Makes for less flickering in some situations.
    public void update(Graphics g) { paint(g); }
    
    // find the rectangle we're inside
    private ImagemapRectangle findrect(int x, int y) {
        int i;
        ImagemapRectangle r = null;
        for(i = 0; i