Changeset 26652 in osm for applications
- Timestamp:
- 2011-09-15T15:12:43+02:00 (13 years ago)
- Location:
- applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer
- Files:
-
- 2 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/JMapViewer.java
r26648 r26652 28 28 29 29 import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker; 30 import org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon; 30 31 import org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle; 31 32 import org.openstreetmap.gui.jmapviewer.interfaces.TileCache; … … 57 58 protected List<MapMarker> mapMarkerList; 58 59 protected List<MapRectangle> mapRectangleList; 60 protected List<MapPolygon> mapPolygonList; 59 61 60 62 protected boolean mapMarkersVisible; 61 63 protected boolean mapRectanglesVisible; 64 protected boolean mapPolygonsVisible; 62 65 63 66 protected boolean tileGridVisible; … … 116 119 mapMarkerList = new LinkedList<MapMarker>(); 117 120 mapRectangleList = new LinkedList<MapRectangle>(); 121 mapPolygonList = new LinkedList<MapPolygon>(); 118 122 mapMarkersVisible = true; 119 123 mapRectanglesVisible = true; 124 mapPolygonsVisible = true; 120 125 tileGridVisible = false; 121 126 setLayout(null); … … 247 252 248 253 /** 249 * Sets the displayed map pane and zoom level so that all map markers are254 * Sets the displayed map pane and zoom level so that all chosen map elements are 250 255 * visible. 251 256 */ 252 public void setDisplayToFitMapMarkers() { 253 if (mapMarkerList == null || mapMarkerList.size() == 0) 257 public void setDisplayToFitMapElements(boolean markers, boolean rectangles, boolean polygons) { 258 int nbElemToCheck = 0; 259 if (markers && mapMarkerList != null) 260 nbElemToCheck += mapMarkerList.size(); 261 if (rectangles && mapRectangleList != null) 262 nbElemToCheck += mapRectangleList.size(); 263 if (polygons && mapPolygonList != null) 264 nbElemToCheck += mapPolygonList.size(); 265 if (nbElemToCheck == 0) 254 266 return; 267 255 268 int x_min = Integer.MAX_VALUE; 256 269 int y_min = Integer.MAX_VALUE; … … 258 271 int y_max = Integer.MIN_VALUE; 259 272 int mapZoomMax = tileController.getTileSource().getMaxZoom(); 260 for (MapMarker marker : mapMarkerList) { 261 int x = OsmMercator.LonToX(marker.getLon(), mapZoomMax); 262 int y = OsmMercator.LatToY(marker.getLat(), mapZoomMax); 263 x_max = Math.max(x_max, x); 264 y_max = Math.max(y_max, y); 265 x_min = Math.min(x_min, x); 266 y_min = Math.min(y_min, y); 267 } 273 274 if (markers) { 275 for (MapMarker marker : mapMarkerList) { 276 int x = OsmMercator.LonToX(marker.getLon(), mapZoomMax); 277 int y = OsmMercator.LatToY(marker.getLat(), mapZoomMax); 278 x_max = Math.max(x_max, x); 279 y_max = Math.max(y_max, y); 280 x_min = Math.min(x_min, x); 281 y_min = Math.min(y_min, y); 282 } 283 } 284 285 if (rectangles) { 286 for (MapRectangle rectangle : mapRectangleList) { 287 x_max = Math.max(x_max, OsmMercator.LonToX(rectangle.getBottomRight().getLon(), mapZoomMax)); 288 y_max = Math.max(y_max, OsmMercator.LatToY(rectangle.getTopLeft().getLat(), mapZoomMax)); 289 x_min = Math.min(x_min, OsmMercator.LonToX(rectangle.getTopLeft().getLon(), mapZoomMax)); 290 y_min = Math.min(y_min, OsmMercator.LatToY(rectangle.getBottomRight().getLat(), mapZoomMax)); 291 } 292 } 293 294 if (polygons) { 295 for (MapPolygon polygon : mapPolygonList) { 296 for (Coordinate c : polygon.getPoints()) { 297 int x = OsmMercator.LonToX(c.getLon(), mapZoomMax); 298 int y = OsmMercator.LatToY(c.getLat(), mapZoomMax); 299 x_max = Math.max(x_max, x); 300 y_max = Math.max(y_max, y); 301 x_min = Math.min(x_min, x); 302 y_min = Math.min(y_min, y); 303 } 304 } 305 } 306 268 307 int height = Math.max(0, getHeight()); 269 308 int width = Math.max(0, getWidth()); 270 // System.out.println(x_min + " < x < " + x_max);271 // System.out.println(y_min + " < y < " + y_max);272 // System.out.println("tiles: " + width + " " + height);273 309 int newZoom = mapZoomMax; 274 310 int x = x_max - x_min; 275 311 int y = y_max - y_min; 276 312 while (x > width || y > height) { 277 // System.out.println("zoom: " + zoom + " -> " + x + " " + y);278 313 newZoom--; 279 314 x >>= 1; … … 287 322 setDisplayPosition(x, y, newZoom); 288 323 } 289 324 290 325 /** 291 326 * Sets the displayed map pane and zoom level so that all map markers are 292 327 * visible. 293 328 */ 294 public void setDisplayToFitMapRectangle() { 295 if (mapRectangleList == null || mapRectangleList.size() == 0) 296 return; 297 int x_min = Integer.MAX_VALUE; 298 int y_min = Integer.MAX_VALUE; 299 int x_max = Integer.MIN_VALUE; 300 int y_max = Integer.MIN_VALUE; 301 int mapZoomMax = tileController.getTileSource().getMaxZoom(); 302 for (MapRectangle rectangle : mapRectangleList) { 303 x_max = Math.max(x_max, OsmMercator.LonToX(rectangle.getBottomRight().getLon(), mapZoomMax)); 304 y_max = Math.max(y_max, OsmMercator.LatToY(rectangle.getTopLeft().getLat(), mapZoomMax)); 305 x_min = Math.min(x_min, OsmMercator.LonToX(rectangle.getTopLeft().getLon(), mapZoomMax)); 306 y_min = Math.min(y_min, OsmMercator.LatToY(rectangle.getBottomRight().getLat(), mapZoomMax)); 307 } 308 int height = Math.max(0, getHeight()); 309 int width = Math.max(0, getWidth()); 310 // System.out.println(x_min + " < x < " + x_max); 311 // System.out.println(y_min + " < y < " + y_max); 312 // System.out.println("tiles: " + width + " " + height); 313 int newZoom = mapZoomMax; 314 int x = x_max - x_min; 315 int y = y_max - y_min; 316 while (x > width || y > height) { 317 // System.out.println("zoom: " + zoom + " -> " + x + " " + y); 318 newZoom--; 319 x >>= 1; 320 y >>= 1; 321 } 322 x = x_min + (x_max - x_min) / 2; 323 y = y_min + (y_max - y_min) / 2; 324 int z = 1 << (mapZoomMax - newZoom); 325 x /= z; 326 y /= z; 327 setDisplayPosition(x, y, newZoom); 329 public void setDisplayToFitMapMarkers() { 330 setDisplayToFitMapElements(true, false, false); 331 } 332 333 /** 334 * Sets the displayed map pane and zoom level so that all map rectangles are 335 * visible. 336 */ 337 public void setDisplayToFitMapRectangles() { 338 setDisplayToFitMapElements(false, true, false); 339 } 340 341 /** 342 * Sets the displayed map pane and zoom level so that all map polygons are 343 * visible. 344 */ 345 public void setDisplayToFitMapPolygons() { 346 setDisplayToFitMapElements(false, false, true); 328 347 } 329 348 … … 507 526 // g.drawString("Tiles in cache: " + tileCache.getTileCount(), 50, 20); 508 527 528 if (mapPolygonsVisible && mapPolygonList != null) { 529 for (MapPolygon polygon : mapPolygonList) { 530 paintPolygon(g, polygon); 531 } 532 } 533 509 534 if (mapRectanglesVisible && mapRectangleList != null) { 510 535 for (MapRectangle rectangle : mapRectangleList) { 511 Coordinate topLeft = rectangle.getTopLeft(); 512 Coordinate bottomRight = rectangle.getBottomRight(); 513 if (topLeft != null && bottomRight != null) { 514 Point pTopLeft = getMapPosition(topLeft.getLat(), topLeft.getLon(), false); 515 Point pBottomRight = getMapPosition(bottomRight.getLat(), bottomRight.getLon(), false); 516 if (pTopLeft != null && pBottomRight != null) { 517 rectangle.paint(g, pTopLeft, pBottomRight); 518 } 519 } 536 paintRectangle(g, rectangle); 520 537 } 521 538 } … … 539 556 } 540 557 558 /** 559 * Paint a single rectangle. 560 */ 561 protected void paintRectangle(Graphics g, MapRectangle rectangle) { 562 Coordinate topLeft = rectangle.getTopLeft(); 563 Coordinate bottomRight = rectangle.getBottomRight(); 564 if (topLeft != null && bottomRight != null) { 565 Point pTopLeft = getMapPosition(topLeft, false); 566 Point pBottomRight = getMapPosition(bottomRight, false); 567 if (pTopLeft != null && pBottomRight != null) { 568 rectangle.paint(g, pTopLeft, pBottomRight); 569 } 570 } 571 } 572 573 /** 574 * Paint a single polygon. 575 */ 576 protected void paintPolygon(Graphics g, MapPolygon polygon) { 577 List<Coordinate> coords = polygon.getPoints(); 578 if (coords != null && coords.size() >= 3) { 579 List<Point> points = new LinkedList<Point>(); 580 for (Coordinate c : coords) { 581 Point p = getMapPosition(c, false); 582 if (p == null) { 583 return; 584 } 585 points.add(p); 586 } 587 polygon.paint(g, points); 588 } 589 } 590 541 591 /** 542 592 * Moves the visible map pane. … … 661 711 } 662 712 713 public void setMapPolygonList(List<MapPolygon> mapPolygonList) { 714 this.mapPolygonList = mapPolygonList; 715 repaint(); 716 } 717 718 public List<MapPolygon> getMapPolygonList() { 719 return mapPolygonList; 720 } 721 663 722 public void addMapMarker(MapMarker marker) { 664 723 mapMarkerList.add(marker); … … 691 750 } 692 751 752 public void addMapPolygon(MapPolygon polygon) { 753 mapPolygonList.add(polygon); 754 repaint(); 755 } 756 757 public void removeMapPolygon(MapPolygon polygon) { 758 mapPolygonList.remove(polygon); 759 repaint(); 760 } 761 762 public void removeAllMapPolygons() { 763 mapPolygonList.clear(); 764 repaint(); 765 } 766 693 767 public void setZoomContolsVisible(boolean visible) { 694 768 zoomSlider.setVisible(visible); … … 736 810 * Enables or disables painting of the {@link MapRectangle} 737 811 * 738 * @param map MarkersVisible812 * @param mapRectanglesVisible 739 813 * @see #addMapRectangle(MapRectangle) 740 814 * @see #getMapRectangleList() … … 742 816 public void setMapRectanglesVisible(boolean mapRectanglesVisible) { 743 817 this.mapRectanglesVisible = mapRectanglesVisible; 818 repaint(); 819 } 820 821 public boolean isMapPolygonsVisible() { 822 return mapPolygonsVisible; 823 } 824 825 /** 826 * Enables or disables painting of the {@link MapPolygon} 827 * 828 * @param mapPolygonsVisible 829 * @see #addMapPolygon(MapPolygon) 830 * @see #getMapPolygonList() 831 */ 832 public void setMapPolygonsVisible(boolean mapPolygonsVisible) { 833 this.mapPolygonsVisible = mapPolygonsVisible; 744 834 repaint(); 745 835 } -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/MapRectangleImpl.java
r26648 r26652 2 2 package org.openstreetmap.gui.jmapviewer; 3 3 4 import java.awt.BasicStroke; 4 5 import java.awt.Color; 5 6 import java.awt.Graphics; 7 import java.awt.Graphics2D; 6 8 import java.awt.Point; 9 import java.awt.Stroke; 7 10 8 11 import org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle; … … 17 20 private Coordinate topLeft; 18 21 private Coordinate bottomRight; 19 Color color; 22 private Color color; 23 private Stroke stroke; 20 24 21 25 public MapRectangleImpl(Bounds bounds) { 22 this(bounds, Color.BLUE );26 this(bounds, Color.BLUE, new BasicStroke(2)); 23 27 } 24 28 25 public MapRectangleImpl(Bounds bounds, Color color ) {29 public MapRectangleImpl(Bounds bounds, Color color, Stroke stroke) { 26 30 this.topLeft = new Coordinate(bounds.getMax().lat(), bounds.getMin().lon()); 27 31 this.bottomRight = new Coordinate(bounds.getMin().lat(), bounds.getMax().lon()); 28 32 this.color = color; 33 this.stroke = stroke; 29 34 } 30 35 … … 50 55 @Override 51 56 public void paint(Graphics g, Point topLeft, Point bottomRight) { 57 // Prepare graphics 58 Color oldColor = g.getColor(); 52 59 g.setColor(color); 60 Stroke oldStroke = null; 61 if (g instanceof Graphics2D) { 62 Graphics2D g2 = (Graphics2D) g; 63 oldStroke = g2.getStroke(); 64 g2.setStroke(stroke); 65 } 66 // Draw 53 67 g.drawRect(topLeft.x, topLeft.y, bottomRight.x - topLeft.x, bottomRight.y - topLeft.y); 68 // Restore graphics 69 g.setColor(oldColor); 70 if (g instanceof Graphics2D) { 71 ((Graphics2D) g).setStroke(oldStroke); 72 } 54 73 } 55 74
Note:
See TracChangeset
for help on using the changeset viewer.