Java Reference
In-Depth Information
Example 15−6: Soundmap.java (continued)
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.
this.addMouseListener(this);
}
/**
* 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.
**/
public void destroy() { image.flush(); }
/**
* To display the applet, we simply draw the image, and highlight the
* current rectangle if any.
**/
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
if (highlight != null) {
g.setColor(Color.red);
g.drawRect(highlight.x, highlight.y,
highlight.width, highlight.height);
g.drawRect(highlight.x+1, highlight.y+1,
highlight.width-2, highlight.height-2);
}
}
/**
* 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.
**/
public void update(Graphics g) { paint(g); }
/**
* Parse a comma-separated list of rectangle coordinates and a URL.
* Used to read the imagemap rectangle definitions from applet parameters
**/
protected ImagemapRectangle getRectangleParameter(String name) {
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());
Search WWH ::




Custom Search