forked from fayecamilleburi/TopologicalSort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTutorial.java
More file actions
482 lines (399 loc) · 16.6 KB
/
Tutorial.java
File metadata and controls
482 lines (399 loc) · 16.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
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
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.swing.*;
public class Tutorial extends JFrame implements ActionListener {
private JLabel labelZero, labelOne, labelTwo, labelThree, labelFour, labelFive, labelSix, match;
private JTextArea resultsArea;
private JButton backButton, submitButton, readyButton, clearButton;
private ArrayList<JPanel> clickedPanels;
private JPanel[] panelsArray = new JPanel[7];
public Tutorial() {
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("Tutorial");
header.setBounds(0, 25, 1280, 30);
header.setForeground(Color.WHITE);
header.setFont(new Font("Arial", Font.BOLD, 25));
header.setHorizontalAlignment(JLabel.CENTER);
panel.add(header);
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);
return panel;
}
public void actionPerformedBack(ActionEvent e) {
if (e.getSource() == backButton) {
dispose();
}
}
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;'>"
+ "This area inscibes the<br>"
+ "problem description.</div></html>";
JLabel content = new JLabel(htmlContent);
content.setBounds(0, 120, 350, 200);
content.setForeground(new Color(0x764B36));
content.setFont(new Font("Arial", Font.ITALIC, 24));
content.setHorizontalAlignment(JLabel.CENTER);
panel.add(content);
String note = "<html><div style='text-align: center;'>"
+ "***<br>"
+ "Click the tiles on the center panel<br>"
+ "according to the order of your choice.<br>"
+ "Once clicked, the tile changes color.<br>"
+ "Proceed with 'Submit' after done choosing.<br>"
+ "On the right panel, your sequence will be<br>"
+ "generated. To check if the sequence is<br>"
+ "applicable based on Topological Sorting,<br>"
+ "“I can...” text will appear. To finish, click<br>"
+ "'We're Ready!'.</div></html>";
JLabel subtext = new JLabel(note);
subtext.setBounds(0, 310, 350, 200);
subtext.setForeground(new Color(0x9B4922));
subtext.setFont(new Font("Arial", Font.ITALIC, 14));
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.setCursor(new Cursor(Cursor.HAND_CURSOR));
panel.setBackground(new Color(0x764B36));
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("A");
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);
panel.setBackground(isPanelClicked(panel) ? new Color(0x9B4922) : new Color(0x764B36));
}
});
labelOne = new JLabel("B");
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("C");
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("D");
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("E");
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("F");
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("G");
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 [ ... ] :");
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.setFont(new Font("Arial", Font.PLAIN, 25));
resultsArea.setForeground(new Color(0x5C3420));
resultsArea.setCaretColor(new Color(0xEFE7DD));
resultsArea.setVisible(false);
panel.add(resultsArea);
panel.add(resultInstruct());
match = new JLabel("[I can ... ]");
match.setBounds(20, 365, 384, 50);
match.setBackground(new Color(0xEFE7DD));
match.setForeground(new Color(0x9B4922));
match.setFont(new Font("Arial", Font.BOLD, 30));
match.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(match);
readyButton = new JButton("We're Ready!");
readyButton.setBounds(290, 460, 120, 30);
readyButton.setBackground(new Color(0x5C3420));
readyButton.setForeground(Color.WHITE);
readyButton.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 12));
readyButton.setFocusable(false);
panel.add(readyButton);
return panel;
}
private JLabel resultInstruct(){
String resultNote= "<html><div style='text-align: center;'>"
+ "This area lists the items down<br>"
+ "chronologically according<br>"
+ "to your sequence.</div></html>";
JLabel resultSample = new JLabel(resultNote);
resultSample.setBounds(20, 70, 384, 275);
resultSample.setBackground(new Color(0xEFE7DD));
resultSample.setFont(new Font("Arial", Font.ITALIC, 24));
resultSample.setForeground(new Color(0x764B36));
return resultSample;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submitButton) {
if (clickedPanels == null || clickedPanels.size() < 7) {
showErrorDialog("Make sure all panels are visited.");
} else {
displayClickedPanels();
resultsArea.setVisible(true);
resultInstruct().setVisible(false);
}
}
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.setVisible(false);
resultInstruct().setVisible(true);
}
}
}
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;
break;
}
}
if (panelIndex != -1) {
JLabel label = (JLabel) panel.getComponent(0);
String panelText = label.getText();
resultsArea.append(panelText + "\n");
}
}
}
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();
}
private void showErrorDialog(String message) {
JOptionPane optionPane = new JOptionPane(message, JOptionPane.ERROR_MESSAGE);
JDialog dialog = optionPane.createDialog("Error");
dialog.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 Tutorial().setVisible(true);
});
}
}