forked from Yehudit-Brickner/OOP2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanel_GUI.java
200 lines (155 loc) · 6.49 KB
/
panel_GUI.java
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
import javax.swing.*;
import java.awt.*;
import java.util.Iterator;
public class panel_GUI extends JPanel {
Graph graph1;
panel_GUI(Graph g) {
this.graph1=g;
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int screenHeight = screensize.height;
int screenWidth = screensize.width;
this.setPreferredSize(new Dimension(screenWidth / 2, screenHeight / 2));
}
public void paint(Graphics g) {
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int screenHeight = screensize.height/2;
int screenWidth =screensize.width/2;
Graphics2D g2D = (Graphics2D) g;
g2D.setPaint(Color.BLACK);
g2D.fillRect(0,0,screenWidth,screenHeight);
double [] arr=find_mins_maxs(this.graph1);
double x_differ=arr[1]-arr[0];
double y_differ=arr[3]-arr[2];
int r1=8;
if(graph1.nodeSize()<500){
Iterator<NodeData> iter1 = graph1.nodeIter();
while (iter1.hasNext()) {
g2D.setPaint(Color.RED);
Vertex n = (Vertex) iter1.next();
double xx=n.getLocation().x();
int x=change_cord_x(xx,x_differ,screenWidth,arr[0]);
double yy=n.getLocation().y();
int y=change_cord_y(yy,y_differ,screenHeight,arr[2]);
g2D.fillOval(x-r1, y-r1, r1*2, r1*2);
g2D.setFont(new Font("Ariel", Font.BOLD, 10));
}
g2D.setPaint(Color.BLUE);
int r2=1;
g2D.setStroke(new BasicStroke(2*r2));
Iterator<EdgeData> iter2 = graph1.edgeIter();
while(iter2.hasNext()) {
Edge e = (Edge) iter2.next();
g2D.setPaint(Color.BLUE);
double xx1 = graph1.Nodes.get(e.getSrc()).getLocation().x();
int x1 = change_cord_x(xx1, x_differ, screenWidth, arr[0]);
double yy1 = graph1.Nodes.get(e.getSrc()).getLocation().y();
int y1 = change_cord_y(yy1, y_differ, screenHeight, arr[2]);
double xx2 = graph1.Nodes.get(e.getDest()).getLocation().x();
int x2 = change_cord_x(xx2, x_differ, screenWidth, arr[0]);
double yy2 = graph1.Nodes.get(e.getDest()).getLocation().y();
int y2 = change_cord_y(yy2, y_differ, screenHeight, arr[2]);
g2D.setPaint(Color.blue);
drawArrowLine(g2D, x1, y1, x2, y2, 6, 6);
}
Iterator<NodeData> iter3 = graph1.nodeIter();
while (iter3.hasNext()) {
Vertex n = (Vertex) iter3.next();
double xx = n.getLocation().x();
int x = change_cord_x(xx, x_differ, screenWidth, arr[0]);
double yy = n.getLocation().y();
int y = change_cord_y(yy, y_differ, screenHeight, arr[2]);
g2D.setFont(new Font("Ariel", Font.BOLD, 11));
g2D.setPaint(Color.green);
g2D.drawString("key: " + n.getKey(), x + 7, y + 7);
}
}
else{
Iterator<NodeData> iter1 = graph1.nodeIter();
while (iter1.hasNext()) {
g2D.setPaint(Color.RED);
Vertex n = (Vertex) iter1.next();
double xx=n.getLocation().x();
int x=change_cord_x(xx,x_differ,screenWidth,arr[0]);
double yy=n.getLocation().y();
int y=change_cord_y(yy,y_differ,screenHeight,arr[2]);
g2D.fillOval(x-r1, y-r1, r1*2, r1*2);
g2D.setFont(new Font("Ariel", Font.BOLD, 10));
g2D.setPaint(Color.green);
g2D.drawString("key: "+ n.getKey() , x+7,y+7 );
}
g2D.setPaint(Color.BLUE);
int r2=1;
g2D.setStroke(new BasicStroke(2*r2));
Iterator<EdgeData> iter2 = graph1.edgeIter();
while(iter2.hasNext()) {
Edge e = (Edge) iter2.next();
g2D.setPaint(Color.BLUE);
double xx1 = graph1.Nodes.get(e.getSrc()).getLocation().x();
int x1 = change_cord_x(xx1, x_differ, screenWidth, arr[0]);
double yy1 = graph1.Nodes.get(e.getSrc()).getLocation().y();
int y1 = change_cord_y(yy1, y_differ, screenHeight, arr[2]);
double xx2 = graph1.Nodes.get(e.getDest()).getLocation().x();
int x2 = change_cord_x(xx2, x_differ, screenWidth, arr[0]);
double yy2 = graph1.Nodes.get(e.getDest()).getLocation().y();
int y2 = change_cord_y(yy2, y_differ, screenHeight, arr[2]);
g2D.setPaint(Color.blue);
drawArrowLine(g2D, x1, y1, x2, y2, 6, 6);
}
}
}
public int change_cord_x(double x,double x_differ,int screenWidth,double minx){
int x_cor=(int)(((x-minx)/x_differ)*(screenWidth-100));
x_cor=x_cor+35;
return x_cor;
}
public int change_cord_y(double y,double y_differ,int screenHeight,double miny){
int y_cor=(int)(((y-miny)/y_differ)*(screenHeight-100));
y_cor=y_cor+35;
return y_cor;
}
public double[] find_mins_maxs(Graph g) {
double minx = Double.MAX_VALUE;
double maxx = Double.MIN_VALUE;
double miny = Double.MAX_VALUE;
double maxy = Double.MIN_VALUE;
double x;
double y;
Iterator<NodeData> iter1 = graph1.nodeIter();
while (iter1.hasNext()){
Vertex n=(Vertex)iter1.next();
x=n.getLocation().x();
y=n.getLocation().y();
if(x<minx){
minx=x;
}
if(x>maxx){
maxx=x;
}
if(y<miny){
miny=y;
}
if(y>maxy){
maxy=y;
}
}
double []arr={minx,maxx,miny,maxy};
return arr;
}
private void drawArrowLine(Graphics g2D, int x1, int y1, int x2, int y2, int d, int h) {
// https://stackoverflow.com/questions/2027613/how-to-draw-a-directed-arrow-line-in-java
int dx = x2 - x1, dy = y2 - y1;
double D = Math.sqrt(dx*dx + dy*dy);
double xm = D - d, xn = xm, ym = h, yn = -h, x;
double sin = dy / D, cos = dx / D;
x = xm*cos - ym*sin + x1;
ym = xm*sin + ym*cos + y1;
xm = x;
x = xn*cos - yn*sin + x1;
yn = xn*sin + yn*cos + y1;
xn = x;
int[] xpoints = {x2, (int) xm, (int) xn};
int[] ypoints = {y2, (int) ym, (int) yn};
g2D.drawLine(x1, y1, x2, y2);
g2D.fillPolygon(xpoints, ypoints, 3);
}
}