-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphGUI.java
607 lines (521 loc) · 24.8 KB
/
GraphGUI.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
import java.util.*;
import java.io.*;
import static java.awt.Color.red;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class GraphGUI {
public static int Red = 0;
public static int Blue = 0;
public static int Green = 0;
/** The graph to be displayed */
private JGraph graph;
/** Label for the input mode instructions */
private JLabel instr;
/** The input mode */
InputMode mode = InputMode.ADD_NODES;
/** Remembers point where last mousedown event occurred */
Point pointUnderMouse;
/** Remembers position of head node */
Point headUnderMouse = null;
/** Remembers position of tail node */
Point tailUnderMouse = null;
/**
* Schedules a job for the event-dispatching thread creating and showing this
* application's GUI.
*/
public static void main(String[] args) {
final GraphGUI GUI = new GraphGUI();
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
GUI.createAndShowGUI();
}
});
}
/** Sets up the GUI window */
public void createAndShowGUI() {
// Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
// Create and set up the window.
JFrame frame = new JFrame("Graph GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add components
createComponents(frame);
// Display the window.
frame.pack();
frame.setVisible(true);
}
/** Puts content in the GUI window */
public void createComponents(JFrame frame) {
// graph display
Container pane = frame.getContentPane();
pane.setLayout(new FlowLayout());
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
graph = new JGraph();
PointMouseListener pml = new PointMouseListener();
graph.addMouseListener(pml);
graph.addMouseMotionListener(pml);
panel.add(graph);
instr = new JLabel("Click to add new nodes , drag to move.");
panel.add(instr, BorderLayout.NORTH);
pane.add(panel);
// controls
MenuBar menu = new MenuBar();
Menu panel1 = new Menu("Options");
MenuItem setColorButton = new MenuItem("Set Pen Color");
panel1.add(setColorButton);
setColorButton.addActionListener(new setColorListener());
MenuItem addNodeButton = new MenuItem("Add/Move Nodes");
panel1.add(addNodeButton);
addNodeButton.addActionListener(new AddNodeListener());
MenuItem clrNodeButton = new MenuItem("Color Nodes");
panel1.add(clrNodeButton);
clrNodeButton.addActionListener(new ClrNodeListener());
MenuItem rmvNodeButton = new MenuItem("Remove Nodes");
panel1.add(rmvNodeButton);
rmvNodeButton.addActionListener(new RmvNodeListener());
MenuItem addEdgeButton = new MenuItem("Add Edges");
panel1.add(addEdgeButton);
addEdgeButton.addActionListener(new AddEdgeListener());
MenuItem clrEdgeButton = new MenuItem("Color Edge");
panel1.add(clrEdgeButton);
clrEdgeButton.addActionListener(new ClrEdgeListener());
MenuItem rmvEdgeButton = new MenuItem("Remove Edges");
panel1.add(rmvEdgeButton);
rmvEdgeButton.addActionListener(new RmvEdgeListener());
MenuItem resetButton = new MenuItem("Reset");
panel1.add(resetButton);
resetButton.addActionListener(new ResetListener());
MenuItem blossom= new MenuItem("Maximum Matching");
panel1.add(blossom);
blossom.addActionListener(new EdmondBlossomMaxMatchListener());
Menu panel2 = new Menu("File");
MenuItem graphSaveButton = new MenuItem("Save Graph");
panel2.add(graphSaveButton);
graphSaveButton.addActionListener(new graphSaveListener());
MenuItem graphUploadButton = new MenuItem("Open Existing Graph");
panel2.add(graphUploadButton);
graphUploadButton.addActionListener(new graphUploadListener());
menu.add(panel2);
menu.add(panel1);
frame.setMenuBar(menu);
}
/**
* Returns a point found within the drawing radius of the given location, or
* null if none
*
* x the x coordinate of the location y the y coordinate of the location
*
* @return a point from the graph if there is one covering this location, or a
* null reference if not
*/
public Point findNearbyPoint(int x, int y) {
Point pointFound = null;
Graph<DisplayNode, DisplayEdge> g = graph.getGraph();
for (int i = 0; i < g.numNodes(); i++) {
Point p = g.getNode(i).getData().getPosition();
double d = p.distance((double) x, (double) y);
if (d < 20) {
pointFound = p;
}
}
return pointFound;
}
/** Constants for recording the input mode */
enum InputMode {
ADD_NODES, RMV_NODES, CLR_NODES, ADD_EDGES, RMV_EDGES, CLR_EDGES
}
private class graphSaveListener implements ActionListener {
/** Event handler for AddNode button */
public void actionPerformed(ActionEvent e) {
try {
String filename = JOptionPane.showInputDialog("Please input name for graph :(Please give extension as sg) ");
FileWriter fout = new FileWriter(filename);
fout.write(graph.getGraph().numNodes() + "\n");
fout.write(graph.getGraph().numEdges() + "\n");
for (int i = 0; i < graph.getGraph().numNodes(); i++) {
Graph<DisplayNode, DisplayEdge>.Node node = graph.getGraph().getNode(i);
DisplayNode data = node.getData();
Point p = data.getPosition();
Color col = data.getColor();
fout.write((int) p.getX() + "," + (int) p.getY() + "," + data.getLabel() + "," + col.getRed() + "," + col.getGreen() + "," + col.getBlue()
+ "\n");
}
for (int i = 0; i < graph.getGraph().numEdges(); i++) {
Graph<DisplayNode, DisplayEdge>.Edge edge = graph.getGraph().getEdge(i);
Graph<DisplayNode, DisplayEdge>.Node n1 = edge.getHead();
Graph<DisplayNode, DisplayEdge>.Node n2 = edge.getTail();
int head_index = 0, tail_index = 0;
for (int j = 0; j < graph.getGraph().numNodes(); j++) {
if(graph.getGraph().getNode(j) == n1){
head_index = j;
}
else if (graph.getGraph().getNode(j) == n2) {
tail_index = j;
}
}
Color col = edge.getData().getColor();
String label = edge.getData().getLabel();
fout.write(head_index + "," + tail_index + "," + label + "," + col.getRed() + "," + col.getGreen() + "," + col.getBlue() + "\n");
}
fout.close();
} catch(IOException ex){
System.out.println(ex);
}
}
}
private class graphUploadListener implements ActionListener {
/** Event handler for AddNode button */
public void actionPerformed(ActionEvent e) {
try {
String filepath;
for (int i=0; i<graph.getGraph().numNodes(); i++) {
graph.getGraph().removeNode(graph.getGraph().getNode(i));
}
for (int i=0; i<graph.getGraph().numEdges(); i++) {
graph.getGraph().removeEdge(graph.getGraph().getEdge(i));
}
graph.repaint();
for (int i=0; i<graph.getGraph().numNodes(); i++) {
graph.getGraph().removeNode(graph.getGraph().getNode(i));
}
graph.repaint();
//Using JFileChooser for opening File Explorer
JFileChooser fc=new JFileChooser();
int j=fc.showOpenDialog(null);
File f=fc.getSelectedFile();
filepath=f.getPath();
FileReader fr = new FileReader(filepath);
BufferedReader br = new BufferedReader(fr);
int numNodes = Integer.parseInt(br.readLine());
int numEdges = Integer.parseInt(br.readLine());
for(int i = 0; i < numNodes; i++){
String[] arrSplit = br.readLine().split(",");
Point newPoint = new Point(Integer.parseInt(arrSplit[0]), Integer.parseInt(arrSplit[1]));
graph.getGraph().addNode(new DisplayNode(newPoint, arrSplit[2], new Color(Integer.parseInt(arrSplit[3]), Integer.parseInt(arrSplit[4]), Integer.parseInt(arrSplit[5]))));
}
for(int i = 0; i < numEdges; i++){
String[] arrSplit = br.readLine().split(",");
graph.getGraph().addEdge(new DisplayEdge(arrSplit[2], new Color(Integer.parseInt(arrSplit[3]), Integer.parseInt(arrSplit[4]), Integer.parseInt(arrSplit[5])), 0), graph.getGraph().getNode(Integer.parseInt(arrSplit[0])), graph.getGraph().getNode(Integer.parseInt(arrSplit[1])));
}
br.close();
fr.close();
graph.repaint();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private class setColorListener implements ActionListener {
/** Event handler for AddNode button */
public void actionPerformed(ActionEvent e) {
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(0, 2, 2, 2));
JTextField _red = new JTextField(5);
JTextField _blue = new JTextField(5);
JTextField _green = new JTextField(5);
pane.add(new JLabel("R"));
pane.add(_red);
pane.add(new JLabel("G"));
pane.add(_green);
pane.add(new JLabel("B"));
pane.add(_blue);
int option = JOptionPane.showConfirmDialog(graph, pane, "Please fill all the fields", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (option == JOptionPane.YES_OPTION) {
String red = _red.getText();
String green = _green.getText();
String blue = _blue.getText();
try {
Red = Integer.parseInt(red);
Green = Integer.parseInt(green);
Blue = Integer.parseInt(blue);
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
}
}
}
/** Listener for AddNode button */
private class AddNodeListener implements ActionListener {
/** Event handler for AddNode button */
public void actionPerformed(ActionEvent e) {
mode = InputMode.ADD_NODES;
instr.setText("Click to add new node; drag to move");
}
}
/**Listener for Coloring Nodes */
private class ClrNodeListener implements ActionListener {
/** Event handler for ClrNode button */
public void actionPerformed(ActionEvent e) {
mode = InputMode.CLR_NODES;
instr.setText("Click to color existing node");
}
}
/** Listener for RmvNode button */
private class RmvNodeListener implements ActionListener {
/** Event handler for RmvNode button */
public void actionPerformed(ActionEvent e) {
mode = InputMode.RMV_NODES;
instr.setText("Click to remove node");
}
}
/** Listener for AddEdge button */
private class AddEdgeListener implements ActionListener {
/** Event handler for AddEdge button */
public void actionPerformed(ActionEvent e) {
mode = InputMode.ADD_EDGES;
instr.setText("Click on two existing nodes to add a new edge");
}
}
/** Listener for RmvEdge button */
private class RmvEdgeListener implements ActionListener {
/** Event handler for RmvEdge button */
public void actionPerformed(ActionEvent e) {
mode = InputMode.RMV_EDGES;
instr.setText("Click to remove edges");
}
}
private class ClrEdgeListener implements ActionListener {
/** Event handler for RmvEdge button */
public void actionPerformed(ActionEvent e) {
mode = InputMode.CLR_EDGES;
instr.setText("Click to color edges");
}
}
/* Listener for Reset button */
private class ResetListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
instr.setText("Graph has been Reset!");
// resets all the edge colors to original color
for (int i=0; i<graph.getGraph().numNodes(); i++) {
graph.getGraph().removeNode(graph.getGraph().getNode(i));
}
for (int i=0; i<graph.getGraph().numEdges(); i++) {
graph.getGraph().removeEdge(graph.getGraph().getEdge(i));
}
graph.repaint();
for (int i=0; i<graph.getGraph().numNodes(); i++) {
graph.getGraph().removeNode(graph.getGraph().getNode(i));
}
graph.repaint();
}
}
private class EdmondBlossomMaxMatchListener implements ActionListener{
public void actionPerformed(ActionEvent e)
{
String filepath;
JFileChooser fc=new JFileChooser();
int j=fc.showOpenDialog(null);
File f=fc.getSelectedFile();
filepath=f.getPath();
EdmondBlossomMaxMatch.main(filepath);
}
}
/** Mouse listener for Pointgraph element */
private class PointMouseListener extends MouseAdapter
implements MouseMotionListener {
/** Responds to click event depending on mode */
public void mouseClicked(MouseEvent e) {
int mouseX = e.getX();
int mouseY = e.getY();
Point nearBy = findNearbyPoint(mouseX, mouseY);
switch (mode) {
case ADD_NODES:
// If the click is not on top of an existing point, create a new one and add it to the graph.
// Otherwise, emit a beep, as shown below:
if(nearBy == null) {
Point newPoint = new Point(mouseX, mouseY);
String data = JOptionPane.showInputDialog("Please input name for node(In Integer): ");
if (data != null) {
graph.getGraph().addNode(new DisplayNode(newPoint, data, new Color(Red, Blue, Green)));
}
} else {
Toolkit.getDefaultToolkit().beep();
}
break;
case RMV_NODES:
// If the click is on top of an existing node, remove it from the graph's list of nodes.
// Otherwise, emit a beep.
if(nearBy != null) {
for (int i=0; i<graph.getGraph().numNodes(); i++) {
if (graph.getGraph().getNode(i).getData().getPosition() == nearBy) {
graph.getGraph().removeNode(graph.getGraph().getNode(i));
}
}
} else {
Toolkit.getDefaultToolkit().beep();
}
break;
case CLR_NODES:
// If the click is on top of an existing node, color.
// Otherwise, emit a beep.
if(nearBy != null) {
int _Red = 0;
int _Blue = 0;
int _Green = 0;
for (int i=0; i<graph.getGraph().numNodes(); i++) {
if (graph.getGraph().getNode(i).getData().getPosition() == nearBy) {
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(0, 2, 2, 2));
JTextField _red = new JTextField(5);
JTextField _blue = new JTextField(5);
JTextField _green = new JTextField(5);
pane.add(new JLabel("R"));
pane.add(_red);
pane.add(new JLabel("G"));
pane.add(_green);
pane.add(new JLabel("B"));
pane.add(_blue);
int option = JOptionPane.showConfirmDialog(graph, pane, "Please fill all the fields", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (option == JOptionPane.YES_OPTION) {
String red = _red.getText();
String blue = _blue.getText();
String green = _green.getText();
try {
_Red = Integer.parseInt(red);
_Blue = Integer.parseInt(blue);
_Green = Integer.parseInt(green);
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
graph.getGraph().getNode(i).getData().setColor(new Color(_Red, _Green, _Blue));
}
}
}
} else {
Toolkit.getDefaultToolkit().beep();
}
break;
case ADD_EDGES:
// If two nodes are clicked, add edge in between them
if (nearBy != null) {
if(headUnderMouse == null) {
headUnderMouse = nearBy;
} else if ((headUnderMouse != null && tailUnderMouse == null) && (!nearBy.equals(headUnderMouse))) {
tailUnderMouse = nearBy;
Graph<DisplayNode,DisplayEdge>.Node head = null;
Graph<DisplayNode,DisplayEdge>.Node tail = null;
for (int i=0; i<graph.getGraph().numNodes(); i++) {
if (graph.getGraph().getNode(i).getData().getPosition() == headUnderMouse) {
head = graph.getGraph().getNode(i);
} else if (graph.getGraph().getNode(i).getData().getPosition() == tailUnderMouse) {
tail = graph.getGraph().getNode(i);
}
}
String data = JOptionPane.showInputDialog("Please input name for edge: ");
// if (data != null && data.length() != 0) {
// try {
// int weight = Integer.parseInt(data);
if(head != null && tail != null)
graph.getGraph().addEdge(new DisplayEdge(data, new Color(Red, Green, Blue), 0), head, tail);
// } catch (Exception f) {
// JOptionPane.showMessageDialog(null,"Please enter a valid number for edge cost","ERROR!",JOptionPane.WARNING_MESSAGE);
// }
//// } else {
// JOptionPane.showMessageDialog(null,"Please enter a valid number for edge cost","ERROR!",JOptionPane.WARNING_MESSAGE);
// }
headUnderMouse = null;
tailUnderMouse = null;
}
}
break;
case RMV_EDGES:
// If two nodes are clicked, remove edge in between them
if (nearBy != null) {
if(headUnderMouse == null) {
headUnderMouse = nearBy;
} else if (headUnderMouse != null && tailUnderMouse == null) {
tailUnderMouse = nearBy;
Graph<DisplayNode,DisplayEdge>.Node head = null;
Graph<DisplayNode,DisplayEdge>.Node tail = null;
for (int i=0; i<graph.getGraph().numNodes(); i++) {
if (graph.getGraph().getNode(i).getData().getPosition() == headUnderMouse) {
head = graph.getGraph().getNode(i);
} else if (graph.getGraph().getNode(i).getData().getPosition() == tailUnderMouse) {
tail = graph.getGraph().getNode(i);
}
}
graph.getGraph().removeEdge(head, tail);
headUnderMouse = null;
tailUnderMouse = null;
}
}
break;
case CLR_EDGES:
// If two nodes are clicked, remove edge in between them
if (nearBy != null) {
int _Red = 0;
int _Blue = 0;
int _Green = 0;
if(headUnderMouse == null) {
headUnderMouse = nearBy;
} else if (headUnderMouse != null && tailUnderMouse == null) {
tailUnderMouse = nearBy;
Graph<DisplayNode,DisplayEdge>.Node head = null;
Graph<DisplayNode,DisplayEdge>.Node tail = null;
for (int i=0; i<graph.getGraph().numNodes(); i++) {
if (graph.getGraph().getNode(i).getData().getPosition() == headUnderMouse) {
head = graph.getGraph().getNode(i);
} else if (graph.getGraph().getNode(i).getData().getPosition() == tailUnderMouse) {
tail = graph.getGraph().getNode(i);
}
}
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(0, 2, 2, 2));
JTextField _red = new JTextField(5);
JTextField _blue = new JTextField(5);
JTextField _green = new JTextField(5);
pane.add(new JLabel("R"));
pane.add(_red);
pane.add(new JLabel("B"));
pane.add(_blue);
pane.add(new JLabel("G"));
pane.add(_green);
int option = JOptionPane.showConfirmDialog(graph, pane, "Please fill all the fields", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (option == JOptionPane.YES_OPTION) {
String red = _red.getText();
String blue = _blue.getText();
String green = _green.getText();
try {
_Red = Integer.parseInt(red);
_Blue = Integer.parseInt(blue);
_Green = Integer.parseInt(green);
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
graph.getGraph().getEdgeRef(head, tail).getData().setColor(new Color(_Red, _Blue, _Green));
}
}
}
break;
}
graph.repaint();
}
/** Records point under mousedown event in anticipation of possible drag */
public void mousePressed(MouseEvent e) {
// Record point under mouse, if any
pointUnderMouse = findNearbyPoint(e.getX(), e.getY());
}
/** Responds to mouseup event */
public void mouseReleased(MouseEvent e) {
// Clear record of point under mouse, if any
pointUnderMouse = null;
}
/** Responds to mouse drag event */
public void mouseDragged(MouseEvent e) {
// If mode allows point motion, and there is a point under the mouse,
// then change its coordinates to the current mouse coordinates & update display
if (mode == InputMode.ADD_NODES && pointUnderMouse != null) {
pointUnderMouse.setLocation(e.getX(), e.getY());
graph.repaint();
}
}
// Empty but necessary to comply with MouseMotionListener interface.
public void mouseMoved(MouseEvent e) {}
}
}