forked from Yehudit-Brickner/OOP2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframeGui.java
403 lines (318 loc) · 13.6 KB
/
frameGui.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import java.io.File;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
public class frameGui extends JFrame implements ActionListener {
algo a1;
panel_GUI panel;
JMenuBar mb;
JMenu vertex,edge,graph;
JMenuItem save,load,isConnected,tsp,center,shortestPathList,shortestPath,connect,removeEdge,edgeSize,addNode,removeNode,nodeSize,makeList;
public frameGui(DirectedWeightedGraphAlgorithms a) {
//public frameGui() {
super();
this.a1= (algo)a;
Graph g=a1.myGraph;
ImageIcon image= new ImageIcon("green fade.png");
this.setIconImage(image.getImage());
this.setResizable(false);
panel = new panel_GUI(g);
this.add(panel);
this.pack();
this.setTitle("Najeeb and Yehudit");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
mb=new JMenuBar();
vertex=new JMenu("vertex");
edge=new JMenu("edge");
graph=new JMenu("graph");
addNode=new JMenuItem("add vertex");
removeNode=new JMenuItem("remove vertex");
nodeSize=new JMenuItem("number of vertexes");
vertex.add(addNode);
vertex.add(removeNode);
vertex.add(nodeSize);
connect=new JMenuItem("add edge");
removeEdge=new JMenuItem("remove edge");
edgeSize =new JMenuItem("number of edges");
edge.add(connect);
edge.add(removeEdge);
edge.add(edgeSize);
isConnected=new JMenuItem("isConnected");
shortestPath=new JMenuItem("shortest Path");
shortestPathList=new JMenuItem("shortest Path list");
center=new JMenuItem("center");
makeList=new JMenuItem("make list");
tsp=new JMenuItem("tsp");
save=new JMenuItem("save");
load=new JMenuItem("load");
graph.add(isConnected);
graph.add(shortestPath);
graph.add(shortestPathList);
graph.add(center);
graph.add(tsp);
graph.add(save);
graph.add(load);
mb.add(vertex);
mb.add(edge);
mb.add(graph);
nodeSize.addActionListener(this);
addNode.addActionListener(this);
removeNode.addActionListener(this);
edgeSize.addActionListener(this);
connect.addActionListener(this);
removeEdge.addActionListener(this);
isConnected.addActionListener(this);
shortestPath.addActionListener(this);
shortestPathList.addActionListener(this);
save.addActionListener(this);
load.addActionListener(this);
center.addActionListener(this);
tsp.addActionListener(this);
this.add(mb);
this.setJMenuBar(mb);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == isConnected) {
boolean b = a1.isConnected();
//System.out.println("is the graph connected? " + b);
if (b) {
JOptionPane.showMessageDialog(null, "the graph is connected");
} else {
JOptionPane.showMessageDialog(null, "the graph isn't connected");
}
}
if (e.getSource() == shortestPath) {
String src = JOptionPane.showInputDialog("please enter the key of the src vertex");
int s = Integer.parseInt(src);
if (this.a1.myGraph.Nodes.get(s) == null) {
JOptionPane.showMessageDialog(null, "error: src vertex doesn't exist");
} else {
String dest = JOptionPane.showInputDialog("please enter the key of the dest vertex");
int d = Integer.parseInt(dest);
if (this.a1.myGraph.Nodes.get(d) == null) {
JOptionPane.showMessageDialog(null, "error: dest vertex doesn't exist");
} else {
double d1 = a1.shortestPathDist(s, d);
JOptionPane.showMessageDialog(null, "the shortest path between vertex "+s+" to vertex "+d+" is: "+d1);
}
}
}
if (e.getSource() == shortestPathList) {
String src = JOptionPane.showInputDialog("please enter the key of the src vertex");
int s = Integer.parseInt(src);
if (this.a1.myGraph.Nodes.get(s) == null) {
JOptionPane.showMessageDialog(null, "error: src vertex doesn't exist");
} else {
String dest = JOptionPane.showInputDialog("please enter the key of the dest vertex");
int d = Integer.parseInt(dest);
if (this.a1.myGraph.Nodes.get(d) == null) {
JOptionPane.showMessageDialog(null, "error: dest vertex doesn't exist");
} else {
List<NodeData> l= a1.shortestPath(s, d);
String str="the shortest path from vertex "+s+" to vertex "+d+" is: "+s;
for (int i=1;i<l.size();i++){
str=str+"->"+l.get(i).getKey();
}
JOptionPane.showMessageDialog(null, str);
}
}
}
if(e.getSource()==center){
NodeData n=a1.center();
if(n!=null) {
// System.out.println("the NodeData in the middle of the graph is the node with key "+ n.getKey());
JOptionPane.showMessageDialog(null,"the vertex wwth the key " +n.getKey()+"is the center" );
}
else{
// System.out.println("this graph doesn't have a center ");
JOptionPane.showMessageDialog(null,"this graph isn't connected, thus it doesn't have a center" );
}
}
if(e.getSource()==tsp){
List <NodeData>l=new ArrayList<NodeData>();
boolean addmore=true;
while(addmore) {
String src = JOptionPane.showInputDialog("please enter the key of a vertex");
int s1 = Integer.parseInt(src);
if (this.a1.myGraph.Nodes.get(s1) == null) {
JOptionPane.showMessageDialog(null, "error: src vertex doesn't exist");
} else {
l.add(this.a1.myGraph.Nodes.get(s1));
// String dest = JOptionPane.showInputDialog("do you want to add more vertexes to the group? if yes return 0, else return 1 ");
//JOptionPane.showMessageDialog(null,"do you want to add more vertexes to the group? " );
String[] responses = {"yes", "no"};
int answer = JOptionPane.showOptionDialog(
null,
"do you want to add more vertexes to the group? ",
"tsp",
JOptionPane.DEFAULT_OPTION,
0,
null,
responses,
responses[0]);
if (answer == 1) {
addmore = false;
}
}
}
List<NodeData> l2=a1.tsp(l);
if(l2.size()>0) {
String str = "the path that contains the list of vertexes is: " + l2.get(0).getKey();
for (int i = 1; i < l2.size(); i++) {
str = str + "->" + l2.get(i).getKey();
}
JOptionPane.showMessageDialog(null, str);
}
else{
String str="there is no path that contains the vertexes [";
for(int i=0;i<l.size()-1;i++){
str=str+l.get(i).getKey() +",";
}
str=str+l.get(l.size()-1).getKey()+"] in this graph ";
JOptionPane.showMessageDialog(null, str);
}
}
if(e.getSource()==save){
JFileChooser f=new JFileChooser();
int ret= f.showDialog(null,null);
File file=f.getSelectedFile();
if(ret==JFileChooser.APPROVE_OPTION){
try {
a1.save(file.getPath());
}
catch (Exception e1){
throw e1;
}
}
}
if(e.getSource()==load){
JFileChooser f=new JFileChooser();
int ret= f.showDialog(null,null);
File file=f.getSelectedFile();
if(ret==JFileChooser.APPROVE_OPTION){
try {
a1.load(file.getPath());
this.setVisible(false);
frameGui fr = new frameGui(this.a1);
}
catch (Exception e1){
throw e1;
}
}
}
if(e.getSource()==addNode) {
String src1 = JOptionPane.showInputDialog("please enter the x coordinates ");
double x = Double.parseDouble(src1);
String src2 = JOptionPane.showInputDialog("please enter the y coordinates ");
double y = Double.parseDouble(src2);
Geo g = new Geo(x, y, 0);
String src3 = JOptionPane.showInputDialog("please enter the Vertex key ");
int key = Integer.parseInt(src3);
if (this.a1.myGraph.Nodes.get(key) != null) {
//String src4 = JOptionPane.showInputDialog("a Vertex with this key exist, if you want to move it return 0, if you wnt to pick a new key return 1 ");
String[] responses = {"yes", "no"};
int answer = JOptionPane.showOptionDialog(
null,
"a vertex with this key exits, do you want to move it? ",
"tsp",
JOptionPane.DEFAULT_OPTION,
0,
null,
responses,
responses[1]);
if (answer == 0) {
Vertex n = new Vertex(key, g);
a1.myGraph.addNode(n);
this.setVisible(false);
frameGui f=new frameGui(this.a1);
}
else {
src3 = JOptionPane.showInputDialog("please enter the Vertex key ");
key = Integer.parseInt(src3);
if (this.a1.myGraph.Nodes.get(key) != null) {
JOptionPane.showMessageDialog(null, "error");
} else {
Vertex n = new Vertex(key, g);
a1.myGraph.addNode(n);
this.setVisible(false);
frameGui f = new frameGui(this.a1);
}
}
} else {
Vertex n = new Vertex(key, g);
a1.myGraph.addNode(n);
this.setVisible(false);
frameGui f=new frameGui(this.a1);
}
}
if(e.getSource()==removeNode){
String src = JOptionPane.showInputDialog("please enter the vertex key you want to remove");
int s=Integer.parseInt(src);
if(this.a1.myGraph.Nodes.get(s)==null) {
JOptionPane.showMessageDialog(null,"error: vertex doesn't exist");
}
else {
a1.myGraph.removeNode(s);
this.setVisible(false);
frameGui f=new frameGui(this.a1);
}
}
if(e.getSource()==nodeSize){
int s=a1.myGraph.nodeSize();
//System.out.println("the number of Vertexes in the graph is " +s)
JOptionPane.showMessageDialog(null,"the number of Vertexes in the graph is " +s);
}
if(e.getSource()==connect){
String src = JOptionPane.showInputDialog("please enter the key of the src vertex");
int s = Integer.parseInt(src);
if (this.a1.myGraph.Nodes.get(s) == null) {
JOptionPane.showMessageDialog(null, "error: src vertex doesn't exist");
} else {
String dest = JOptionPane.showInputDialog("please enter the key of the dest vertex");
int d = Integer.parseInt(dest);
if (this.a1.myGraph.Nodes.get(d) == null) {
JOptionPane.showMessageDialog(null, "error: dest vertex doesn't exist");
} else {
String w1 = JOptionPane.showInputDialog("please enter the weight of the edge ");
double w=Double.parseDouble(w1);
a1.myGraph.connect(s,d,w);
this.setVisible(false);
frameGui f=new frameGui(this.a1);
}
}
}
if(e.getSource()==removeEdge) {
String src = JOptionPane.showInputDialog("please enter the key of the src vertex");
int s=Integer.parseInt(src);
if(this.a1.myGraph.Nodes.get(s)==null) {
JOptionPane.showMessageDialog(null,"error: src vertex doesn't exist");
}
else {
String dest = JOptionPane.showInputDialog("please enter the key of the dest vertex");
int d=Integer.parseInt(dest);
if(this.a1.myGraph.Nodes.get(d)==null) {
JOptionPane.showMessageDialog(null,"error: dest vertex doesn't exist");
}
else{
a1.myGraph.removeEdge(s, d);
this.setVisible(false);
frameGui f=new frameGui(this.a1);
}
}
}
if(e.getSource()==edgeSize){
int s=a1.myGraph.edgeSize();
String out="the number of edges in the graph is " +s;
JOptionPane.showMessageDialog(null,"the number of edges in the graph is " +s);
//System.out.println("the number of edges in the graph is " +s);
}
}
// public static void main(String[] args) {
// frameGui myGui=new frameGui("G1.json");
}