001: package net.xoetrope.swing;
002:
003: import java.io.Reader;
004: import java.util.Enumeration;
005: import java.util.StringTokenizer;
006: import java.util.Vector;
007:
008: import java.awt.Cursor;
009: import java.awt.Point;
010: import java.awt.Polygon;
011: import java.awt.event.MouseEvent;
012: import java.awt.event.MouseMotionListener;
013:
014: import net.xoetrope.xml.XmlElement;
015: import net.xoetrope.xml.XmlSource;
016: import net.xoetrope.xui.XResourceManager;
017:
018: /**
019: * <p>A widget for displaying an image and associating hotspots at
020: * coordinates specified in an external file</p>
021: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003<br>
022: * License: see license.txt
023: * @version 1.0
024: */
025: public class XHotspotImage extends XImage implements
026: MouseMotionListener {
027: protected static Cursor handCursor, defaultCursor;
028: protected Vector hotspots;
029: protected Vector names;
030:
031: /**
032: * Constructor. Sets cursors and creates the Vector which stores the hotspots.
033: * Adds a new MouseMotionListener to the image
034: */
035: public XHotspotImage() {
036: if (handCursor == null) {
037: handCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
038: defaultCursor = Cursor
039: .getPredefinedCursor(Cursor.DEFAULT_CURSOR);
040: }
041:
042: hotspots = new Vector();
043: addMouseMotionListener(this );
044: }
045:
046: /**
047: * Get the name of the hotspot in element i of the names vector
048: * @param i The index of the hotspot
049: * @return The Name of the hotspot
050: */
051: public String getName(int i) {
052: if (i != -1)
053: return (String) names.elementAt(i);
054: return null;
055: }
056:
057: /**
058: * Reads the hotspot information from the reader parameter and adds them to
059: * the names and hotspots vectors.
060: * @param r Reader of the file.
061: */
062: public void read(Reader r) {
063: names = new Vector();
064: hotspots = new Vector();
065: XmlElement element = XmlSource.read(r);
066: Enumeration e = element.getChildren().elements();
067: while (e.hasMoreElements()) {
068: XmlElement ele = (XmlElement) e.nextElement();
069: Object name = ele.getAttribute("name");
070: if (name == null) {
071: names.addElement("");
072: } else {
073: names.addElement(ele.getAttribute("name").toString());
074: }
075: addHotspot(ele.getAttribute("coords").toString());
076: }
077: }
078:
079: private int getNextTokenAsInt(StringTokenizer tokenizer) {
080: String s = tokenizer.nextToken().trim();
081: return Integer.parseInt(s);
082: }
083:
084: /**
085: * Creates a polygon and iterates the coordinates in the pts paramater adding
086: * each to the polygon. Adds the polygon to the hotspots Vector
087: * @param pts String containing the points pairs of the polygon.
088: */
089: private void addHotspot(String pts) {
090: Polygon p = new Polygon();
091: StringTokenizer tokenizer = new StringTokenizer(pts, ",");
092: int x0 = getNextTokenAsInt(tokenizer);
093: int y0 = getNextTokenAsInt(tokenizer);
094: p.addPoint(x0, y0);
095:
096: int x, y;
097: while (tokenizer.hasMoreTokens()) {
098: x = getNextTokenAsInt(tokenizer);
099: y = getNextTokenAsInt(tokenizer);
100: p.addPoint(x, y);
101: }
102:
103: // Close the polygon
104: p.addPoint(x0, y0);
105: hotspots.addElement(p);
106: }
107:
108: /**
109: * Check the Point p to see if any of the polygons in the hotspots Vector contain
110: * it. If so return the index of the Vector element.
111: * @param p The Point which we are checking
112: * @return The index of the polygon which contains the point or -1 if none of
113: * the polygons contain the Point
114: */
115: public int checkHotspot(Point p) {
116: int numPolygons = hotspots.size();
117: for (int i = 0; i < numPolygons; i++) {
118: Polygon poly = (Polygon) hotspots.elementAt(i);
119:
120: if (poly.contains(p)) {
121: setCursor(handCursor);
122: return i;
123: }
124: }
125: setCursor(defaultCursor);
126: return -1;
127: }
128:
129: /**
130: * Call the checkHotSpot function to change the cursor
131: * @param e MouseEvent
132: */
133: public void mouseMoved(MouseEvent e) {
134: checkHotspot(e.getPoint());
135: }
136:
137: /**
138: * Unused method.
139: * @param e
140: */
141: public void mouseDragged(MouseEvent e) {
142: }
143: }
|