source: josm/src/org/openstreetmap/josm/data/osm/visitor/SimplePaintVisitor.java@ 144

Last change on this file since 144 was 144, checked in by imi, 18 years ago
  • added plugin system
  • added plugin: mappainter (thanks NickW!)
File size: 3.4 KB
Line 
1package org.openstreetmap.josm.data.osm.visitor;
2
3import java.awt.Color;
4import java.awt.Graphics;
5import java.awt.Point;
6
7import org.openstreetmap.josm.Main;
8import org.openstreetmap.josm.data.osm.Node;
9import org.openstreetmap.josm.data.osm.Segment;
10import org.openstreetmap.josm.data.osm.Way;
11import org.openstreetmap.josm.gui.NavigatableComponent;
12import org.openstreetmap.josm.tools.ColorHelper;
13
14/**
15 * A visitor that paint a simple scheme of every primitive it visits to a
16 * previous set graphic environment.
17 *
18 * @author imi
19 */
20public class SimplePaintVisitor implements Visitor {
21
22 public final static Color darkerblue = new Color(0,0,96);
23 public final static Color darkblue = new Color(0,0,128);
24 public final static Color darkgreen = new Color(0,128,0);
25
26 /**
27 * The environment to paint to.
28 */
29 protected Graphics g;
30 /**
31 * MapView to get screen coordinates.
32 */
33 protected NavigatableComponent nc;
34
35 protected static final double PHI = Math.toRadians(20);
36
37 /**
38 * Draw a small rectangle.
39 * White if selected (as always) or red otherwise.
40 *
41 * @param n The node to draw.
42 */
43 public void visit(Node n) {
44 drawNode(n, n.selected ? getPreferencesColor("selected", Color.WHITE)
45 : getPreferencesColor("node", Color.RED));
46 }
47
48 /**
49 * Draw just a line between the points.
50 * White if selected (as always) or green otherwise.
51 */
52 public void visit(Segment ls) {
53 drawSegment(ls, getPreferencesColor("segment", darkgreen));
54 }
55
56 /**
57 * Draw a darkblue line for all segments.
58 * @param w The way to draw.
59 */
60 public void visit(Way w) {
61 // only to overwrite with blue
62 Color wayColor = getPreferencesColor("way", darkblue);
63 for (Segment ls : w.segments) {
64 if (ls.incomplete) {
65 wayColor = getPreferencesColor("incomplete way", darkerblue);
66 break;
67 }
68 }
69
70 for (Segment ls : w.segments)
71 if (!ls.selected) // selected already in good color
72 drawSegment(ls, w.selected ? getPreferencesColor("selected", Color.WHITE) : wayColor);
73 }
74
75 /**
76 * Draw the node as small rectangle with the given color.
77 *
78 * @param n The node to draw.
79 * @param color The color of the node.
80 */
81 public void drawNode(Node n, Color color) {
82 Point p = nc.getPoint(n.eastNorth);
83 g.setColor(color);
84 g.drawRect(p.x-1, p.y-1, 2, 2);
85 }
86
87 /**
88 * Draw a line with the given color.
89 */
90 protected void drawSegment(Segment ls, Color col) {
91 if (ls.incomplete)
92 return;
93 if (ls.selected)
94 col = getPreferencesColor("selected", Color.WHITE);
95 g.setColor(col);
96 Point p1 = nc.getPoint(ls.from.eastNorth);
97 Point p2 = nc.getPoint(ls.to.eastNorth);
98 g.drawLine(p1.x, p1.y, p2.x, p2.y);
99
100 if (Main.pref.getBoolean("draw.segment.direction")) {
101 double t = Math.atan2(p2.y-p1.y, p2.x-p1.x) + Math.PI;
102 g.drawLine(p2.x,p2.y, (int)(p2.x + 10*Math.cos(t-PHI)), (int)(p2.y + 10*Math.sin(t-PHI)));
103 g.drawLine(p2.x,p2.y, (int)(p2.x + 10*Math.cos(t+PHI)), (int)(p2.y + 10*Math.sin(t+PHI)));
104 }
105 }
106
107 public static Color getPreferencesColor(String colName, Color def) {
108 String colStr = Main.pref.get("color."+colName);
109 if (colStr.equals("")) {
110 Main.pref.put("color."+colName, ColorHelper.color2html(def));
111 return def;
112 }
113 return ColorHelper.html2color(colStr);
114 }
115
116
117 public void setGraphics(Graphics g) {
118 this.g = g;
119 }
120
121 public void setNavigatableComponent(NavigatableComponent nc) {
122 this.nc = nc;
123 }
124}
Note: See TracBrowser for help on using the repository browser.