Ticket #6955: MapObject.java

File MapObject.java, 6.7 KB (added by jhuntley, 15 years ago)
Line 
1/**
2 *
3 */
4package org.openstreetmap.gui.jmapviewer.scene.entities;
5
6import java.awt.Graphics;
7import java.awt.Graphics2D;
8import java.awt.Point;
9import java.awt.RenderingHints;
10import java.awt.geom.AffineTransform;
11import java.util.LinkedList;
12import java.util.List;
13
14import org.openstreetmap.gui.jmapviewer.Coordinate;
15import org.openstreetmap.gui.jmapviewer.OsmMercator;
16import org.openstreetmap.gui.jmapviewer.interfaces.MapViewInterface;
17
18/**
19 * Abstract Map Object for defining objects in OSM Space.
20 *
21 * @author Jason Huntley
22 */
23public abstract class MapObject {
24
25 /** The child object list. */
26 private final List<MapObject> childObjectList;
27
28 /** The parent. */
29 private MapObject parent = null;
30
31 /** The local coordinate. */
32 private Point localCoordinate=new Point();
33
34 /** The world coordinate. */
35 private Coordinate worldCoordinate=new Coordinate(0,0);
36
37 /** The scale. */
38 private final double scale = 1.0;
39
40 /** The rotate. */
41 private double rotate = 0;
42
43 /** The at. */
44 private AffineTransform at = new AffineTransform();
45
46 /**
47 * Instantiates a new map object.
48 */
49 public MapObject() {
50 childObjectList = new LinkedList<MapObject>();
51 }
52
53 /**
54 * Gets the at.
55 *
56 * @return the at
57 */
58 public AffineTransform getAt() {
59 return at;
60 }
61
62 /**
63 * Sets the at.
64 *
65 * @param at the at to set
66 */
67 public void setAt(AffineTransform at) {
68 this.at = at;
69 }
70
71 /**
72 * Adds the map object.
73 *
74 * @param obj the obj
75 */
76 public void addMapObject(MapObject obj) {
77 obj.setParent(this);
78 childObjectList.add(obj);
79 }
80
81 /**
82 * Removes the map object.
83 *
84 * @param obj the obj
85 */
86 public void removeMapObject(MapObject obj) {
87 childObjectList.remove(obj);
88 }
89
90 /**
91 * Gets the parent.
92 *
93 * @return the parent
94 */
95 public MapObject getParent() {
96 return parent;
97 }
98
99 /**
100 * Sets the parent.
101 *
102 * @param parent the parent to set
103 */
104 public void setParent(MapObject parent) {
105 this.parent = parent;
106 }
107
108 /**
109 * Initialize state of object.
110 *
111 * @param view the view
112 */
113 public abstract void initialize(MapViewInterface view);
114
115 /**
116 * Gets the local scale.
117 *
118 * @return the local scale
119 */
120 public double getLocalScale() {
121 return scale;
122 }
123
124 /**
125 * Gets the local coordinates.
126 *
127 * @return the local coordinates
128 */
129 public Point getLocalCoordinates() {
130 return localCoordinate;
131 }
132
133 /**
134 * Sets the local screen coordinates.
135 *
136 * @param pt the new local coordinates
137 */
138 public void setLocalCoordinates(Point pt) {
139 if (pt==null)
140 localCoordinate=new Point(0,0);
141 else
142 localCoordinate=new Point(pt);
143 }
144
145 /**
146 * Sets the local screen coordinates.
147 *
148 * @param x the x
149 * @param y the y
150 */
151 public void setLocalCoordinates(int x, int y) {
152 this.setLocalCoordinates(new Point(x,y));
153 }
154
155 /**
156 * Gets the world coordinate.
157 *
158 * @return the worldCoordinate
159 */
160 public Coordinate getWorldCoordinate() {
161 return worldCoordinate;
162 }
163
164 /**
165 * Sets the world coordinate.
166 *
167 * @param worldCoordinate the worldCoordinate to set
168 */
169 public void setWorldCoordinate(Coordinate worldCoordinate) {
170 this.worldCoordinate = worldCoordinate;
171 }
172
173 /**
174 * Sets the world coordinate.
175 *
176 * @param lat the lat
177 * @param lon the lon
178 */
179 public void setWorldCoordinate(double lat, double lon) {
180 this.worldCoordinate = new Coordinate(lat,lon);
181 }
182
183 /**
184 * Gets the rotate.
185 *
186 * @return the rotate in radians
187 */
188 public double getRotate() {
189 return rotate;
190 }
191
192 /**
193 * Sets the rotate.
194 *
195 * @param radians the new rotate
196 */
197 public void setRotate(double radians) {
198 this.rotate = radians;
199 }
200
201 /**
202 * Sets the rotate degrees.
203 *
204 * @param degrees the new rotate degrees
205 */
206 public void setRotateDegrees(double degrees) {
207 this.rotate = OsmMercator.degToRad(degrees);
208 }
209
210 /**
211 * Update local. If window resizes or zoom occurs, the local
212 * coordinates will change on base level nodes.
213 *
214 * @param view the view
215 */
216 public void updateLocal(MapViewInterface view) {
217 if (parent==null) {
218 Point pt=view.getMapPosition(getWorldCoordinate(),false);
219 this.setLocalCoordinates(pt);
220 }
221 }
222
223 /**
224 * Update world. After local transformations occur,
225 * the global coordinates need to be update to reflect
226 * new positions.
227 *
228 * @param view the view
229 * @param nextAt the next at
230 */
231 public void updateWorld(MapViewInterface view, AffineTransform nextAt) {
232 Point next=new Point((int)nextAt.getTranslateX(),(int)nextAt.getTranslateY());
233 setWorldCoordinate(view.getPosition(next));
234 }
235
236 /**
237 * Update.
238 *
239 * @param view the view
240 */
241 public void update(MapViewInterface view) {
242 updateLocal(view);
243
244 if (parent!=null) {
245 getAt().setToIdentity();
246 getAt().setTransform(parent.getAt());
247
248 at.translate(getLocalCoordinates().x, getLocalCoordinates().y);
249
250 at.scale(getLocalScale(), getLocalScale());
251
252 at.rotate(getRotate());
253 } else {
254 double actualScale=getLocalScale()/view.getMeterPerPixel();
255
256 at.setToIdentity();
257
258 at.translate(getLocalCoordinates().x, getLocalCoordinates().y);
259
260 at.scale(actualScale, actualScale);
261
262 at.rotate(getRotate());
263 }
264
265 updateWorld(view, at);
266
267 for (MapObject obj : childObjectList) {
268 obj.update(view);
269 }
270 }
271
272 /**
273 * Draw object.
274 *
275 * @param g2 the g2
276 * @param at the at
277 */
278 protected abstract void drawObject(Graphics2D g2, AffineTransform at);
279
280 /**
281 * Draw.
282 *
283 * @param g the g
284 * @param view the view
285 */
286 public void draw(Graphics g, MapViewInterface view) {
287 Graphics2D g2 = (Graphics2D) g;
288
289 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
290 RenderingHints.VALUE_ANTIALIAS_ON);
291
292 drawObject(g2, at);
293
294 for (MapObject obj : childObjectList) {
295 obj.draw(g2, view);
296 }
297 }
298}