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