forked from fayecamilleburi/TopologicalSort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS_UI.java
More file actions
550 lines (459 loc) · 18.3 KB
/
DFS_UI.java
File metadata and controls
550 lines (459 loc) · 18.3 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
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
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.swing.*;
public class DFS_UI extends JFrame implements ActionListener {
private JLabel labelZero, labelOne, labelTwo, labelThree, labelFour, labelFive, labelSix, matchArea;
private JTextArea resultsArea;
private JButton backButton, exitButton, submitButton, readyButton, clearButton;
private ArrayList<JPanel> clickedPanels;
private JPanel[] panelsArray = new JPanel[7];
private LinkedList<Integer>order = new LinkedList<>();
public DFS_UI() {
initComponents();
clickedPanels = new ArrayList<>();
}
private void initComponents() {
setTitle("GRWM");
setSize(new Dimension(1280, 720));
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(mainPanel());
}
public JPanel mainPanel() {
JPanel panel = new JPanel(null);
panel.setBounds(0, 0, 1280, 720);
panel.setBackground(new Color(0x5C3420));
panel.add(headerPanel());
panel.add(contentPanel());
return panel;
}
public JPanel headerPanel() {
JPanel panel = new JPanel(null);
panel.setSize(1280, 60);
panel.setBackground(new Color(0x5C3420));
JLabel header = new JLabel("Preparing for School");
header.setBounds(0, 15, 1280, 30);
header.setForeground(Color.WHITE);
header.setFont(new Font("Arial", Font.BOLD, 25));
header.setHorizontalAlignment(JLabel.CENTER);
panel.add(header);
JLabel subtext = new JLabel("using Depth-First Search");
subtext.setBounds(0, 43, 1280, 20);
subtext.setForeground(Color.WHITE);
subtext.setFont(new Font("Arial", Font.ITALIC, 12));
subtext.setHorizontalAlignment(JLabel.CENTER);
panel.add(subtext);
backButton = new JButton("Back");
backButton.setBounds(20, 20, 100, 40);
backButton.setBackground(new Color(0x9B4922));
backButton.setForeground(Color.WHITE);
backButton.addActionListener(this::actionPerformedBack);
backButton.setFocusable(false);
panel.add(backButton);
exitButton = new JButton("Exit");
exitButton.setBounds(1150, 20, 100, 40);
exitButton.setBackground(new Color(0x9B4922));
exitButton.setForeground(Color.WHITE);
exitButton.addActionListener(this::actionPerformedExit);
exitButton.setFocusable(false);
panel.add(exitButton);
return panel;
}
public void actionPerformedBack(ActionEvent e) {
if (e.getSource() == backButton) {
dispose();
SwingUtilities.invokeLater(() -> {
new Choices().setVisible(true);
});
}
}
public void actionPerformedExit(ActionEvent e) {
if (e.getSource() == exitButton) {
SwingUtilities.invokeLater(() -> {
new Credits().setVisible(true);
});
}
}
public JPanel contentPanel() {
JPanel panel = new JPanel(null);
panel.setBounds(0, 80, 1280, 640);
panel.setBackground(new Color(0xEFE7DD));
panel.add(description());
panel.add(itemPanel());
panel.add(resultsPanel());
return panel;
}
public JPanel description() {
JPanel panel = new JPanel(null);
panel.setBounds(10, 10, 350, 580);
panel.setBackground(new Color(0xEFE7DD));
JLabel quote = new JLabel("''");
quote.setBounds(0, 50, 350, 120);
quote.setForeground(new Color(0x764B36));
quote.setFont(new Font("Arial", Font.BOLD, 100));
quote.setHorizontalAlignment(JLabel.CENTER);
panel.add(quote);
String htmlContent = "<html><div style='text-align: center;'>"
+ "Sun shines and alarm rings...<br>"
+ "Oh, it's my first day in college!<br>"
+ "I wonder, what to do first?<br>"
+ "I need your help to complete<br>"
+ "my morning routine so I can<br>"
+ "come fresh and ready for my<br>"
+ "school activities.</div></html>";
JLabel content = new JLabel(htmlContent);
content.setBounds(0, 80, 350, 300);
content.setForeground(new Color(0x764B36));
content.setFont(new Font("Arial", Font.PLAIN, 20));
content.setHorizontalAlignment(JLabel.CENTER);
panel.add(content);
String note = "<html><div style='text-align: center;'>"
+ "***<br>"
+ "Click the tiles on the center panel according to the<br>"
+ "order of your choice. Check if the sequence is<br>"
+ "applicable.</div></html>";
JLabel subtext = new JLabel(note);
subtext.setBounds(0, 410, 350, 100);
subtext.setForeground(new Color(0x9B4922));
subtext.setFont(new Font("Arial", Font.ITALIC, 12));
subtext.setHorizontalAlignment(JLabel.CENTER);
panel.add(subtext);
return panel;
}
public JPanel itemPanel() {
JPanel panel = new JPanel(null);
panel.setBounds(360, 10, 470, 580);
panel.setBackground(new Color(0xEFE7DD));
panelsArray[0] = nodeZero();
panel.add(panelsArray[0]);
panelsArray[1] = nodeOne();
panel.add(panelsArray[1]);
panelsArray[2] = nodeTwo();
panel.add(panelsArray[2]);
panelsArray[3] = nodeThree();
panel.add(panelsArray[3]);
panelsArray[4] = nodeFour();
panel.add(panelsArray[4]);
panelsArray[5] = nodeFive();
panel.add(panelsArray[5]);
panelsArray[6] = nodeSix();
panel.add(panelsArray[6]);
clearButton = new JButton("Clear");
clearButton.setBounds(250, 460, 100, 30);
clearButton.setBackground(new Color(0x5C3420));
clearButton.setForeground(Color.WHITE);
clearButton.setFont(new Font("Arial", Font.BOLD, 12));
clearButton.setFocusable(false);
clearButton.addActionListener(this);
panel.add(clearButton);
submitButton = new JButton("Submit");
submitButton.setBounds(360, 460, 100, 30);
submitButton.setBackground(new Color(0x5C3420));
submitButton.setForeground(Color.WHITE);
submitButton.setFont(new Font("Arial", Font.BOLD, 12));
submitButton.setFocusable(false);
submitButton.addActionListener(this);
panel.add(submitButton);
return panel;
}
public JPanel nodeZero() {
JPanel panel = new JPanel(null);
panel.setBounds(10, 50, 450, 90);
panel.setBackground(new Color(0x764B36));
panel.setCursor(new Cursor(Cursor.HAND_CURSOR));
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
togglePanelState(panel);
panel.setBackground(isPanelClicked(panel) ? new Color(0x9B4922) : new Color(0x764B36));
}
});
labelZero = new JLabel("Fix the bed");
labelZero.setBounds(0, 0, 450, 90);
labelZero.setForeground(Color.WHITE);
labelZero.setFont(new Font("Arial", Font.PLAIN, 15));
labelZero.setHorizontalAlignment(JLabel.CENTER);
panel.add(labelZero);
return panel;
}
public JPanel nodeOne() {
JPanel panel = new JPanel(null);
panel.setBounds(10, 150, 143, 90);
panel.setBackground(new Color(0x764B36));
panel.setCursor(new Cursor(Cursor.HAND_CURSOR));
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
togglePanelState(panel); // Toggle panel state (add/remove from ArrayList)
panel.setBackground(isPanelClicked(panel) ? new Color(0x9B4922) : new Color(0x764B36));
}
});
labelOne = new JLabel("Eat breakfast");
labelOne.setBounds(0, 0, 143, 90);
labelOne.setForeground(Color.WHITE);
labelOne.setFont(new Font("Arial", Font.PLAIN, 15));
labelOne.setHorizontalAlignment(JLabel.CENTER);
panel.add(labelOne);
return panel;
}
public JPanel nodeTwo() {
JPanel panel = new JPanel(null);
panel.setBounds(163, 150, 144, 90);
panel.setBackground(new Color(0x764B36));
panel.setCursor(new Cursor(Cursor.HAND_CURSOR));
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
togglePanelState(panel);
panel.setBackground(isPanelClicked(panel) ? new Color(0x9B4922) : new Color(0x764B36));
}
});
labelTwo = new JLabel("Take a bath");
labelTwo.setBounds(0, 0, 143, 90);
labelTwo.setForeground(Color.WHITE);
labelTwo.setFont(new Font("Arial", Font.PLAIN, 15));
labelTwo.setHorizontalAlignment(JLabel.CENTER);
panel.add(labelTwo);
return panel;
}
public JPanel nodeThree() {
JPanel panel = new JPanel(null);
panel.setBounds(317, 150, 143, 90);
panel.setBackground(new Color(0x764B36));
panel.setCursor(new Cursor(Cursor.HAND_CURSOR));
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
togglePanelState(panel);
panel.setBackground(isPanelClicked(panel) ? new Color(0x9B4922) : new Color(0x764B36));
}
});
labelThree = new JLabel("Prepare things");
labelThree.setBounds(0, 0, 143, 90);
labelThree.setForeground(Color.WHITE);
labelThree.setFont(new Font("Arial", Font.PLAIN, 15));
labelThree.setHorizontalAlignment(JLabel.CENTER);
panel.add(labelThree);
return panel;
}
public JPanel nodeFour() {
JPanel panel = new JPanel(null);
panel.setBounds(10, 250, 220, 90);
panel.setBackground(new Color(0x764B36));
panel.setCursor(new Cursor(Cursor.HAND_CURSOR));
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
togglePanelState(panel);
panel.setBackground(isPanelClicked(panel) ? new Color(0x9B4922) : new Color(0x764B36));
}
});
labelFour = new JLabel("Brush teeth");
labelFour.setBounds(0, 0, 220, 90);
labelFour.setForeground(Color.WHITE);
labelFour.setFont(new Font("Arial", Font.PLAIN, 15));
labelFour.setHorizontalAlignment(JLabel.CENTER);
panel.add(labelFour);
return panel;
}
public JPanel nodeFive() {
JPanel panel = new JPanel(null);
panel.setBounds(240, 250, 220, 90);
panel.setBackground(new Color(0x764B36));
panel.setCursor(new Cursor(Cursor.HAND_CURSOR));
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
togglePanelState(panel);
panel.setBackground(isPanelClicked(panel) ? new Color(0x9B4922) : new Color(0x764B36));
}
});
labelFive = new JLabel("Put on uniform");
labelFive.setBounds(0, 0, 220, 90);
labelFive.setForeground(Color.WHITE);
labelFive.setFont(new Font("Arial", Font.PLAIN, 15));
labelFive.setHorizontalAlignment(JLabel.CENTER);
panel.add(labelFive);
return panel;
}
public JPanel nodeSix() {
JPanel panel = new JPanel(null);
panel.setBounds(10, 350, 450, 90);
panel.setBackground(new Color(0x764B36));
panel.setCursor(new Cursor(Cursor.HAND_CURSOR));
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
togglePanelState(panel);
panel.setBackground(isPanelClicked(panel) ? new Color(0x9B4922) : new Color(0x764B36));
}
});
labelSix = new JLabel("Go to school");
labelSix.setBounds(0, 0, 450, 90);
labelSix.setForeground(Color.WHITE);
labelSix.setFont(new Font("Arial", Font.PLAIN, 15));
labelSix.setHorizontalAlignment(JLabel.CENTER);
panel.add(labelSix);
return panel;
}
public JPanel resultsPanel() {
JPanel panel = new JPanel(null);
panel.setBounds(830, 10, 424, 580);
panel.setBackground(new Color(0xEFE7DD));
JLabel subheading = new JLabel("My routine:");
subheading.setBounds(20, 50, 424, 25);
subheading.setForeground(new Color(0x764B36));
subheading.setFont(new Font("Arial", Font.BOLD, 25));
panel.add(subheading);
resultsArea = new JTextArea();
resultsArea.setEditable(false);
resultsArea.setBounds(20, 85, 384, 275);
resultsArea.setBackground(new Color(0xEFE7DD));
resultsArea.setCaretColor(new Color(0xEFE7DD));
panel.add(resultsArea);
matchArea = new JLabel();
matchArea.setBounds(20, 355, 384, 50);
matchArea.setBackground(new Color(0xEFE7DD));
matchArea.setFont(new Font("Arial", Font.BOLD, 24));
matchArea.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(matchArea);
readyButton = new JButton("We're ready!");
readyButton.setBounds(300, 460, 120, 30);
readyButton.setBackground(new Color(0x5C3420));
readyButton.setForeground(Color.WHITE);
readyButton.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 12));
readyButton.addActionListener(this::actionPerformedReady);
readyButton.setFocusable(false);
panel.add(readyButton);
return panel;
}
public static String linkedListToString(LinkedList<Integer> linkedList) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < linkedList.size(); i++) {
sb.append(linkedList.get(i));
if (i < linkedList.size() - 1) {
sb.append(" ");
}
}
return sb.toString();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submitButton) {
if (clickedPanels == null || clickedPanels.size() < 7) {
showErrorDialog("Make sure all panels are visited.");
} else {
displayClickedPanels();
orderChecking();
}
}
if (e.getSource() == clearButton) {
if (clickedPanels.isEmpty()) {
showErrorDialog("You haven't clicked panels!");
} else {
clickedPanels.clear();
for (JPanel panel : panelsArray) {
panel.setBackground(new Color(0x764B36));
}
resultsArea.setText("");
matchArea.setText("");
order.clear();
}
}
}
private void displayClickedPanels() {
resultsArea.setText("");
resultsArea.setFont(new Font("Arial", Font.PLAIN, 25));
resultsArea.setForeground(new Color(0x5C3420));
for (int i = 0; i < clickedPanels.size(); i++) {
JPanel panel = clickedPanels.get(i);
int panelIndex = -1;
for (int j = 0; j < panelsArray.length; j++) {
if (panel == panelsArray[j]) {
panelIndex = j;
order.add(j);
break;
}
}
if (panelIndex != -1) {
JLabel label = (JLabel) panel.getComponent(0);
String panelText = label.getText();
resultsArea.append(panelText + "\n");
}
}
}
private void orderChecking(){
String result = linkedListToString(order);
TopologicalSort_DFS graph = new TopologicalSort_DFS(7);
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(0, 3);
graph.addEdge(1, 4);
graph.addEdge(2, 5);
graph.addEdge(3, 6);
graph.addEdge(4, 6);
graph.addEdge(5, 6);
List<List<Integer>> allSorts = graph.topologicalSortDFS();
boolean foundMatch = false;
matchArea.setText("\n");
matchArea.setFont(new Font("Arial", Font.BOLD, 30));
matchArea.setForeground(new Color(0x9B4922));
for (List<Integer> sort : allSorts) {
StringBuilder sb = new StringBuilder();
for (int vertex : sort) {
sb.append(vertex).append(" ");
}
String currentSortString = sb.toString().trim();
if (result.equals(currentSortString)) {
matchArea.setText("I can do that!");
foundMatch = true;
order.clear();
break;
}
}
if (!foundMatch) {
matchArea.setText("Oops!");
order.clear();
}
}
private void showErrorDialog(String message) {
JOptionPane optionPane = new JOptionPane(message, JOptionPane.ERROR_MESSAGE);
JDialog dialog = optionPane.createDialog("Error");
dialog.setVisible(true);
}
public void actionPerformedReady(ActionEvent e) {
if (e.getSource() == readyButton) {
if (clickedPanels.isEmpty()) {
showErrorDialog("Please make your choices first.");
}
else if(matchArea.getText().equals("I can do that!")){
SwingUtilities.invokeLater(() -> {
new Success().setVisible(true);
});
}
else if(matchArea.getText().equals("Oops!")){
SwingUtilities.invokeLater(() -> {
new NoMatch().setVisible(true);
});
}
}
}
private void togglePanelState(JPanel panel) {
if (clickedPanels.contains(panel)) {
clickedPanels.remove(panel);
} else {
clickedPanels.add(panel);
}
}
private boolean isPanelClicked(JPanel panel) {
return clickedPanels.contains(panel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new DFS_UI().setVisible(true);
});
}
}