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