001: package net.xoetrope.samples.travel;
002:
003: import java.awt.Component;
004: import java.awt.Point;
005: import java.awt.event.MouseEvent;
006: import java.awt.event.MouseListener;
007: import java.awt.event.MouseMotionListener;
008:
009: import net.xoetrope.awt.XImage;
010: import net.xoetrope.awt.XLabel;
011: import net.xoetrope.awt.XPanel;
012: import net.xoetrope.xui.XPage;
013: import net.xoetrope.xui.XResourceManager;
014: import net.xoetrope.xui.style.XStyleFactory;
015: import net.xoetrope.awt.XImageMap;
016:
017: /**
018: * A bitmap map viewer with dragging capacility plus hotspot handling.
019: * <p>Copyright: Copyright (c) Xoetrope Ltd., 2001-2003</p>
020: * License: see license.txt
021: * @version 1.0
022: */
023:
024: public class XMapViewer extends XPanel implements MouseListener,
025: MouseMotionListener {
026: long counter;
027: boolean draggingOn = true;
028: boolean drag = false;
029: int buttonX = 0;
030: Point dragPoint;
031: public XImageMap imgMap;
032: XImage imgNavLeft, imgNavRight, imgNavUp, imgNavDown, imgDrag;
033: XLabel lblStatus;
034: XPanel pnlControls;
035: XPanel pnlMap;
036: XStyleFactory compFactory;
037: int pWidth;
038: int pHeight;
039:
040: public XMapViewer() {
041: super ();
042: compFactory = new XStyleFactory(XPage.XUI_AWT_PACKAGE);
043: compFactory.setParentComponent(this );
044: }
045:
046: public void addMouseListener(MouseListener l) {
047: imgMap.addMouseListener(l);
048: }
049:
050: public int hashCode() {
051: return imgMap.hashCode();
052: }
053:
054: public void setMap(String map, int width, int height,
055: String hotspots) {
056: pWidth = getSize().width;
057: pHeight = getSize().height - 30;
058: pnlControls = (XPanel) compFactory.addComponent(XPage.PANEL, 0,
059: pHeight, pWidth, 30, null, "back");
060:
061: pnlMap = new XPanel();
062: pnlMap.setBounds(0, 0, width, height);
063: add(pnlMap);
064:
065: imgMap = new XImageMap();
066: imgMap.setBounds(0, 0, width, height);
067: imgMap.setImage(XResourceManager.getImage(map));
068: pnlMap.add(imgMap);
069:
070: compFactory.setParentComponent(pnlControls);
071: lblStatus = (XLabel) compFactory.addComponent(XPage.LABEL, 40,
072: 4, 150, 26, null, "back");
073: imgDrag = (XImage) compFactory.addComponent(XPage.IMAGE, 2, 2,
074: 25, 25, "dragon.gif");
075:
076: remove(pnlControls);
077: add(pnlControls, 0);
078: buttonX = 27;
079: mapEvents();
080:
081: setupHotspots(hotspots);
082: }
083:
084: public Component addLocation(int x, int y, String text) {
085: compFactory.setParentComponent(pnlMap);
086: XLabel lbl = (XLabel) compFactory.addComponent(XPage.LABEL, x,
087: y, 20, 20, text, XPage.HOTSPOTIMAGE);
088:
089: pnlMap.remove(lbl);
090: pnlMap.add(lbl, 0);
091: return lbl;
092: }
093:
094: public Component addButton(String image) {
095: compFactory.setParentComponent(pnlControls);
096: XImage imgTemp = (XImage) compFactory.addComponent(XPage.IMAGE,
097: buttonX, 2, 25, 25, image);
098: buttonX += 27;
099: lblStatus.setLocation(buttonX, lblStatus.getLocation().y);
100: return imgTemp;
101: }
102:
103: public void centerLocation(Component comp) {
104: Point pt = comp.getLocation();
105: moveMap(-(pt.x - 125), -(pt.y - 150));
106: }
107:
108: public void setupHotspots(String hotspots) {
109: if (hotspots != null) {
110: try {
111: imgMap.read(XResourceManager.getBufferedReader(
112: hotspots, null));
113: } catch (Exception ex) {
114: ex.printStackTrace();
115: }
116: }
117: }
118:
119: public void mapEvents() {
120: imgMap.addMouseListener(this );
121: imgMap.addMouseMotionListener(this );
122: imgDrag.addMouseListener(this );
123: imgDrag.addMouseMotionListener(this );
124: }
125:
126: public void toggleDrag() {
127: draggingOn = !draggingOn;
128: if (draggingOn)
129: imgDrag.setImage(XResourceManager.getImage("dragon.gif"));
130: else
131: imgDrag.setImage(XResourceManager.getImage("dragoff.gif"));
132: }
133:
134: private void startDrag(MouseEvent evt) {
135: if (draggingOn)
136: dragPoint = evt.getPoint();
137: }
138:
139: private void stopDrag(MouseEvent evt) {
140: if (draggingOn) {
141: Point p = evt.getPoint();
142: int x = (int) (p.x - dragPoint.x);
143: int y = (int) (p.y - dragPoint.y);
144: moveMap(pnlMap.getLocation().x + x, pnlMap.getLocation().y
145: + y);
146: }
147: }
148:
149: public void dragMap(MouseEvent evt) {
150: if (evt.getID() == evt.MOUSE_RELEASED) {
151: if (draggingOn) {
152: Point p = evt.getPoint();
153: int x = (int) (p.x - dragPoint.x);
154: int y = (int) (p.y - dragPoint.y);
155: moveMap(x, y);
156: }
157: }
158: }
159:
160: private void moveMap(int x, int y) {
161: if (x > 0)
162: x = 0;
163: else if (pnlMap.getSize().width <= pWidth)
164: x = 0;
165: else if (x < pWidth - pnlMap.getSize().width)
166: x = pWidth - pnlMap.getSize().width;
167:
168: if (y > 0)
169: y = 0;
170: else if (pnlMap.getSize().height <= pHeight)
171: y = 0;
172: else if (y < (pHeight - pnlMap.getSize().height))
173: y = pHeight - pnlMap.getSize().height;
174:
175: pnlMap.setLocation(x, y);
176: }
177:
178: public void setStatus(String text) {
179: lblStatus.setText(text);
180: }
181:
182: public void addPoint(Point pt) {
183: imgMap.addPoint(pt);
184: imgMap.invalidate();
185: }
186:
187: public void addPoint(int x, int y) {
188: imgMap.addPoint(x, y);
189: imgMap.invalidate();
190: }
191:
192: public void mouseClicked(MouseEvent e) {
193: dragMap(e);
194: }
195:
196: public void mouseEntered(MouseEvent e) {
197: }
198:
199: public void mouseExited(MouseEvent e) {
200: }
201:
202: public void mousePressed(MouseEvent e) {
203: startDrag(e);
204: }
205:
206: public void mouseReleased(MouseEvent e) {
207: stopDrag(e);
208:
209: if (imgDrag.contains((int) e.getPoint().x,
210: (int) (e.getPoint().y)))
211: toggleDrag();
212: }
213:
214: public void mouseMoved(MouseEvent e) {
215: Point p = e.getPoint();
216: imgMap.checkHotspot(new Point(p.x, p.y));
217: }
218:
219: public void mouseDragged(MouseEvent e) {
220: Point p = e.getPoint();
221: imgMap.checkHotspot(new Point(p.x, p.y));
222: stopDrag(e);
223: }
224:
225: public String getName(int i) {
226: return imgMap.getName(i);
227: }
228:
229: public int checkHotspot(Point p) {
230: return imgMap.checkHotspot(new Point(p.x, p.y));
231: }
232:
233: }
|