Soundmappublic class Soundmap extends Applet A Java applet that simulates a client-side imagemap.
Plays a sound whenever the user clicks on one of the hyperlinks. |
Fields Summary |
---|
protected Image | image | protected Vector | rects | protected AudioClip | sound |
Methods Summary |
---|
public void | destroy()Called when the applet is being unloaded from the system.
We use it here to "flush" the image we no longer need. This may
result in memory and other resources being freed more quickly. image.flush();
| protected Soundmap$ImagemapRectangle | getRectangleParameter(java.lang.String name)Parse a comma-separated list of rectangle coordinates and a URL.
Used to read the imagemap rectangle definitions from applet parameters
int x, y, w, h;
URL url;
String value = this.getParameter(name);
if (value == null) return null;
try {
StringTokenizer st = new StringTokenizer(value, ",");
x = Integer.parseInt(st.nextToken());
y = Integer.parseInt(st.nextToken());
w = Integer.parseInt(st.nextToken());
h = Integer.parseInt(st.nextToken());
url = new URL(this.getDocumentBase(), st.nextToken());
}
catch (NoSuchElementException e) { return null; }
catch (NumberFormatException e) { return null; }
catch (MalformedURLException e) { return null; }
return new ImagemapRectangle(x, y, w, h, url);
| public void | init()Initialize the applet
// Look up the name of the image, relative to a base URL, and load it.
// Note the use of three Applet methods in this one line.
image = this.getImage(this.getDocumentBase(), this.getParameter("image"));
// Lookup and parse a list of rectangular areas and the URLs they map to.
// The convenience routine getRectangleParameter() is defined below.
rects = new Vector();
ImagemapRectangle r;
for(int i = 0; (r = getRectangleParameter("rect" + i)) != null; i++)
rects.addElement(r);
// Look up a sound to play when the user clicks one of those areas.
sound = this.getAudioClip(this.getDocumentBase(),
this.getParameter("sound"));
// Specify an "event listener" object to respond to mouse button
// presses and releases. Note that this is the Java 1.1 event model.
// Note that it also uses a Java 1.1 inner class, defined below.
this.addMouseListener(new Listener());
| public void | paint(java.awt.Graphics g)To display the applet, we simply draw the image. g.drawImage(image, 0, 0, this);
| public void | update(java.awt.Graphics g)We override this method so that it doesn't clear the background
before calling paint(). No clear is necessary, since paint() overwrites
everything with an image. Causes less flickering this way. paint(g);
|
|