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