root/trunk/PhysicalNodePainter.java

Revision 3, 2.7 kB (checked in by dpaola2, 6 days ago)

old pathways project added

Line 
1 /**
2  * PhysicalNodePainter.java
3  *
4  * This class represents a way to draw a PhysicalNode on the screen, and its
5  * instances can be used to draw any PhysicalNode on the screen in the way it
6  * represents.  JGraphViewer will have several of these associated with
7  * PhysicalNodes, and it will use them to draw those PhysicalNodes.  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 PhysicalNode.
10  */
11
12 import java.awt.*;
13 import java.awt.geom.*;
14 import java.util.*;
15
16 public class PhysicalNodePainter extends ObjectPainter {
17
18         public PhysicalNodePainter(Color color, float radius) {
19                 setColor(color);
20                 setRadius(radius);
21         }
22
23         public PhysicalNodePainter() {
24                 setColor(Color.BLACK);
25                 setRadius(2);
26         }
27
28         public Color getColor() { return color; }
29         public void setColor(Color color) { this.color = color; }
30         // when you type color enough, the word loses all meaning :)
31
32         // radius is in pixels
33         public float getRadius() { return radius; }
34         public void setRadius(float radius) { this.radius = radius; }
35         // it'd be cool if Java could automate trivial get/set functions
36
37         public double visualDistanceTo(Object object, Point2D.Double real, BackgroundMap map, AffineTransform realToScreen) {
38                 // convert both points to screen, return their distance less radius
39                 if(!((PhysicalNode)object).isOnMap(map)) return Double.POSITIVE_INFINITY;
40                 return realToScreen.transform(real, null).distance(
41                         ((Point2D.Double)computeLocation(object, realToScreen)))
42                         - radius;
43         }
44
45         protected void setupPainting(Graphics2D g) {
46                 g.setColor(color);
47                 g.setStroke(new BasicStroke());
48                 cachedEllipse.width = cachedEllipse.height = 2 * radius;
49         }
50
51         protected Object computeLocation(Object object, AffineTransform realToScreen) {
52                  return realToScreen.transform(((PhysicalNode) object).point, null);
53         }
54
55         protected boolean needToPaint(Object object, Object location, BackgroundMap map, Graphics2D g) {
56                 if(!((PhysicalNode)object).isOnMap(map)) return false;
57                 return true;
58         }
59
60         protected void paintSingleObject(Object object, Object location, Graphics2D g) {
61                 Point2D.Double screenPoint = (Point2D.Double)location;
62                 cachedEllipse.x = screenPoint.getX() - radius;
63                 cachedEllipse.y = screenPoint.getY() - radius;
64
65                 g.fill(cachedEllipse);
66         }
67
68         Color color; // the color we use to draw the node
69         float radius;  // how big the node should be drawn
70
71         Ellipse2D.Double cachedEllipse = new Ellipse2D.Double();
72
73         // Later, we will have multiple modes, for example, a mode for drawing
74         // the nodes as a point, or a mode for drawing them with a picture.
75         // Alternately, the same thing could be done by subclassing this.
76 }
Note: See TracBrowser for help on using the browser.