source: josm/branch/0.5/src/org/openstreetmap/josm/data/osm/visitor/SimplePaintVisitor.java@ 340

Last change on this file since 340 was 340, checked in by framm, 17 years ago
  • merged in JOSM render speed patch by Brent Easton
  • merged in Imi's changes to remove eigenheimstrasse.de references
  • merged in the plugin download whitespace stripping bugfix
File size: 5.9 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data.osm.visitor;
3
4import java.awt.Color;
5import java.awt.Graphics;
6import java.awt.Graphics2D;
7import java.awt.Point;
8import java.awt.Rectangle;
9import java.awt.geom.GeneralPath;
10import java.awt.geom.Line2D;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.data.osm.DataSet;
14import org.openstreetmap.josm.data.osm.Relation;
15import org.openstreetmap.josm.data.osm.Node;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.osm.Way;
18import org.openstreetmap.josm.gui.NavigatableComponent;
19import org.openstreetmap.josm.tools.ColorHelper;
20
21/**
22 * A visitor that paint a simple scheme of every primitive it visits to a
23 * previous set graphic environment.
24 *
25 * @author imi
26 */
27public class SimplePaintVisitor implements Visitor {
28
29 public final static Color darkerblue = new Color(0,0,96);
30 public final static Color darkblue = new Color(0,0,128);
31 public final static Color darkgreen = new Color(0,128,0);
32
33 /**
34 * The environment to paint to.
35 */
36 protected Graphics g;
37 /**
38 * MapView to get screen coordinates.
39 */
40 protected NavigatableComponent nc;
41
42 public boolean inactive;
43
44 protected static final double PHI = Math.toRadians(20);
45
46 /**
47 * Preferences
48 */
49 protected Color inactiveColor;
50 protected Color selectedColor;
51 protected Color nodeColor;
52 protected Color dfltWayColor;
53 protected Color incompleteColor;
54 protected Color backgroundColor;
55 protected boolean showDirectionArrow;
56 protected boolean showOrderNumber;
57
58 /**
59 * Draw subsequent segments of same color as one Path
60 */
61 protected Color currentColor = null;
62 protected GeneralPath currentPath = new GeneralPath();
63
64 public void visitAll(DataSet data) {
65
66 inactiveColor = getPreferencesColor("inactive", Color.DARK_GRAY);
67 selectedColor = getPreferencesColor("selected", Color.WHITE);
68 nodeColor = getPreferencesColor("node", Color.RED);
69 dfltWayColor = getPreferencesColor("way", darkblue);
70 incompleteColor = getPreferencesColor("incomplete way", darkerblue);
71 backgroundColor = getPreferencesColor("background", Color.BLACK);
72 showDirectionArrow = Main.pref.getBoolean("draw.segment.direction");
73 showOrderNumber = Main.pref.getBoolean("draw.segment.order_number");
74
75 for (final OsmPrimitive osm : data.ways)
76 if (!osm.deleted && !osm.selected)
77 osm.visit(this);
78 displaySegments(null);
79 for (final OsmPrimitive osm : data.nodes)
80 if (!osm.deleted && !osm.selected)
81 osm.visit(this);
82 for (final OsmPrimitive osm : data.getSelected())
83 if (!osm.deleted)
84 osm.visit(this);
85 displaySegments(null);
86 }
87
88 /**
89 * Draw a small rectangle.
90 * White if selected (as always) or red otherwise.
91 *
92 * @param n The node to draw.
93 */
94 public void visit(Node n) {
95 Color color = null;
96 if (inactive)
97 color = inactiveColor;
98 else if (n.selected)
99 color = selectedColor;
100 else
101 color = nodeColor;
102 drawNode(n, color);
103 }
104
105 /**
106 * Draw a darkblue line for all segments.
107 * @param w The way to draw.
108 */
109 public void visit(Way w) {
110 Color wayColor;
111 if (inactive)
112 wayColor = inactiveColor;
113 else {
114 wayColor = dfltWayColor;
115 }
116
117 int orderNumber = 0;
118 Node lastN = null;
119 for (Node n : w.nodes) {
120 if (lastN == null) {
121 lastN = n;
122 continue;
123 }
124 orderNumber++;
125 drawSegment(lastN, n, w.selected && !inactive ? selectedColor : wayColor, showDirectionArrow);
126 if (showOrderNumber)
127 drawOrderNumber(lastN, n, orderNumber);
128 lastN = n;
129 }
130 }
131
132 public void visit(Relation e) {
133 // relations are not (yet?) drawn.
134 }
135
136 /**
137 * Draw an number of the order of the two consecutive nodes within the
138 * parents way
139 */
140 protected void drawOrderNumber(Node n1, Node n2, int orderNumber) {
141 int strlen = (""+orderNumber).length();
142 Point p1 = nc.getPoint(n1.eastNorth);
143 Point p2 = nc.getPoint(n2.eastNorth);
144 int x = (p1.x+p2.x)/2 - 4*strlen;
145 int y = (p1.y+p2.y)/2 + 4;
146
147 Rectangle screen = g.getClipBounds();
148 if (screen.contains(x,y)) {
149 Color c = g.getColor();
150 g.setColor(backgroundColor);
151 g.fillRect(x-1, y-12, 8*strlen+1, 14);
152 g.setColor(c);
153 g.drawString(""+orderNumber, x, y);
154 }
155 }
156
157 /**
158 * Draw the node as small rectangle with the given color.
159 *
160 * @param n The node to draw.
161 * @param color The color of the node.
162 */
163 public void drawNode(Node n, Color color) {
164 Point p = nc.getPoint(n.eastNorth);
165 g.setColor(color);
166 Rectangle screen = g.getClipBounds();
167
168 if (screen.contains(p.x, p.y))
169 g.drawRect(p.x-1, p.y-1, 2, 2);
170 }
171
172 /**
173 * Draw a line with the given color.
174 */
175 protected void drawSegment(Node n1, Node n2, Color col, boolean showDirection) {
176 g.setColor(col);
177 Point p1 = nc.getPoint(n1.eastNorth);
178 Point p2 = nc.getPoint(n2.eastNorth);
179
180 Rectangle screen = g.getClipBounds();
181 Line2D line = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
182 if (screen.contains(p1.x, p1.y, p2.x, p2.y) || screen.intersectsLine(line))
183 {
184 g.drawLine(p1.x, p1.y, p2.x, p2.y);
185 currentPath.moveTo(p1.x, p1.y);
186 currentPath.lineTo(p2.x, p2.y);
187
188 if (showDirection) {
189 double t = Math.atan2(p2.y-p1.y, p2.x-p1.x) + Math.PI;
190 currentPath.lineTo((int)(p2.x + 10*Math.cos(t-PHI)), (int)(p2.y + 10*Math.sin(t-PHI)));
191 currentPath.moveTo((int)(p2.x + 10*Math.cos(t+PHI)), (int)(p2.y + 10*Math.sin(t+PHI)));
192 currentPath.lineTo(p2.x, p2.y);
193 }
194 }
195 }
196
197 public static Color getPreferencesColor(String colName, Color def) {
198 String colStr = Main.pref.get("color."+colName);
199 if (colStr.equals("")) {
200 Main.pref.put("color."+colName, ColorHelper.color2html(def));
201 return def;
202 }
203 return ColorHelper.html2color(colStr);
204 }
205
206 public void setGraphics(Graphics g) {
207 this.g = g;
208 }
209
210 public void setNavigatableComponent(NavigatableComponent nc) {
211 this.nc = nc;
212 }
213
214 protected void displaySegments(Color newColor) {
215 if (currentPath != null) {
216 g.setColor(currentColor);
217 ((Graphics2D) g).draw(currentPath);
218 currentPath = new GeneralPath();
219 currentColor = newColor;
220 }
221 }
222}
Note: See TracBrowser for help on using the repository browser.