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

Last change on this file since 30 was 30, checked in by imi, 18 years ago
  • Removed edit layer, combine action, save gpx (integrated in normal save)
  • Simplified and unified shortkeys
  • many small code simplifications
  • added undo
  • broken checkin!
File size: 2.6 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.data.osm.Key;
8import org.openstreetmap.josm.data.osm.LineSegment;
9import org.openstreetmap.josm.data.osm.Node;
10import org.openstreetmap.josm.data.osm.Track;
11import org.openstreetmap.josm.gui.MapView;
12
13/**
14 * A visitor that paint a simple scheme of every primitive it visits to a
15 * previous set graphic environment.
16 *
17 * @author imi
18 */
19public class SimplePaintVisitor implements Visitor {
20
21 private final static Color darkblue = new Color(0,0,128);
22 private final static Color darkgreen = new Color(0,128,0);
23
24 /**
25 * The environment to paint to.
26 */
27 private final Graphics g;
28 /**
29 * MapView to get screen coordinates.
30 */
31 private final MapView mv;
32 /**
33 * Can be set to non-<code>null</code> and then replace every other color.
34 */
35 private final Color forceColor;
36
37 /**
38 * Construct the painter visitor.
39 * @param g The graphics to draw to.
40 * @param mv The view to get screen coordinates from.
41 * @param forceColor If non-<code>null</code>, always draw with this color.
42 */
43 public SimplePaintVisitor(Graphics g, MapView mv, Color forceColor) {
44 this.g = g;
45 this.mv = mv;
46 this.forceColor = forceColor;
47 }
48
49 /**
50 * Draw a small rectangle.
51 * White if selected (as always) or red otherwise.
52 *
53 * @param n The node to draw.
54 */
55 public void visit(Node n) {
56 drawNode(n, n.isSelected() ? Color.WHITE : Color.RED);
57 }
58
59 /**
60 * Draw just a line between the points.
61 * White if selected (as always) or green otherwise.
62 */
63 public void visit(LineSegment ls) {
64 drawLineSegment(ls, darkgreen);
65 }
66
67 /**
68 * Draw a darkblue line for all line segments.
69 * @param t The track to draw.
70 */
71 public void visit(Track t) {
72 for (LineSegment ls : t.segments)
73 drawLineSegment(ls, darkblue);
74 }
75
76 /**
77 * Do not draw a key.
78 */
79 public void visit(Key k) {
80 }
81
82 /**
83 * Draw the node as small rectangle with the given color.
84 *
85 * @param n The node to draw.
86 * @param color The color of the node.
87 */
88 private void drawNode(Node n, Color color) {
89 Point p = mv.getScreenPoint(n.coor);
90 g.setColor(forceColor != null ? forceColor : color);
91 g.drawRect(p.x-1, p.y-1, 2, 2);
92 }
93
94 /**
95 * Draw a line with the given color.
96 */
97 private void drawLineSegment(LineSegment ls, Color col) {
98 if (forceColor != null)
99 col = forceColor;
100 else if (ls.isSelected())
101 col = Color.WHITE;
102 g.setColor(col);
103 Point p1 = mv.getScreenPoint(ls.start.coor);
104 Point p2 = mv.getScreenPoint(ls.end.coor);
105 g.drawLine(p1.x, p1.y, p2.x, p2.y);
106 }
107}
Note: See TracBrowser for help on using the repository browser.