📄 imagemap.java
字号:
*/ public void init(ImageMap parent, String args) { this.parent = parent; StringTokenizer st = new StringTokenizer(args, ", "); X = Integer.parseInt(st.nextToken()); Y = Integer.parseInt(st.nextToken()); W = Integer.parseInt(st.nextToken()); H = Integer.parseInt(st.nextToken()); if (st.hasMoreTokens()) { // hasMoreTokens() Skips the trailing comma handleArg(st.nextToken("")); } else { handleArg(null); } makeImages(); } /** * This method handles the remainder of the argument string after * the standard initializer has parsed off the 4 rectangular * parameters. If the subclass does not override this method, * the remainder will be ignored. */ public void handleArg(String s) { } /** * This method sets the image to be used to render the ImageArea * when it is highlighted. */ public void setHighlight(Image img) { hlImage = img; } /** * This method handles the construction of the various images * used to highlight this particular ImageArea when the user * interacts with it. */ public void makeImages() { setHighlight(parent.getHighlight(X, Y, W, H)); } /** * This method tests to see if a point is inside this ImageArea. * The standard method assumes a rectangular area as parsed by * the standard initializer. If a more complex area is required * then this method will have to be overridden by the subclass. */ public boolean inside(int x, int y) { return (x >= X && x < (X + W) && y >= Y && y < (Y + H)); } /** * This utility method draws a rectangular subset of a highlight * image. */ public void drawImage(Graphics g, Image img, int imgx, int imgy, int x, int y, int w, int h) { Graphics ng = g.create(); ng.clipRect(x, y, w, h); ng.drawImage(img, imgx, imgy, this); } /** * This method handles the updates from drawing the images. */ public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { if (img == hlImage) { return parent.imageUpdate(img, infoflags, x + X, y + Y, width, height); } else { return false; } } /** * This utility method shows a string in the status bar. */ public void showStatus(String msg) { parent.getAppletContext().showStatus(msg); } /** * This utility method tells the browser to visit a URL. */ public void showDocument(URL u) { parent.getAppletContext().showDocument(u); } /** * This method highlights the specified area when the user enters * it with his mouse. The standard highlight method is to replace * the indicated rectangular area of the image with the primary * highlighted image. */ public void highlight(Graphics g, boolean on) { if (on) { g.drawImage(hlImage, X, Y, this); } else { drawImage(g, parent.baseImage, 0, 0, X, Y, W, H); } } /** * This method changes the active state of the ImageArea, which * indicates whether the user is currently "inside" this area. * It turns around and calls the highlight method which is likely * to have been overridden by subclasses seeking a custom highlight. */ public void setState(Graphics g, boolean on) { highlight(g, on); active = on; } /** * The press method is called when the user presses the mouse * button inside the ImageArea. The location is supplied, but * the standard implementation is to call the overloaded method * with no arguments. */ public void press(int x, int y) { press(); } /** * The overloaded press method is called when the user presses the * mouse button inside the ImageArea. This method can be overridden * if the ImageArea does not need to know the location of the press. */ public void press() { } /** * The lift method is called when the user releases the mouse button. * The location is supplied, but the standard implementation is to * call the overloaded method with no arguments. Only those ImageAreas * that were informed of a press will be informed of the corresponding * release. */ public void lift(int x, int y) { lift(); } /** * The overloaded lift method is called when the user releases the * mouse button. This method can be overridden if the ImageArea * does not need to know the location of the release. */ public void lift() { } /** * The drag method is called when the user moves the mouse while * the button is pressed. Only those ImageAreas that were informed * of a press will be informed of the corresponding mouse movements. */ public void drag(int x, int y) { }}/** * The classic "Fetch a URL" ImageArea class. * This class extends the basic ImageArea Class to fetch a URL when * the user clicks in the area. * * @author Jim Graham * @version %I%, %G% */class HrefArea extends ImageMapArea { /** The URL to be fetched when the user clicks on this area. */ URL anchor; /** * The argument string is the URL to be fetched. */ public void handleArg(String arg) { try { anchor = new URL(parent.getDocumentBase(), arg); } catch (MalformedURLException e) { anchor = null; } } /** * The status message area is updated to show the destination URL. * The default graphics highlight feedback is used. */ public void highlight(Graphics g, boolean on) { super.highlight(g, on); showStatus((on && anchor != null) ? "Go To " + anchor.toExternalForm() : null); } /** * The new URL is fetched when the user releases the mouse button * only if they are still in the area. */ public void lift(int x, int y) { if (inside(x, y) && anchor != null) { showDocument(anchor); } // Note that we should not be active, so no repaint is necessary. }}/** * An audio feedback ImageArea class. * This class extends the basic ImageArea Class to play a sound each * time the user enters the area. * * @author Jim Graham * @version %I%, %G% */class SoundArea extends ImageMapArea { /** The URL of the sound to be played. */ String sound; /** * The argument is the URL of the sound to be played. * This method also sets this type of area to be non-terminal. */ public void handleArg(String arg) { sound = arg; terminal = false; } /** * The highlight method plays the sound in addition to the usual * graphical highlight feedback. */ public void highlight(Graphics g, boolean on) { if (on && !active) { parent.play(parent.getDocumentBase(), sound); } super.highlight(g, on); }}/** * An click feedback ImageArea class. * This class extends the basic ImageArea Class to show the locations * of clicks in the image in the status message area. This utility * ImageArea class is useful when setting up ImageMaps. * * @author Jim Graham * @version %I%, %G% */class ClickArea extends ImageMapArea { /** The X location of the last mouse press. */ int startx; /** The Y location of the last mouse press. */ int starty; /** * The argument is ignored, but we use this method to set this type * of area to be non-terminal. */ public void handleArg(String arg) { terminal = false; } /** This class overrides the highlight method to prevent highlighting. */ public void highlight(Graphics g, boolean on) { } String ptstr(int x, int y) { return "("+x+", "+y+")"; } /** * When the user presses the mouse button, start showing coordinate * feedback in the status message line. */ public void press(int x, int y) { showStatus("Clicked at "+ptstr(x, y)); startx = x; starty = y; } /** * Update the coordinate feedback every time the user moves the mouse * while he has the button pressed. */ public void drag(int x, int y) { showStatus("Rectangle from "+ptstr(startx, starty) +" to "+ptstr(x, y) +" is "+(x-startx)+"x"+(y-starty)); } /** * Update the coordinate feedback one last time when the user releases * the mouse button. */ public void lift(int x, int y) { drag(x, y); }}/** * A message feedback ImageArea class. * This class extends the basic ImageArea Class to show the a given * message in the status message area when the user enters this area. * * @author Jim Graham * @version %I%, %G% */class NameArea extends ImageMapArea { /** The string to be shown in the status message area. */ String name; /** * The argument is the string to be displayed in the status message * area. This method also sets this type of area to be non-terminal. */ public void handleArg(String arg) { name = arg; terminal = false; } /** * The highlight method displays the message in addition to the usual * graphical highlight feedback. */ public void highlight(Graphics g, boolean on) { super.highlight(g, on); showStatus(on ? name : null); }}/** * An improved "Fetch a URL" ImageArea class. * This class extends the basic ImageArea Class to fetch a URL when * the user clicks in the area. In addition, special custom highlights * are used to make the area look and feel like a 3-D button. * * @author Jim Graham * @version %I%, %G% */class HrefButtonArea extends ImageMapArea { /** The URL to be fetched when the user clicks on this area. */ URL anchor; /** The highlight image for when the button is "UP". */ Image upImage; /** The highlight image for when the button is "DOWN". */ Image downImage; /** This flag indicates if the "button" is currently pressed. */ boolean pressed = false; /** The border size for the 3-D effect. */ int border = 5; /** * The argument string is the URL to be fetched. * This method also constructs the various highlight images needed * to achieve the 3-D effect. */ public void handleArg(String arg) { try { anchor = new URL(parent.getDocumentBase(), arg); } catch (MalformedURLException e) { anchor = null; } if (border * 2 > W || border * 2 > H) { border = Math.min(W, H) / 2; } } public void makeImages() { upImage = parent.getHighlight(X, Y, W, H, new ButtonFilter(false, parent.hlpercent, border, W, H)); downImage = parent.getHighlight(X, Y, W, H, new ButtonFilter(true, parent.hlpercent, border, W, H)); } public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { if (img == (pressed ? downImage : upImage)) { return parent.imageUpdate(img, infoflags, x + X, y + Y, width, height); } else { return (img == downImage || img == upImage); } } /** * The status message area is updated to show the destination URL.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -