📄 imagemap.java
字号:
import java.applet.Applet;import java.awt.Image;import java.awt.Graphics;import java.awt.Rectangle;import java.util.StringTokenizer;import java.util.Vector;import java.util.Hashtable;import java.net.URL;import java.awt.image.*;import java.net.MalformedURLException;/** * An extensible ImageMap applet class. * The active areas on the image are controlled by ImageArea classes * that can be dynamically loaded over the net. * * @author Jim Graham * @version %I%, %G% */public class ImageMap extends Applet { /** * The unhighlighted image being mapped. */ Image baseImage; /** * The list of image area handling objects; */ ImageMapArea areas[]; /** * The primary highlight mode to be used. */ static final int BRIGHTER = 0; static final int DARKER = 1; int hlmode = BRIGHTER; /** * The percentage of highlight to apply for the primary highlight mode. */ int hlpercent = 50; /** * Get a rectangular region of the baseImage highlighted according to * the primary highlight specification. */ Image getHighlight(int x, int y, int w, int h) { return getHighlight(x, y, w, h, hlmode, hlpercent); } /** * Get a rectangular region of the baseImage with a specific highlight. */ Image getHighlight(int x, int y, int w, int h, int mode, int percent) { return getHighlight(x, y, w, h, new HighlightFilter(mode == BRIGHTER, percent)); } /** * Get a rectangular region of the baseImage modified by an image filter. */ Image getHighlight(int x, int y, int w, int h, ImageFilter filter) { Image cropped = makeImage(baseImage, new CropImageFilter(x, y, w, h)); return makeImage(cropped, filter); } /** * Make the primary highlighted version of the baseImage. */ Image makeImage(Image orig, ImageFilter filter) { return createImage(new FilteredImageSource(orig.getSource(), filter)); } /** * Parse a string representing the desired highlight to be applied. */ void parseHighlight(String s) { if (s == null) { return; } if (s.startsWith("brighter")) { hlmode = BRIGHTER; if (s.length() > "brighter".length()) { hlpercent = Integer.parseInt(s.substring("brighter".length())); } } else if (s.startsWith("darker")) { hlmode = DARKER; if (s.length() > "darker".length()) { hlpercent = Integer.parseInt(s.substring("darker".length())); } } } /** * Initialize the applet. Get attributes. * * Initialize the ImageAreas. * Each ImageArea is a subclass of the class ImageArea, and is * specified with an attribute of the form: * areaN=ImageAreaClassName,arguments... * The ImageAreaClassName is parsed off and a new instance of that * class is created. The initializer for that class is passed a * reference to the applet and the remainder of the attribute * string, from which the class should retrieve any information it * needs about the area it controls and the actions it needs to * take within that area. */ public void init() { String s; parseHighlight(getParameter("highlight")); baseImage = getImage(getDocumentBase(), getParameter("img")); Vector areaVec = new Vector(); int num = 1; while (true) { ImageMapArea newArea; s = getParameter("area"+num); if (s == null) { // Try rect for backwards compatibility. s = getParameter("rect"+num); if (s == null) { break; } String url = getParameter("href"+num); if (url != null) s += "," + url; newArea = new HrefArea(); } else { int classend = s.indexOf(","); try { String name = s.substring(0, classend); newArea = (ImageMapArea) Class.forName(name).newInstance(); } catch (Exception e) { e.printStackTrace(); break; } s = s.substring(classend+1); } newArea.init(this, s); areaVec.addElement(newArea); num++; } areas = new ImageMapArea[areaVec.size()]; areaVec.copyInto(areas); checkSize(); } /** * Check the size of this applet while the image is being loaded. */ synchronized void checkSize() { int w = baseImage.getWidth(this); int h = baseImage.getHeight(this); if (w > 0 && h > 0) { resize(w, h); repaintrect.x = repaintrect.y = 0; repaintrect.width = w; repaintrect.height = h; fullrepaint = true; repaint(); } } private boolean fullrepaint = false; private Rectangle repaintrect = new Rectangle(); private long lastupdate = 0; private final static long UPDATERATE = 100; /** * Handle updates from images being loaded. */ public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { if ((infoflags & (WIDTH | HEIGHT)) != 0) { checkSize(); } if ((infoflags & (SOMEBITS | FRAMEBITS | ALLBITS)) != 0) { repaint(((infoflags & (FRAMEBITS | ALLBITS)) != 0) ? 0 : UPDATERATE, x, y, width, height); } return (infoflags & (ALLBITS | ERROR)) == 0; } /** * Paint the image and all active highlights. */ public void paint(Graphics g) { synchronized(this) { if (fullrepaint) { g = g.create(); g.clipRect(repaintrect.x, repaintrect.y, repaintrect.width, repaintrect.height); fullrepaint = false; } } if (baseImage == null) { return; } g.drawImage(baseImage, 0, 0, this); if (areas != null) { for (int i = areas.length; --i >= 0; ) { if (areas[i].active || areas[i].entered) { areas[i].setState(g, areas[i].entered); } } } } /** * Update the active highlights on the image. */ public void update(Graphics g) { if (fullrepaint) { paint(g); return; } if (baseImage == null) { return; } g.drawImage(baseImage, 0, 0, this); if (areas == null) { return; } // First unhighlight all of the deactivated areas for (int i = areas.length; --i >= 0; ) { if (areas[i].active && !areas[i].entered) { areas[i].setState(g, false); } } // Then highlight all of the activated areas for (int i = areas.length; --i >= 0; ) { if (areas[i].entered) { areas[i].setState(g, true); } } } /** * Make sure that no ImageAreas are highlighted. */ public void mouseExit() { boolean changed = false; for (int i = 0; i < areas.length; i++) { if (areas[i].active) { areas[i].entered = false; changed = true; } } if (changed) { repaint(); } } /** * Find the ImageAreas that the mouse is in. */ public boolean mouseMove(java.awt.Event evt, int x, int y) { boolean changed = false; boolean propagate = true; for (int i = 0; i < areas.length; i++) { if (areas[i].inside(x, y)) { areas[i].entered = propagate; if (areas[i].terminal) { propagate = false; } } else { areas[i].entered = false; } if (areas[i].active != areas[i].entered) { changed = true; } } if (changed) { repaint(); } return true; } int pressX; int pressY; /** * Inform all active ImageAreas of a mouse press. */ public boolean mouseDown(java.awt.Event evt, int x, int y) { pressX = x; pressY = y; for (int i = 0; i < areas.length; i++) { if (areas[i].inside(x, y)) { areas[i].press(x, y); if (areas[i].terminal) { break; } } } return true; } /** * Inform all active ImageAreas of a mouse release. * Only those areas that were inside the original mouseDown() * are informed of the mouseUp. */ public boolean mouseUp(java.awt.Event evt, int x, int y) { for (int i = 0; i < areas.length; i++) { if (areas[i].inside(pressX, pressY)) { areas[i].lift(x, y); if (areas[i].terminal) { break; } } } return true; } /** * Inform all active ImageAreas of a mouse drag. * Only those areas that were inside the original mouseDown() * are informed of the mouseUp. */ public boolean mouseDrag(java.awt.Event evt, int x, int y) { mouseMove(evt, x, y); for (int i = 0; i < areas.length; i++) { if (areas[i].inside(pressX, pressY)) { areas[i].drag(x, y); if (areas[i].terminal) { break; } } } return true; }}/** * The base ImageArea class. * This class performs the basic functions that most ImageArea * classes will need and delegates specific actions to the subclasses. * * @author Jim Graham * @version %I%, %G% */class ImageMapArea implements ImageObserver { /** The applet parent that contains this ImageArea. */ ImageMap parent; /** The X location of the area (if rectangular). */ int X; /** The Y location of the area (if rectangular). */ int Y; /** The size().width of the area (if rectangular). */ int W; /** The size().height of the area (if rectangular). */ int H; /** * This flag indicates whether the user was in this area during the * last scan of mouse locations. */ boolean entered = false; /** This flag indicates whether the area is currently highlighted. */ boolean active = false; /** * This flag indicates whether the area is terminal. Terminal areas * prevent any areas which are under them from being activated when * the mouse is inside them. Some areas may wish to change this to * false so that they can augment other areas that they are on top of. */ boolean terminal = true; /** * This is the default highlight image if no special effects are * needed to draw the highlighted image. It is created by the * default "makeImages()" method. */ Image hlImage; /** * Initialize this ImageArea as called from the applet. * If the subclass does not override this initializer, then it * will perform the basic functions of setting the parent applet * and parsing out 4 numbers from the argument string which specify * a rectangular region for the ImageArea to act on. * The remainder of the argument string is passed to the handleArg() * method for more specific handling by the subclass.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -