Java Reference
In-Depth Information
Example 15−6: Soundmap.java (continued)
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);
}
/** Called when a mouse button is pressed. */
public void mousePressed(MouseEvent e) {
// On button down, check if we're inside one of the rectangles.
// If so, highlight the rectangle, display a message, and play a sound.
// The utility routine findrect() is defined below.
ImagemapRectangle r = findrect(e);
// If a rectangle is found, and is not already highlighted
if (r != null && r != highlight) {
highlight = r;
// Remember which rectangle it is
showStatus("To: " + r.url);
// display its URL in status line
sound.play();
// play the sound
repaint();
// request a redraw to highlight it
}
}
/** Called when a mouse button is released. */
public void mouseReleased(MouseEvent e) {
// If the user releases the mouse button over a highlighted
// rectangle, tell the browser to display its URL. Also,
// erase the highlight and clear status
if (highlight != null) {
ImagemapRectangle r = findrect(e);
if (r == highlight) getAppletContext().showDocument(r.url);
showStatus("");
// clear the message.
highlight = null;
// forget the highlight
repaint();
// request a redraw
}
}
/** Unused methods of the MouseListener interface */
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
/** Find the rectangle we're inside. */
protected ImagemapRectangle findrect(MouseEvent e) {
int i, x = e.getX(), y = e.getY();
for(i = 0; i < rects.size(); i++) {
ImagemapRectangle r = (ImagemapRectangle) rects.elementAt(i);
if (r.contains(x, y)) return r;
}
return null;
}
/**
* A helper class. Just like java.awt.Rectangle, but with a URL field.
* Note the use of a nested toplevel class for neatness.
**/
static class ImagemapRectangle extends Rectangle {
Search WWH ::




Custom Search