Changeset 8 in josm for src/org/openstreetmap/josm/gui/MapView.java
- Timestamp:
- 2005-10-03T04:18:02+02:00 (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/gui/MapView.java
r7 r8 1 1 package org.openstreetmap.josm.gui; 2 2 3 import java.awt.Color;4 3 import java.awt.Graphics; 5 4 import java.awt.Point; … … 8 7 import java.awt.event.ComponentListener; 9 8 import java.awt.event.KeyEvent; 10 import java.util.HashSet;11 import java.util.LinkedList;12 import java.util.List;13 import java.util.Set;14 9 15 10 import javax.swing.AbstractAction; … … 27 22 import org.openstreetmap.josm.data.osm.Track; 28 23 import org.openstreetmap.josm.data.projection.Projection; 24 import org.openstreetmap.josm.gui.engine.Engine; 25 import org.openstreetmap.josm.gui.engine.SimpleEngine; 29 26 30 27 /** … … 81 78 public final DataSet dataSet; 82 79 80 /** 81 * The drawing engine. 82 */ 83 private Engine engine; 83 84 84 85 /** … … 94 95 // initialize the projection 95 96 setProjection(Main.pref.projection.clone()); 97 98 // initialize the engine 99 engine = new SimpleEngine(this); 96 100 } 97 101 … … 136 140 projection.latlon2xy(p); 137 141 } 138 return new Point(toScreenX(p.x), toScreenY(p.y)); 142 int x = ((int)Math.round((p.x-center.x) / scale + getWidth()/2)); 143 int y = ((int)Math.round((center.y-p.y) / scale + getHeight()/2)); 144 return new Point(x,y); 139 145 } 140 146 … … 179 185 180 186 // pending line segments 181 for (LineSegment ls : dataSet.pendingLineSegments ) {182 Point A = getScreenPoint(ls. start.coor);183 Point B = getScreenPoint(ls. end.coor);187 for (LineSegment ls : dataSet.pendingLineSegments()) { 188 Point A = getScreenPoint(ls.getStart().coor); 189 Point B = getScreenPoint(ls.getEnd().coor); 184 190 double c = A.distanceSq(B); 185 191 double a = p.distanceSq(B); … … 194 200 // tracks & line segments 195 201 minDistanceSq = Double.MAX_VALUE; 196 for (Track t : dataSet.tracks ) {197 for (LineSegment ls : t.segments ) {198 Point A = getScreenPoint(ls. start.coor);199 Point B = getScreenPoint(ls. end.coor);202 for (Track t : dataSet.tracks()) { 203 for (LineSegment ls : t.segments()) { 204 Point A = getScreenPoint(ls.getStart().coor); 205 Point B = getScreenPoint(ls.getEnd().coor); 200 206 double c = A.distanceSq(B); 201 207 double a = p.distanceSq(B); … … 213 219 // TODO areas 214 220 215 216 221 return null; // nothing found 217 222 } … … 224 229 */ 225 230 public void zoomTo(GeoPoint newCenter, double scale) { 231 boolean oldAutoScale = autoScale; 232 GeoPoint oldCenter = center; 233 double oldScale = this.scale; 234 226 235 autoScale = false; 227 236 center = newCenter.clone(); … … 229 238 this.scale = scale; 230 239 recalculateCenterScale(); 231 fireStateChanged(); // in case of autoScale, recalculate fired. 240 241 firePropertyChange("center", oldCenter, center); 242 if (oldAutoScale != autoScale) 243 firePropertyChange("autoScale", oldAutoScale, autoScale); 244 if (oldScale != scale) 245 firePropertyChange("scale", oldScale, scale); 232 246 } 233 247 … … 254 268 h = 20; 255 269 Bounds bounds = dataSet.getBoundsXY(); 270 271 boolean oldAutoScale = autoScale; 272 GeoPoint oldCenter = center; 273 double oldScale = this.scale; 274 256 275 if (bounds == null) { 257 276 // no bounds means standard scale and center … … 266 285 scale = Math.max(scaleX, scaleY); // minimum scale to see all of the screen 267 286 } 268 fireStateChanged(); 287 288 firePropertyChange("center", oldCenter, center); 289 if (oldAutoScale != autoScale) 290 firePropertyChange("autoScale", oldAutoScale, autoScale); 291 if (oldScale != scale) 292 firePropertyChange("scale", oldScale, scale); 269 293 } 270 294 repaint(); … … 283 307 @Override 284 308 public void paint(Graphics g) { 285 // empty out everything 286 g.setColor(Color.BLACK); 287 g.fillRect(0,0,getWidth(),getHeight()); 288 289 // draw tracks 290 if (dataSet.tracks != null) 291 for (Track track : dataSet.tracks) 292 for (LineSegment ls : track.segments) { 293 g.setColor(ls.selected || track.selected ? Color.WHITE : Color.GRAY); 294 g.drawLine(toScreenX(ls.start.coor.x), toScreenY(ls.start.coor.y), 295 toScreenX(ls.end.coor.x), toScreenY(ls.end.coor.y)); 296 } 297 298 // draw pending line segments 299 for (LineSegment ls : dataSet.pendingLineSegments) { 300 g.setColor(ls.selected ? Color.WHITE : Color.LIGHT_GRAY); 301 g.drawLine(toScreenX(ls.start.coor.x), toScreenY(ls.start.coor.y), 302 toScreenX(ls.end.coor.x), toScreenY(ls.end.coor.y)); 303 } 304 305 // draw nodes 306 Set<Integer> alreadyDrawn = new HashSet<Integer>(); 307 int width = getWidth(); 308 for (Node w : dataSet.nodes) { 309 g.setColor(w.selected ? Color.WHITE : Color.RED); 310 int x = toScreenX(w.coor.x); 311 int y = toScreenY(w.coor.y); 312 int size = 3; 313 if (alreadyDrawn.contains(y*width+x)) { 314 size = 7; 315 x -= 2; 316 y -= 2; 317 } else 318 alreadyDrawn.add(y*width+x); 319 g.drawArc(x, y, size, size, 0, 360); 320 } 321 } 322 323 /** 324 * Return the x-screen coordinate for the given point. 325 * @param x The X-position (easting) of the desired point 326 * @return The screen coordinate for the point. 327 */ 328 public int toScreenX(double x) { 329 return (int)Math.round((x-center.x) / scale + getWidth()/2); 330 } 331 332 /** 333 * Return the y-screen coordinate for the given point. 334 * @param y The Y-position (northing) of the desired point 335 * @return The screen coordinate for the point. 336 */ 337 public int toScreenY(double y) { 338 return (int)Math.round((center.y-y) / scale + getHeight()/2); 339 } 340 341 342 // Event handling functions and data 343 344 /** 345 * The event list with all state chaned listener 346 */ 347 List<ChangeListener> listener = new LinkedList<ChangeListener>(); 348 /** 349 * Add an event listener to the state changed event queue. If passed 350 * <code>null</code>, nothing happens. 351 */ 352 public final void addChangeListener(ChangeListener l) { 353 if (l != null) 354 listener.add(l); 355 } 356 /** 357 * Remove an event listener from the event queue. If passed 358 * <code>null</code>, nothing happens. 359 */ 360 public final void removeChangeListener(ChangeListener l) { 361 listener.remove(l); 362 } 363 /** 364 * Fires an ChangeEvent. Occours, when an non-data value changed, as example 365 * the autoScale - state or the center. Is not fired, dataSet internal things 366 * changes. 367 */ 368 public final void fireStateChanged() { 369 ChangeEvent e = null; 370 for(ChangeListener l : listener) { 371 if (e == null) 372 e = new ChangeEvent(this); 373 l.stateChanged(e); 374 } 375 } 376 309 engine.init(g); 310 engine.drawBackground(getPoint(0,0,true), getPoint(getWidth(), getHeight(), true)); 311 312 for (Track t : dataSet.tracks()) 313 engine.drawTrack(t); 314 for (LineSegment ls : dataSet.pendingLineSegments()) 315 engine.drawPendingLineSegment(ls); 316 for (Node n : dataSet.nodes) 317 engine.drawNode(n); 318 } 319 377 320 /** 378 321 * Notify from the projection, that something has changed. … … 393 336 if (projection == this.projection) 394 337 return; 338 339 Projection oldProjection = this.projection; 340 395 341 if (this.projection != null) 396 342 this.projection.removeChangeListener(this); 397 343 this.projection = projection; 398 344 projection.addChangeListener(this); 345 399 346 stateChanged(new ChangeEvent(this)); 347 firePropertyChange("projection", oldProjection, projection); 400 348 } 401 349 … … 421 369 if (this.autoScale != autoScale) { 422 370 this.autoScale = autoScale; 423 fire StateChanged();371 firePropertyChange("autoScale", !autoScale, autoScale); 424 372 } 425 373 }
Note:
See TracChangeset
for help on using the changeset viewer.