source: osm/applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/MapRectangleImpl.java@ 29513

Last change on this file since 29513 was 29513, checked in by galo, 12 years ago

Includes a tree of layers to the map

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.gui.jmapviewer;
3
4import java.awt.BasicStroke;
5import java.awt.Color;
6import java.awt.Graphics;
7import java.awt.Graphics2D;
8import java.awt.Point;
9import java.awt.Rectangle;
10import java.awt.Stroke;
11
12import org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle;
13
14/**
15 * @author Vincent
16 *
17 */
18public class MapRectangleImpl extends MapObjectImpl implements MapRectangle {
19
20 private Coordinate topLeft;
21 private Coordinate bottomRight;
22
23 public MapRectangleImpl(String name, Coordinate topLeft, Coordinate bottomRight) {
24 this(null, name, topLeft, bottomRight);
25 }
26 public MapRectangleImpl(Layer layer, Coordinate topLeft, Coordinate bottomRight) {
27 this(layer, null, topLeft, bottomRight);
28 }
29 public MapRectangleImpl(Layer layer, String name, Coordinate topLeft, Coordinate bottomRight) {
30 this(layer, name, topLeft, bottomRight, getDefaultStyle());
31 }
32 public MapRectangleImpl(Layer layer, String name, Coordinate topLeft, Coordinate bottomRight, Style style) {
33 super(layer, name, style);
34 this.topLeft = topLeft;
35 this.bottomRight = bottomRight;
36 }
37
38 /* (non-Javadoc)
39 * @see org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle#getTopLeft()
40 */
41 @Override
42 public Coordinate getTopLeft() {
43 return topLeft;
44 }
45
46 /* (non-Javadoc)
47 * @see org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle#getBottomRight()
48 */
49 @Override
50 public Coordinate getBottomRight() {
51 return bottomRight;
52 }
53
54 /* (non-Javadoc)
55 * @see org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle#paint(java.awt.Graphics, java.awt.Point, java.awt.Point)
56 */
57 @Override
58 public void paint(Graphics g, Point topLeft, Point bottomRight) {
59 // Prepare graphics
60 Color oldColor = g.getColor();
61 g.setColor(getColor());
62 Stroke oldStroke = null;
63 if (g instanceof Graphics2D) {
64 Graphics2D g2 = (Graphics2D) g;
65 oldStroke = g2.getStroke();
66 g2.setStroke(getStroke());
67 }
68 // Draw
69 g.drawRect(topLeft.x, topLeft.y, bottomRight.x - topLeft.x, bottomRight.y - topLeft.y);
70 // Restore graphics
71 g.setColor(oldColor);
72 if (g instanceof Graphics2D) {
73 ((Graphics2D) g).setStroke(oldStroke);
74 }
75 int width=bottomRight.x-topLeft.x;
76 int height=bottomRight.y-topLeft.y;
77 Point p= new Point(topLeft.x+(width/2), topLeft.y+(height/2));
78 if(getLayer()==null||getLayer().isVisibleTexts()) paintText(g, p);
79 }
80 public static Style getDefaultStyle(){
81 return new Style(Color.BLUE, null, new BasicStroke(2), getDefaultFont());
82 }
83 @Override
84 public String toString() {
85 return "MapRectangle from " + topLeft + " to " + bottomRight;
86 }
87}
Note: See TracBrowser for help on using the repository browser.