-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdge.java
More file actions
293 lines (258 loc) · 10.6 KB
/
Edge.java
File metadata and controls
293 lines (258 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package tpgraphe;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
public class Edge implements Comparable<Edge> {
private static int edgeCount = 0;
private GraphNode nodeFrom = null;
private GraphNode nodeTo = null;
private Float value = new Float(0);
private Integer ID = null;
private boolean dijkstra = false;
private boolean improvement = false;
//private Sommet sommer1 = null;//From
//private Sommet sommet2 = null;//To
private Point ps1 = new Point();
private Point ps2 = new Point();
private Rectangle rec = new Rectangle(24, 24);
//private int val = 0;//Value
private Color color = Color.BLACK;
/*public Arc(Sommet a, Sommet b, int val) {
this(a, b);
this.val = val;
}
public Arc(Sommet a, Sommet b) {
this.nodeFrom = a;
this.nodeTo = b;
updateLoc();
}*/
public Edge(GraphNode nodeFrom, GraphNode nodeTo) {
this.nodeTo = nodeTo;
this.nodeFrom = nodeFrom;
ID = Edge.edgeCount++;
}
public Edge(GraphNode nodeFrom, GraphNode nodeTo, Float value) {
this(nodeFrom, nodeTo);
this.value = value;
}
public static int getEdgeCount() {
return edgeCount;
}
public GraphNode getNodeTo() {
return nodeTo;
}
public GraphNode getNodeFrom() {
return nodeFrom;
}
public Float getValue() {
return value;
}
public void setValue(Float value) {
this.value = value;
}
public Integer getID() {
return ID;
}
public boolean isDijkstra() {
return dijkstra;
}
public void setDijkstra(boolean dijkstra) {
this.dijkstra = dijkstra;
}
public boolean isImprovement() {
return improvement;
}
public void setImprovement(boolean improvement) {
this.improvement = improvement;
}
/*public int getName() {
return val;
}
public void setVal(int val) {
this.val = val;
}*/
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Rectangle getloc() {
Rectangle rec = this.rec;
rec.x = rec.x - rec.width / 2;
rec.y = rec.y - rec.height / 2;
return rec;
}
private void updateLoc() {
int dx = nodeTo.getPosition().x - nodeFrom.getPosition().x, dy = nodeTo.getPosition().y - nodeFrom.getPosition().y;
rec.x = nodeFrom.getPosition().x + dx / 2;
rec.y = nodeFrom.getPosition().y + dy / 2;
}
public void draw(Graphics a) {
if (nodeFrom != null && nodeTo != null) {
calculateCoordinate();
//System.out.print("so1=" + nodeFrom.rec.getLocation() + ", ps1=" + ps1.getLocation() + ", ");
//System.out.print("so2=" + nodeTo.rec.getLocation() + ", ps2=" + ps2.getLocation() + "\n");
}
Graphics2D g = (Graphics2D) a;
Color c = g.getColor();
g.setColor(this.getColor());
g.setStroke(new BasicStroke(2));
//g.drawLine(nodeFrom.getPosition().x, nodeFrom.getPosition().y, nodeTo.getPosition().x, nodeTo.getPosition().y);
/*
* int dx = nodeTo.getPosition().x - nodeFrom.getPosition().x, dy =
* nodeTo.getPosition().y - nodeFrom.getPosition().y; int h = 18; double
* alpha = Math.atan2(dy, dx); double dcos = h * Math.cos(alpha), dsin =
* h * Math.sin(alpha); Point p = new Point((int)
* (nodeTo.getPosition().x - dcos - dsin), (int)
* (nodeTo.getPosition().y + dcos - dsin)); Point q = new Point((int)
* (nodeTo.getPosition().x - dcos + dsin), (int)
* (nodeTo.getPosition().y - dcos - dsin));
* g.drawLine(nodeTo.getPosition().x, nodeTo.getPosition().y, p.x,
* p.y); g.drawLine(nodeTo.getPosition().x, nodeTo.getPosition().y,
* q.x, q.y);
*/
g.drawLine(ps1.x, ps1.y, ps2.x, ps2.y);
int dx = ps2.x - ps1.x, dy = ps2.y - ps1.y;
int h = 8;
double alpha = Math.atan2(dy, dx);
double dcos = h * Math.cos(alpha), dsin = h * Math.sin(alpha);
Point p = new Point((int) (ps2.x - dcos - dsin), (int) (ps2.y + dcos - dsin));
Point q = new Point((int) (ps2.x - dcos + dsin), (int) (ps2.y - dcos - dsin));
g.drawLine(ps2.x, ps2.y, p.x, p.y);
g.drawLine(ps2.x, ps2.y, q.x, q.y);
////////////////////////////////////////////////////////////////////////
updateLoc();
g.setColor(Color.GRAY);
//g.fillRect(rec.x - rec.width / 2, rec.y - rec.height / 2, rec.width, rec.height);
g.fillRoundRect(rec.x - rec.width / 2, rec.y - rec.height / 2, rec.width, rec.height, 6, 6);
g.setColor(Color.WHITE);
g.drawString(String.valueOf(value), rec.x - 2, rec.y + 4);
g.setColor(c);
}
public String getDrawDirection() {
String drawDirection = "";
int boxFromX2 = nodeFrom.getLoc().x + nodeFrom.getLoc().width;
int boxFromY2 = nodeFrom.getLoc().y + nodeFrom.getLoc().height;
int boxToX2 = nodeTo.getLoc().x + nodeTo.getLoc().width;
int boxToY2 = nodeTo.getLoc().y + nodeTo.getLoc().height;
if (nodeFrom.getLoc().x > boxToX2 && boxFromY2 < nodeTo.getLoc().y) {
drawDirection = "RIGHT_TOP";
} else if (nodeFrom.getLoc().x > boxToX2 && nodeFrom.getLoc().y > boxToY2) {
drawDirection = "RIGHT_BOTTOM";
} else if (boxFromX2 < nodeTo.getLoc().x && nodeFrom.getLoc().y > boxToY2) {
drawDirection = "LEFT_BOTTOM";
} else if (boxFromX2 < nodeTo.getLoc().x && boxFromY2 < nodeTo.getLoc().y) {
drawDirection = "LEFT_TOP";
} else if (nodeFrom.getLoc().x > boxToX2) {
drawDirection = "RIGHT";
} else if (boxFromY2 < nodeTo.getLoc().y) {
drawDirection = "TOP";
} else if (nodeFrom.getLoc().y > boxToY2) {
drawDirection = "BOTTOM";
} else if (boxFromX2 < nodeTo.getLoc().x) {
drawDirection = "LEFT";
}
return drawDirection;
}
private void calculateCoordinate() {
String drawDirection = getDrawDirection();
int boxFromX2 = nodeFrom.getLoc().x + nodeFrom.getLoc().width;
int boxFromY2 = nodeFrom.getLoc().y + nodeFrom.getLoc().height;
int boxToX2 = nodeTo.getLoc().x + nodeTo.getLoc().width;
int boxToY2 = nodeTo.getLoc().y + nodeTo.getLoc().height;
int diffY = 0;
int diffX = 0;
if (drawDirection == "BOTTOM" || drawDirection == "TOP") {
if (nodeFrom.getLoc().x <= nodeTo.getLoc().x && boxFromX2 >= boxToX2) {
diffX = (nodeTo.getLoc().width) / 2;
diffX = nodeTo.getLoc().x + diffX;
} else if (nodeFrom.getLoc().x > nodeTo.getLoc().x && boxFromX2 > boxToX2) {
diffX = (boxToX2 - nodeFrom.getLoc().x) / 2;
diffX = nodeFrom.getLoc().x + diffX;
} else if (nodeFrom.getLoc().x < nodeTo.getLoc().x && boxFromX2 < boxToX2) {
diffX = (boxFromX2 - nodeTo.getLoc().x) / 2;
diffX = nodeTo.getLoc().x + diffX;
} else if (nodeFrom.getLoc().x > nodeTo.getLoc().x && boxFromX2 < boxToX2) {
diffX = (nodeFrom.getLoc().width) / 2;
diffX = nodeFrom.getLoc().x + diffX;
}
if (diffX == 0) {
diffX = nodeFrom.getLoc().x + (nodeFrom.getLoc().width / 2);
}
}
if (drawDirection == "RIGHT" || drawDirection == "LEFT") {
if (nodeFrom.getLoc().y <= nodeTo.getLoc().y && boxFromY2 >= boxToY2) {
diffY = (boxToY2 - nodeTo.getLoc().y) / 2;
diffY = nodeTo.getLoc().y + diffY;
} else if (nodeFrom.getLoc().y > nodeTo.getLoc().y && boxFromY2 > boxToY2) {
diffY = (boxToY2 - nodeFrom.getLoc().y) / 2;
diffY = nodeFrom.getLoc().y + diffY;
} else if (nodeFrom.getLoc().y < nodeTo.getLoc().y && boxFromY2 < boxToY2) {
diffY = (boxFromY2 - nodeTo.getLoc().y) / 2;
diffY = nodeTo.getLoc().y + diffY;
} else if (nodeFrom.getLoc().y > nodeTo.getLoc().y && boxFromY2 < boxToY2) {
diffY = (nodeFrom.getLoc().height) / 2;
diffY = nodeFrom.getLoc().y + diffY;
}
}
if (drawDirection == "RIGHT_BOTTOM") {
this.ps1.x = nodeFrom.getLoc().x + diffX;
this.ps1.y = nodeFrom.getLoc().y;
this.ps2.x = boxToX2;
this.ps2.y = boxToY2 - diffY;
} else if (drawDirection == "RIGHT_TOP") {
this.ps1.x = nodeFrom.getLoc().x;
this.ps1.y = boxFromY2 - diffY;
this.ps2.x = boxToX2 - diffX;
this.ps2.y = nodeTo.getLoc().y;
} else if (drawDirection == "LEFT_BOTTOM") {
this.ps1.x = boxFromX2 - diffX;
this.ps1.y = nodeFrom.getLoc().y;
this.ps2.x = nodeTo.getLoc().x;
this.ps2.y = boxToY2 - diffY;
} else if (drawDirection == "LEFT_TOP") {
this.ps1.x = boxFromX2;
this.ps1.y = boxFromY2 - diffY;
this.ps2.x = nodeTo.getLoc().x + diffX;
this.ps2.y = nodeTo.getLoc().y;
} else if (drawDirection == "RIGHT") {
this.ps1.x = nodeFrom.getLoc().x;
this.ps1.y = diffY;
this.ps2.x = boxToX2;
this.ps2.y = diffY;
} else if (drawDirection == "TOP") {
this.ps1.x = diffX;
this.ps1.y = boxFromY2;
this.ps2.x = diffX;
this.ps2.y = nodeTo.getLoc().y;
} else if (drawDirection == "BOTTOM") {
this.ps1.x = diffX;
this.ps1.y = nodeFrom.getLoc().y;
this.ps2.x = diffX;
this.ps2.y = boxToY2;
} else if (drawDirection == "LEFT") {
this.ps1.x = boxFromX2;
this.ps1.y = diffY;
this.ps2.x = nodeTo.getLoc().x;
this.ps2.y = diffY;
}
}
public String toString() {
return "Edge{ id=" + ID + ", nodeFrom=" + (nodeFrom == null ? "null" : nodeFrom.getName()) + ", nodeTo=" +
(nodeTo == null ? "null" : nodeTo.getName()) + ", value=" + (value == null ? "null" : value) +
", color=" + color + ", dij=" + dijkstra + ", imp=" + improvement + "}";
}
@Override
public int compareTo(Edge e) {
if (value.intValue() > e.value.intValue()) {
return 1;
} else if (value.intValue() < e.value.intValue()) {
return -1;
}
return 0;
}
}