import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

        // Illustrate the principles of image maps by drawing
        // an image and monitoring mouse movements over it.
        // The mouse's position is displayed in the applet's
        // status line within the browser.
        // Clicking in the central portion of the image causes a
        // transfer to the URL indicated by the `destination' parameter.
public class ImageMapApplet extends Applet {
    public void init() throws RuntimeException {
        // Get the image name.
        String imageName = checkParameter(parameterInfo[imageParameter][0]);
        setImage(extractImage(imageName));

        // Destination parameter is optional as there is a default.
        String whereTo = getParameter(parameterInfo[destinationParameter][0]);
        if(whereTo != null){
            setDestination(whereTo);
        }
    }

    public void start(){
        // Setup the mouse listener objects.
        setupMapper();
    }

        // Scale the image to the full size of the applet.
    public void paint(Graphics g){
        final Dimension size = getSize();
        Image image = getImage();
        if(image != null){
            g.drawImage(image,0,0,size.width,size.height,this);
        }
    }

        // Supply information on the parameters used.
    public String[][] getParameterInfo(){
        return parameterInfo;
    }
        
        // Retrieve fileName from this applet's archive file.
    protected Image extractImage(String fileName) throws RuntimeException {
        try {
            // Identify the resource via the applet's ClassLoader.
            InputStream imageStream = getClass().getResourceAsStream(fileName);
            // Read the whole Image as a single byte array.
            byte imageBytes[] = new byte[imageStream.available()];
            imageStream.read(imageBytes);
            // Turn the data into an Image.
            Toolkit tk = Toolkit.getDefaultToolkit();
            return tk.createImage(imageBytes);
        }
        catch(Exception e){
            throw new RuntimeException(e.toString());
        }
    }

    protected void setupMapper(){
        // Monitor mouse movements and display the (x,y) position
        // in the status line.
        addMouseMotionListener(new MouseMotionAdapter(){
            public void mouseMoved(MouseEvent e){
                if(inTransferArea(e)){
                    showStatus("Goto: "+getDestination());
                }
                else{
                    showStatus("");
                }
            }
        });

        // Detect button presses and transfer to a new URL if
        // the button is pressed in the central area of the image.
        addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent e){
                // Decide whether to transfer to the destination.
                if(inTransferArea(e)){
                    try{
                        showStatus("Transferring.");
                        AppletContext context = getAppletContext();
                        context.showDocument(new URL(getDestination()));
                    }
                    catch(Exception except){
                        showStatus("Transfer failed.");
                    }
                }
                else{
                    showStatus("Outside the area.");
                }
            }
        });
    }

        // Return true if the mouse is currently in the transfer area.
    protected boolean inTransferArea(MouseEvent e){
        Dimension size = getSize();
        final int xBorder = size.width/4, yBorder = size.height/4;
        final int x = e.getX(), y = e.getY();
        if((x < xBorder) || (x > (size.width-xBorder))){
            return false;
        }
        else if((y < yBorder) || (y > (size.height-yBorder))){
            return false;
        }
        else{
            return true;
        }
    }

        // Retrieve the given parameter from the applet's properties.
    protected String checkParameter(String parameter) throws RuntimeException {
        String value = getParameter(parameter);
        if(value == null){
            throw new RuntimeException("Parameter: "+parameter+" is missing.");
        }
        return value;
    }

    protected Image getImage(){
        return image;
    }

    protected void setImage(Image i){
        image = i;
    }

    protected String getDestination(){
        return destination;
    }

    protected void setDestination(String d){
        destination = d;
    }

    // The image to be displayed in the full size of the applet.
    private Image image;
    // Where to transfer to.
    private String destination = "http://www.javasoft.com/";
    // Provide information on the parameters expected by this applet.
    private final int imageParameter = 0, destinationParameter = 1;
    private final String[][] parameterInfo = {
        {"image", "image file",
         "Name of the image to be displayed, relative to the document base."
        },
        { "destination", "URL", "Where to transfer to.",
        },
    };
}
