/Users/lyon/j4p/src/graphics/graph/Node.java
|
1 package graphics.graph;
2
3 import java.awt.*;
4
5 class Node {
6 private double x;
7 private double y;
8
9 private double dx;
10 private double dy;
11
12 private boolean fixed;
13
14 private String lbl;
15
16 private Class nodeClass = null;
17
18 public Node(String _lbl, Class c) {
19 lbl = _lbl;
20 nodeClass = c;
21 }
22
23 public String toString() {
24 return lbl + ":nodeClass:" + nodeClass;
25 }
26
27 public Class getNodeClass() {
28 return nodeClass;
29 }
30
31 public void setNodeClass(Class c) {
32 nodeClass = c;
33 }
34
35
36 public double getX() {
37 return x;
38 }
39
40
41 /**
42 * moveNode nuges the node if it is not fixed
43 * it also checks the extremes and
44 * reduces the dimensions
45 */
46 public void nugeNode(Dimension d) {
47 if (!isFixed())
48 nuge();
49 ceilingNode(d);
50 oneHalfDxDy();
51 }
52
53 public static void nIsNotFixed(Node n) {
54 //what does this code do?
55 //int temp = 0;
56 //int halfTemp = temp/2;
57 //n.setX(n.getX() + (temp * Math.random() - halfTemp));
58 //n.setY(n.getY() + (temp * Math.random() - halfTemp));
59 }
60
61 public void incrementDxDy(double _dx, double _dy) {
62 dx = dx + _dx;
63 dy = dy + _dy;
64 }
65
66 /**
67 * push node by small amount on screen
68 */
69 public void nuge() {
70 double nodeX = getX();
71 int minDistance = 15;
72 setX(nodeX
73 + Math.max(-minDistance,
74 Math.min(
75 minDistance,
76 getDx())));
77 double nodeY = getY();
78 setY(nodeY
79 + Math.max(-minDistance,
80 Math.min(minDistance, getDy())));
81 }
82
83 /**
84 * ceilingNode makes sure that node
85 * does not exceed a given dimension
86 */
87 public void ceilingNode(Dimension d) {
88 if (getX() > d.width)
89 setX(d.width);
90 if (getY() > d.height)
91 setY(d.height);
92 }
93
94 /**
95 * cut dx and dy by one half
96 * @author D. Lyon
97 */
98 public void oneHalfDxDy() {
99 setDx(getDx() / 2);
100 setDy(getDy() / 2);
101 }
102
103 public void setX(double _x) {
104 x = _x;
105 if (x < 0) x = 0;
106 }
107
108 public double getY() {
109 return y;
110 }
111
112 public void setY(double _y) {
113 y = _y;
114 if (y < 0) y = 0;
115 }
116
117 public double getDx() {
118 return dx;
119 }
120
121 public void setDx(double dx) {
122 this.dx = dx;
123 }
124
125 public double getDy() {
126 return dy;
127 }
128
129 public void setDy(double dy) {
130 this.dy = dy;
131 }
132
133 public Dimension getSize(FontMetrics fm) {
134 int w = fm.stringWidth(getLbl()) + 10;
135 int h = fm.getHeight() + 4;
136 return new Dimension(w, h);
137 }
138
139 public void draw(Graphics g) {
140 FontMetrics fm = g.getFontMetrics();
141 Dimension d = getSize(fm);
142 int w = d.width;
143 int h = d.height;
144 int ix = (int) x;
145 int iy = (int) y;
146 g.fillRect(ix - w / 2, iy - h / 2, w, h);
147 g.setColor(Color.black);
148 g.drawRect(ix - w / 2, iy - h / 2, w - 1, h - 1);
149 g.drawString(getLbl(), (ix - (w - 10) / 2),
150 (iy - (h - 4) / 2) + fm.getAscent());
151 }
152
153 public boolean isFixed() {
154 return fixed;
155 }
156
157 public void setFixed(boolean fixed) {
158 this.fixed = fixed;
159 }
160
161
162 public String getLbl() {
163 return lbl;
164 }
165
166 public void setLbl(String _lbl) {
167 lbl = _lbl;
168 }
169 }