import java.awt.*;
import java.applet.*;
import java.util.*;
import java.net.*;
public class ImageMap extends Applet {
Image background;
boolean overlay;
Vector areas = new Vector();
Vector images = new Vector();
Vector urls = new Vector();
int current = -1;
public void init() {
System.out.println ("ImageMap applet copyright Prominence Dot Com 1996");
// get the parameters
background = getImage(getDocumentBase(), getParameter("main"));
prepareImage(background, this);
overlay = Boolean.valueOf(getParameter("overlay")).booleanValue();
String bgcolor = getParameter("bgcolor");
if (bgcolor != null)
setBackground (new Color (Integer.parseInt (bgcolor.substring (1), 16)));
boolean ok = true;
for (int i = 1; ok; ++i) {
String areaStr = getParameter("area"+i);
String imageStr = getParameter("image"+i);
String urlStr = getParameter("url"+i);
if ((areaStr != null) && (imageStr != null) && (urlStr != null)) {
try {
StringTokenizer str = new StringTokenizer(areaStr);
Rectangle area = new Rectangle(Integer.parseInt(str.nextToken()),
Integer.parseInt(str.nextToken()),
Integer.parseInt(str.nextToken()),
Integer.parseInt(str.nextToken()));
Image image = getImage(getDocumentBase(), imageStr);
URL url = new URL(getDocumentBase(), urlStr);
areas.addElement(area);
images.addElement(image);
urls.addElement(url);
prepareImage (image, this);
} catch (MalformedURLException ex) {
System.out.println ("Invalid format for URL " + i + " : " + urlStr);
} catch (NoSuchElementException ex) {
System.out.println ("Invalid format for area " + i + " : " + areaStr);
} // end try
} else {
ok = false;
} // end if
} // end for
} // end init
public boolean handleEvent(Event e) {
switch(e.id) {
case Event.MOUSE_ENTER:
case Event.MOUSE_MOVE:
case Event.MOUSE_EXIT:
case Event.MOUSE_DOWN:
case Event.MOUSE_DRAG:
case Event.MOUSE_UP:
int next = -1;
if (e.id != Event.MOUSE_EXIT) {
for (int i = 0; i < areas.size (); ++i) {
Rectangle rect = (Rectangle) areas.elementAt (i);
if (rect.inside (e.x, e.y)) {
next = i;
} // end if
} // end for
} // end if
if (next != current) {
int temp = current;
current = next;
repaintImages (temp, current);
} // end if
if ((e.id == Event.MOUSE_DOWN) && (current != -1)) {
URL url = (URL) urls.elementAt (current);
getAppletContext().showDocument(url);
}
break;
} // end switch
return(super.handleEvent(e));
} // end handleEvent
protected void repaintImages(int last, int current) {
if (overlay) {
if (last != -1) {
Rectangle oldRect = (Rectangle) areas.elementAt (last);
Image oldImage = (Image) images.elementAt (last);
repaint (oldRect.x, oldRect.y, oldImage.getWidth (this), oldImage.getHeight (this));
}
if (current != -1) {
Rectangle newRect = (Rectangle) areas.elementAt (current);
Image newImage = (Image) images.elementAt (current);
repaint (newRect.x, newRect.y, newImage.getWidth (this), newImage.getHeight (this));
}
} else {
repaint (0, background.getHeight (this), size ().width, size ().height -
background.getHeight (this));
} // end if
} // end repaintImages
public void update (Graphics g) {
int x = background.getWidth (this), y = background.getHeight (this);
if ((x > 0) && (y > 0)) {
g.setColor (getBackground ());
g.fillRect (x, 0, size ().width - x, y);
g.fillRect (0, y, size ().width, size ().height - y);
paint (g);
} else {
super.update (g);
}
}
public void paint(Graphics g) {
g.drawImage (background, 0, 0, this);
if (current != -1) {
Image image = (Image) images.elementAt (current);
if (overlay) {
Rectangle rect = (Rectangle) areas.elementAt (current);
g.drawImage (image, rect.x, rect.y, this);
} else {
int w = image.getWidth (this), h = image.getHeight (this);
g.drawImage (image, (size ().width - w) / 2, (size ().height +
background.getHeight (this) - h) / 2, this);
} // end if
} // end if
} // end paint
} // end ImageMap