root/trunk/PhysicalEdgePainter.java

Revision 3, 2.6 kB (checked in by dpaola2, 1 week ago)

old pathways project added

Line 
1 /**
2  * PhysicalEdgePainter.java
3  *
4  * This class represents a way to draw a PhysicalEdge on the screen, and its
5  * instances can be used to draw any PhysicalEdge on the screen in the way it
6  * represents.  JGraphViewer will have several of these associated with
7  * PhysicalEdges, and it will use them to draw those PhysicalEdges.  Objects
8  * of this type will also be able to determine how close a mouse-click on a
9  * given point is to the on-screen representation of a PhysicalEdge.
10  */
11
12 import java.awt.*;
13 import java.awt.geom.*;
14 import java.util.*;
15
16 public class PhysicalEdgePainter extends ObjectPainter {
17
18         public PhysicalEdgePainter(Color color, float thickness) {
19                 setColor(color);
20                 setThickness(thickness);
21         }
22
23         public PhysicalEdgePainter() {
24                 setColor(Color.BLACK);
25                 setThickness(1);
26         }
27
28         public Color getColor() { return color; }
29         public void setColor(Color color) { this.color = color; }
30
31         // thickness is in pixels
32         public float getThickness() { return thickness; }
33         public void setThickness(float thickness) { this.thickness = thickness; }
34
35         public double visualDistanceTo(Object object, Point2D.Double real, BackgroundMap map, AffineTransform realToScreen) {
36                 if(!((PhysicalEdge)object).isOnMap(map)) return Double.POSITIVE_INFINITY;               
37                 return ((Line2D.Double)computeLocation(object, realToScreen)).ptSegDist(
38                         realToScreen.transform(real, null))
39                         - thickness / 2;
40         }
41
42         protected void setupPainting(Graphics2D g) {
43
44                 g.setColor(color);
45                 g.setStroke(new BasicStroke(thickness));
46         }
47
48         protected Object computeLocation(Object object, AffineTransform realToScreen) {
49                 // casts make this look more complicated than it is; it really
50                 // just gets the endpoints of the edge, transforms them, and
51                 // makes them into a line.
52
53                 return new Line2D.Double(
54                         realToScreen.transform(((PhysicalNode)((PhysicalEdge)object).getNodeA()).point, null),
55                         realToScreen.transform(((PhysicalNode)((PhysicalEdge)object).getNodeB()).point, null)
56                 );
57         }
58
59         protected boolean needToPaint(Object object, Object location, BackgroundMap map, Graphics2D g) {
60                 if(!((PhysicalEdge)object).isOnMap(map)) return false;
61                 return g.getClipBounds(new Rectangle()).intersectsLine((Line2D)location);
62         }
63
64         protected void paintSingleObject(Object object, Object location, Graphics2D g) {
65                 g.draw((Line2D.Double)location);
66         }
67
68         Color color; // the color we use to draw the edge
69         float thickness;  // how thick it should be
70
71         // Later, we will have multiple modes, for example, a mode for drawing
72         // the nodes as a point, or a mode for drawing them with a picture.
73         // Alternately, the same thing could be done by subclassing this.
74 }
Note: See TracBrowser for help on using the browser.