-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
1188 lines (987 loc) · 41.9 KB
/
GUI.java
File metadata and controls
1188 lines (987 loc) · 41.9 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
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package officialInternalAssessment;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
// add navigation to all sources of the program
//add functionalities for fractions and percentage questions
// re-factor the program to also make use of google fire-base
public class GUI extends JFrame implements ActionListener{
private CardLayout cardLayout;
private JPanel cardPanel;
private Timer timer;
JLabel label;
JTextField text;
//General Pages
private static final String LOGIN = "Login Portal";
private static final String CREATION = "User Creation";
private static final String STUDENTDASH = "Student Dashboard";
private static final String LEARNMATH = "Learn Math";
private static final String PRACTICEMATH = "Practice Math";
private static final String PRACTICEREPORT = "Practice Report";
private static final String TEACHERDASH = "Teacher Dashboard";
private static final String STUDENTPROFILE = "Student Profile";
//for login info
private JTextField usernameFieldLogin;
private JPasswordField pFieldLogin;
private JLabel errorLabelLogin;
// for resetting login
private void resetUserLogin() {
usernameFieldLogin.setText("");
pFieldLogin.setText("");
errorLabelLogin.setText("");
}
// for user creation
private JTextField usernameFieldCreation;
private JPasswordField pFieldCreation;
private JPasswordField cpFieldCreation;
private JCheckBox studentBox;
private JCheckBox teacherBox;
private JLabel errorLabelCreation;
// Reset method for user creation
private void resetUserCreationForm() {
usernameFieldCreation.setText("");
pFieldCreation.setText("");
cpFieldCreation.setText("");
studentBox.setSelected(false);
teacherBox.setSelected(false);
errorLabelCreation.setText("");
}
private User currentUser = null; // <-- track logged-in user
private JPanel practiceReportPanel = null; // <-- keep reference to report panel
public void stopPracticeTimer() {
if (timer.isRunning()) {
timer.stop();
}
}
// Add this method inside GUI class
public void startPracticeTimer() {
// If you declared 'timer' as a class-level variable
if (timer != null && !timer.isRunning()) {
timer.start();
}
}
public GUI() {
super("Math Study");
// Get screen size
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setSize(screenSize.width, screenSize.height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create card panel
cardLayout = new CardLayout();
cardPanel = new JPanel(cardLayout);
//Create general pages
cardPanel.add(createLoginPortal(), LOGIN);
cardPanel.add(createUserCreation(), CREATION);
cardPanel.add(createStudentDashboard(), STUDENTDASH);
cardPanel.add(createLearnMath(), LEARNMATH);
cardPanel.add(createPracticeMath(), PRACTICEMATH);
cardPanel.add(createTeacherDashboard(), TEACHERDASH);
cardPanel.add(new JPanel(), STUDENTPROFILE); // cannot call student profile straight away because current user has not been established
practiceReportPanel = createPracticeReport();
cardPanel.add(practiceReportPanel, PRACTICEREPORT);
//Create topic pages
add(cardPanel);
setVisible(true);
cardLayout.show(cardPanel, LOGIN);
// use cardLayout.show to jump directly to a page but the default should always be to LOGIN
// cardLayout.show(cardPanel, LEARNMATH);
// cardLayout.show(cardPanel, PRACTICEMATH);
}
// login portal done
private JPanel createLoginPortal() {
// Outer panel with GridBagLayout to center everything
JPanel outerPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
// Inner panel with BoxLayout for vertical stacking
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
// Title
JLabel title = new JLabel("Login Site", SwingConstants.CENTER);
title.setFont(new Font("Arial", Font.BOLD, 36));
title.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(title);
panel.add(Box.createVerticalStrut(10));
// Username row
JPanel userPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 12, 0));
JLabel userLabel = new JLabel("Username:");
usernameFieldLogin = new JTextField(15);
userPanel.add(userLabel);
userPanel.add(usernameFieldLogin);
panel.add(userPanel);
panel.add(Box.createVerticalStrut(15));
// Password row
JPanel passPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 12, 0));
JLabel passLabel = new JLabel("Password:");
pFieldLogin = new JPasswordField(15);
passPanel.add(passLabel);
passPanel.add(pFieldLogin);
panel.add(passPanel);
panel.add(Box.createVerticalStrut(15));
// Login button
JButton loginButton = new JButton(" Login ");
loginButton.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(loginButton);
panel.add(Box.createVerticalStrut(5));
// Create User Button
JButton createUserButton = new JButton();
createUserButton.setText("<html><u> Create new user? </u></html>");
createUserButton.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(createUserButton);
createUserButton.setBorderPainted(false);
//Dimension controls
Dimension fieldSize = new Dimension(450, 35);
usernameFieldLogin.setSize(fieldSize);
pFieldLogin.setSize(fieldSize);
// usernameField.setMaximumSize(fieldSize);
// passwordField.setMaximumSize(fieldSize);
Dimension buttonSize = new Dimension(100, 35);
loginButton.setSize(buttonSize);
createUserButton.setSize(buttonSize);
// Error label
errorLabelLogin = new JLabel("", SwingConstants.CENTER);
errorLabelLogin.setForeground(Color.RED);
errorLabelLogin.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(errorLabelLogin);
// Center the inner panel inside the outer panel
gbc.gridx = 0;
gbc.gridy = 0;
outerPanel.add(panel, gbc);
// ActionListener for login button
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String userText = usernameFieldLogin.getText().trim();
String passText = new String(pFieldLogin.getPassword()).trim();
if (userText.isEmpty()) {
errorLabelLogin.setText("Error: No username was filled.");
return;
} else if (passText.isEmpty()) {
errorLabelLogin.setText("Error: No password was filled.");
return;
}
User loggedInUser = Records.checkLogin(userText, passText);
if (loggedInUser == null) {
errorLabelLogin.setText("Error: Invalid username or password.");
return;
}
// Successful login -> store the user
currentUser = loggedInUser; // <-- IMPORTANT
errorLabelLogin.setText("");
if ("student".equalsIgnoreCase(currentUser.getRole())) {
cardLayout.show(cardPanel, STUDENTDASH);
} else if ("teacher".equalsIgnoreCase(currentUser.getRole())) {
cardLayout.show(cardPanel, TEACHERDASH);
}
}
});
createUserButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
resetUserCreationForm();
cardLayout.show(cardPanel, CREATION);
}
});
return outerPanel;
}
// user creation done
private JPanel createUserCreation() {
JPanel outerPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints formGbc = new GridBagConstraints();
formGbc.insets = new Insets(8, 8, 8, 8);
formGbc.fill = GridBagConstraints.HORIZONTAL;
JLabel title = new JLabel("User Creation", SwingConstants.CENTER);
title.setFont(new Font("Arial", Font.BOLD, 36));
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
panel.add(title, gbc);
JButton returnButton = new JButton("Return to Login");
gbc.gridy++;
panel.add(returnButton, gbc);
// Username
JLabel userLabel = new JLabel("Username:");
usernameFieldCreation = new JTextField(15);
formGbc.gridx = 0; formGbc.gridy = 2;
panel.add(userLabel, formGbc);
formGbc.gridx = 1;
panel.add(usernameFieldCreation, formGbc);
// Password
JLabel passwordLabel = new JLabel("Password:");
pFieldCreation = new JPasswordField(15);
formGbc.gridx = 0; formGbc.gridy = 3;
panel.add(passwordLabel, formGbc);
formGbc.gridx = 1;
panel.add(pFieldCreation, formGbc);
// Confirm password
JLabel cpasswordLabel = new JLabel("Confirm Password:");
cpFieldCreation = new JPasswordField(15);
formGbc.gridx = 0; formGbc.gridy = 4;
panel.add(cpasswordLabel, formGbc);
formGbc.gridx = 1;
panel.add(cpFieldCreation, formGbc);
// Role
studentBox = new JCheckBox("Student");
teacherBox = new JCheckBox("Teacher");
JPanel rolePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
rolePanel.add(studentBox);
rolePanel.add(new JLabel("or"));
rolePanel.add(teacherBox);
formGbc.gridx = 0; formGbc.gridy = 5; formGbc.gridwidth = 2;
panel.add(rolePanel, formGbc);
// Create button
JButton createUserButton = new JButton("Create new user");
formGbc.gridy = 6; formGbc.gridx = 0; formGbc.gridwidth = 2;
panel.add(createUserButton, formGbc);
// Error label
errorLabelCreation = new JLabel("", SwingConstants.CENTER);
errorLabelCreation.setForeground(Color.RED);
formGbc.gridy = 7;
panel.add(errorLabelCreation, formGbc);
gbc = new GridBagConstraints();
gbc.gridx = 0; gbc.gridy = 0;
outerPanel.add(panel, gbc);
// Events
returnButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
resetUserLogin();
cardLayout.show(cardPanel, LOGIN);
}
});
createUserButton.addActionListener(e -> {
String userText = usernameFieldCreation.getText().trim();
String passText = new String(pFieldCreation.getPassword()).trim();
String cpassText = new String(cpFieldCreation.getPassword()).trim();
if (userText.isEmpty()) {
errorLabelCreation.setText("Error: No username was filled.");
} else if (passText.isEmpty()) {
errorLabelCreation.setText("Error: No password was filled.");
} else if (cpassText.isEmpty()) {
errorLabelCreation.setText("Error: Password has not been confirmed.");
} else if (!cpassText.equals(passText)) {
errorLabelCreation.setText("Error: Password confirmation does not match password");
} else if (!studentBox.isSelected() && !teacherBox.isSelected()) {
errorLabelCreation.setText("Error: User has not selected role as teacher or student");
} else if (studentBox.isSelected() && teacherBox.isSelected()) {
errorLabelCreation.setText("Error: User cannot select both student and teacher");
} else {
if (studentBox.isSelected()) {
Records.addUser(new Student(userText, passText));
} else if (teacherBox.isSelected()) {
Records.addUser(new Teacher(userText, passText));
}
resetUserCreationForm(); // reset fields after submission
cardLayout.show(cardPanel, LOGIN);
}
});
return outerPanel;
}
// student dashboard mostly done
// add a few QoL features like welcome messages
// student dash board ongoing (fix it so that it uses a database if possible)
// need to add student profile
private JPanel createStudentDashboard() {
// Outer panel with GridBagLayout to center everything
JPanel outerPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
// Inner panel with BoxLayout for vertical stacking
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
// Title
JLabel title = new JLabel("Student Dashboard", SwingConstants.CENTER);
title.setFont(new Font("Arial", Font.BOLD, 36));
title.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(title);
panel.add(Box.createVerticalStrut(10));
// View Student Profile button
JButton viewProfile = new JButton("👤 View Student Profile");
viewProfile.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(viewProfile);
panel.add(Box.createVerticalStrut(10));
try {
// Activity Panel
JPanel activityPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
// Learn Math button
URL learnUrl = new URL("https://img.icons8.com/color/96/book.png");
JButton learnMath = new JButton("Learn Math", new ImageIcon(learnUrl));
learnMath.setHorizontalTextPosition(SwingConstants.CENTER);
learnMath.setVerticalTextPosition(SwingConstants.BOTTOM);
activityPanel.add(learnMath);
// Practice Math button
URL practiceUrl = new URL("https://img.icons8.com/color/96/pencil.png");
JButton practiceMath = new JButton("Practice Math", new ImageIcon(practiceUrl));
practiceMath.setHorizontalTextPosition(SwingConstants.CENTER);
practiceMath.setVerticalTextPosition(SwingConstants.BOTTOM);
activityPanel.add(practiceMath);
panel.add(activityPanel);
panel.add(Box.createVerticalStrut(15));
// Button sizes
Dimension buttonSize1 = new Dimension(400, 50);
viewProfile.setPreferredSize(buttonSize1);
Dimension buttonSize2 = new Dimension(300, 300);
learnMath.setPreferredSize(buttonSize2);
practiceMath.setPreferredSize(buttonSize2);
learnMath.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.show(cardPanel, LEARNMATH);
}
});
practiceMath.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.show(cardPanel, PRACTICEMATH);
// When entering Practice Math, start timer
SwingUtilities.invokeLater(() -> {
// Call a public method in GUI to start the timer
startPracticeTimer();
});
}
});
viewProfile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// logic for establishing the currentUser for the student profile
if (currentUser != null) {
// Remove old profile panel (if it exists)
for (Component comp : cardPanel.getComponents()) {
if (STUDENTPROFILE.equals(cardPanel.getLayout().toString())) {
cardPanel.remove(comp);
break;
}
}
// Refreshing logic if students wants to re-check their current history of performances
cardPanel.add(createViewStudentProfile(currentUser), STUDENTPROFILE); //Add a fresh profile panel for the logged-in student
cardLayout.show(cardPanel, STUDENTPROFILE); //Switching logic
cardPanel.revalidate();
cardPanel.repaint();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
gbc.gridx = 0;
gbc.gridy = 0;
outerPanel.add(panel, gbc);
// Logout Button
JButton logoutButton = new JButton("Logout");
logoutButton.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(Box.createVerticalStrut(15));
panel.add(logoutButton);
logoutButton.addActionListener(e -> {
currentUser = null;
resetUserLogin();
cardLayout.show(cardPanel, LOGIN);
});
return outerPanel;
}
// learn math page ongoing
// maybe add more color/visuals to the GUI
// create a default topic through topic
private JPanel createLearnMath() {
JPanel outerPanel = new JPanel(new GridBagLayout());
outerPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5); // spacing
// ===== TOP BAR =====
JPanel topBar = new JPanel(new GridBagLayout());
GridBagConstraints topGbc = new GridBagConstraints();
topGbc.insets = new Insets(5, 5, 5, 5);
JButton homeButton = new JButton("Home");
JLabel title = new JLabel("Learn Math");
title.setFont(new Font("Arial", Font.BOLD, 36));
JComboBox<String> topicDropdown = new JComboBox<>(new String[]{
"Algebra", "Areas & Perimeters" //, "Fractions", "Percentages"
});
// Home button on left
topGbc.gridx = 0;
topGbc.gridy = 0;
topGbc.anchor = GridBagConstraints.WEST;
topBar.add(homeButton, topGbc);
// Title centered
topGbc.gridx = 1;
topGbc.weightx = 1.0;
topGbc.anchor = GridBagConstraints.CENTER;
topBar.add(title, topGbc);
// Dropdown on right
topGbc.gridx = 2;
topGbc.weightx = 0;
topGbc.anchor = GridBagConstraints.EAST;
topBar.add(topicDropdown, topGbc);
// Add top bar to outerPanel
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
outerPanel.add(topBar, gbc);
// ===== LEFT: IMAGE =====
JPanel imagePanel = new JPanel();
imagePanel.setPreferredSize(new Dimension(200, 300));
imagePanel.setBorder(BorderFactory.createTitledBorder("Image"));
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 2;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0.2;
gbc.weighty = 1.0;
outerPanel.add(imagePanel, gbc);
// ===== CENTER: EXPLANATION =====
JPanel explanationPanel = new JPanel(new BorderLayout());
explanationPanel.setBorder(BorderFactory.createTitledBorder("Explanation"));
JTextArea explanationText = new JTextArea();
explanationText.setLineWrap(true);
explanationText.setWrapStyleWord(true);
explanationPanel.add(new JScrollPane(explanationText), BorderLayout.CENTER);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 2;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0.6;
gbc.weighty = 1.0;
outerPanel.add(explanationPanel, gbc);
// ===== RIGHT: EXAMPLES + PRACTICE BUTTON =====
JPanel rightPanel = new JPanel(new GridBagLayout());
GridBagConstraints rightGbc = new GridBagConstraints();
JPanel examplesPanel = new JPanel(new BorderLayout());
examplesPanel.setBorder(BorderFactory.createTitledBorder("Examples"));
JTextArea examplesText = new JTextArea();
examplesText.setLineWrap(true);
examplesText.setWrapStyleWord(true);
examplesPanel.add(new JScrollPane(examplesText), BorderLayout.CENTER);
rightGbc.gridx = 0;
rightGbc.gridy = 0;
rightGbc.weightx = 1.0;
rightGbc.weighty = 1.0;
rightGbc.fill = GridBagConstraints.BOTH;
rightPanel.add(examplesPanel, rightGbc);
JButton practiceButton = new JButton("Practice");
practiceButton.setPreferredSize(new Dimension(120, 40));
rightGbc.gridx = 0;
rightGbc.gridy = 1;
rightGbc.weighty = 0;
rightGbc.anchor = GridBagConstraints.CENTER;
rightPanel.add(practiceButton, rightGbc);
gbc.gridx = 2;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 2;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0.2;
gbc.weighty = 1.0;
outerPanel.add(rightPanel, gbc);
// ===== Logic =====
homeButton.addActionListener(e -> cardLayout.show(cardPanel, STUDENTDASH));
practiceButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.show(cardPanel, PRACTICEMATH);
// When entering Practice Math, start timer
SwingUtilities.invokeLater(() -> {
// Call a public method in GUI to start the timer
startPracticeTimer();
});
}
});
topicDropdown.addActionListener(e -> {
String selected = (String) topicDropdown.getSelectedItem();
Topics.Topic t = Topics.getTopic(selected);
if (t != null) {
explanationText.setText(t.getExplanation());
examplesText.setText(t.getExamples());
}
});
// Default selection
topicDropdown.setSelectedIndex(0);
return outerPanel;
}
// practice math ongoing
// maybe add more color/visuals to the GUI
// functionalities need to be added
private JPanel createPracticeMath() {
// Outer panel with GridBagLayout to center everything
JPanel outerPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
// Inner panel with BoxLayout for vertical stacking
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
// ===== TOP BAR =====
JPanel topBar = new JPanel();
topBar.setLayout(new BoxLayout(topBar, BoxLayout.X_AXIS));
JButton homeButton = new JButton("Home");
JLabel title = new JLabel("Practice Math");
title.setFont(new Font("Arial", Font.BOLD, 32));
title.setHorizontalAlignment(SwingConstants.CENTER);
JComboBox<String> topicDropdown = new JComboBox<>(Topics.topicNames());
topBar.add(homeButton);
topBar.add(Box.createHorizontalStrut(20));
topBar.add(title);
topBar.add(Box.createHorizontalGlue());
topBar.add(topicDropdown);
panel.add(topBar);
panel.add(Box.createVerticalStrut(20));
// ===== CENTER CONTENT =====
// Row: Question + Time
JPanel qTimeRow = new JPanel(new BorderLayout());
JLabel questionLabel = new JLabel("Question 0 of 10");
JLabel timeLabel = new JLabel("Time: 0s");
qTimeRow.add(questionLabel, BorderLayout.WEST);
qTimeRow.add(timeLabel, BorderLayout.EAST);
panel.add(qTimeRow);
panel.add(Box.createVerticalStrut(10));
// Row: Display question
JTextField questionField = new JTextField();
questionField.setFont(new Font("Arial", Font.PLAIN, 24));
questionField.setHorizontalAlignment(JTextField.CENTER);
questionField.setEditable(false);
questionField.setPreferredSize(new Dimension(600, 50));
panel.add(questionField);
panel.add(Box.createVerticalStrut(15));
// Row: Input + Answer button + Next Question Button
JPanel inputRow = new JPanel();
inputRow.setLayout(new BoxLayout(inputRow, BoxLayout.X_AXIS));
JTextField answerField = new JTextField();
answerField.setMaximumSize(new Dimension(250, 40));
JButton answerButton = new JButton("Answer");
answerButton.setMaximumSize(new Dimension(100, 40));
JButton nextQButton = new JButton("Next Question");
nextQButton.setMaximumSize(new Dimension(150, 40));
inputRow.add(answerField);
inputRow.add(Box.createHorizontalStrut(10));
inputRow.add(answerButton);
inputRow.add(nextQButton);
panel.add(inputRow);
panel.add(Box.createVerticalStrut(15));
// Row: Result
JLabel resultLabel = new JLabel("Result", SwingConstants.CENTER);
resultLabel.setFont(new Font("Arial", Font.BOLD, 18));
resultLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(resultLabel);
// Add inner panel to outerPanel (centered)
gbc.gridx = 0;
gbc.gridy = 0;
outerPanel.add(panel, gbc);
// ===== Logic =====
final int[] score = {0};
final int[] questionsAsked = {0};
final String[] currentAnswer = {""};
final boolean[] answered = {false}; // track if student already answered
// Timer logic
final int[] elapsedSeconds = {0};
timer = new Timer(1000, e -> {
elapsedSeconds[0]++;
timeLabel.setText("Time: " + elapsedSeconds[0] + "s");
});
// Apply a DocumentFilter to block letters and decimals
((javax.swing.text.AbstractDocument) answerField.getDocument()).setDocumentFilter(new javax.swing.text.DocumentFilter() {
@Override
public void insertString(FilterBypass fb, int offset, String string, javax.swing.text.AttributeSet attr)
throws javax.swing.text.BadLocationException {
if (string.matches("\\d+")) { // Only allow digits
super.insertString(fb, offset, string, attr);
}
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text, javax.swing.text.AttributeSet attrs)
throws javax.swing.text.BadLocationException {
if (text.matches("\\d+")) { // Only allow digits
super.replace(fb, offset, length, text, attrs);
}
}
});
Runnable newQuestion = () -> {
String selectedTopic = (String) topicDropdown.getSelectedItem();
String q = Questions.generateQuestion(selectedTopic);
questionField.setText(q);
currentAnswer[0] = Questions.getAnswer(q);
// FIX: Use empty string and ensure focus
answerField.setText("");
answerField.requestFocusInWindow();
resultLabel.setText("Result");
// enable answering again
answerField.setEnabled(true);
answerButton.setEnabled(true);
answered[0] = false;
// update question count
questionLabel.setText("Question " + (questionsAsked[0] + 1) + " of 10");
};
// Generate first question
newQuestion.run();
homeButton.addActionListener(e -> {
stopPracticeTimer(); // stop timer before leaving
cardLayout.show(cardPanel, STUDENTDASH); // then go back to dashboard
});
// When topic is changed, generate a new question
topicDropdown.addActionListener(e -> {
// reset counters when switching topics
questionsAsked[0] = 0;
score[0] = 0;
elapsedSeconds[0] = 0;
newQuestion.run();
});
answerButton.addActionListener(e -> {
if (answered[0]) return; // prevent multiple answers
String userAnswer = answerField.getText().trim();
if (userAnswer.equals(currentAnswer[0])) {
resultLabel.setText("Correct!");
score[0]++;
} else {
resultLabel.setText("Incorrect. The Correct answer is " + currentAnswer[0]);
}
questionsAsked[0]++;
answered[0] = true; // mark as answered
// disable input until nextQButton
answerField.setEnabled(false);
answerButton.setEnabled(false);
if (questionsAsked[0] >= 10) {
timer.stop();
// Save to report panel
JPanel reportPanel = practiceReportPanel;
JLabel scoreLabel = (JLabel) reportPanel.getClientProperty("scoreLabel");
JLabel timeTakenLabel = (JLabel) reportPanel.getClientProperty("timeLabel");
JTextArea feedbackArea = (JTextArea) reportPanel.getClientProperty("feedbackArea");
scoreLabel.setText(score[0] + "/10");
timeTakenLabel.setText(elapsedSeconds[0] + "s");
java.util.List<String> mistakes = new ArrayList<>();
feedbackArea.setText(Feedback.generateFeedback(score[0], 10, mistakes));
// Store attempt in student profile
if (currentUser != null && "student".equals(currentUser.getRole())) {
String topic = (String) topicDropdown.getSelectedItem();
Records.recordAttempt(currentUser, topic, score[0], elapsedSeconds[0]);
}
// Show report card
cardLayout.show(cardPanel, PRACTICEREPORT);
// Reset
score[0] = 0;
questionsAsked[0] = 0;
elapsedSeconds[0] = 0;
timer.start();
newQuestion.run();
}
});
nextQButton.addActionListener(e -> {
if (!answered[0]) {
// student skipped without answering
resultLabel.setText("Skipped. Correct answer was: " + currentAnswer[0]);
questionsAsked[0]++;
}
if (questionsAsked[0] >= 10) {
timer.stop();
// Save to report panel
JPanel reportPanel = practiceReportPanel;
JLabel scoreLabel = (JLabel) reportPanel.getClientProperty("scoreLabel");
JLabel timeTakenLabel = (JLabel) reportPanel.getClientProperty("timeLabel");
JTextArea feedbackArea = (JTextArea) reportPanel.getClientProperty("feedbackArea");
scoreLabel.setText(score[0] + "/10");
timeTakenLabel.setText(elapsedSeconds[0] + "s");
java.util.List<String> mistakes = new ArrayList<>();
feedbackArea.setText(Feedback.generateFeedback(score[0], 10, mistakes));
// Store attempt in student profile
if (currentUser != null && "student".equals(currentUser.getRole())) {
String topic = (String) topicDropdown.getSelectedItem();
Records.recordAttempt(currentUser, topic, score[0], elapsedSeconds[0]);
}
// Show report card
cardLayout.show(cardPanel, PRACTICEREPORT);
// Reset
score[0] = 0;
questionsAsked[0] = 0;
elapsedSeconds[0] = 0;
timer.start();
newQuestion.run();
}
newQuestion.run();
});
return outerPanel;
}
// practice report ongoing
// works well but logic needs to better intuitively understood by me the developer
// make it so results can be stored in the student profile and records which can later be viewed by teacher
// find ways to make feedback more powerful
private JPanel createPracticeReport() {
JPanel outerPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
gbc.fill = GridBagConstraints.BOTH;
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
// ===== TOP BAR =====
JPanel topBar = new JPanel(new FlowLayout(FlowLayout.LEFT));
JButton homeButton = new JButton("Home");
JLabel title = new JLabel("Practice Report", SwingConstants.CENTER);
title.setFont(new Font("Arial", Font.BOLD, 32));
topBar.add(homeButton);
panel.add(topBar);
title.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(title);
panel.add(Box.createVerticalStrut(20));
// ===== SCORE + TIME ROW =====
JPanel statsRow = new JPanel(new GridBagLayout());
GridBagConstraints statsGbc = new GridBagConstraints();
statsGbc.insets = new Insets(10, 20, 10, 20);
statsGbc.fill = GridBagConstraints.BOTH;
JLabel scoreLabel = new JLabel("Score: 0/10", SwingConstants.CENTER);
scoreLabel.setFont(new Font("Arial", Font.BOLD, 28));
scoreLabel.setBorder(BorderFactory.createTitledBorder("Total Score"));
JLabel timeLabel = new JLabel("0s", SwingConstants.CENTER);
timeLabel.setFont(new Font("Arial", Font.BOLD, 28));
timeLabel.setBorder(BorderFactory.createTitledBorder("Time Taken"));
statsGbc.gridx = 0; statsGbc.gridy = 0; statsGbc.weightx = 0.5;
statsRow.add(scoreLabel, statsGbc);
statsGbc.gridx = 1; statsGbc.gridy = 0; statsGbc.weightx = 0.5;
statsRow.add(timeLabel, statsGbc);
panel.add(statsRow);
panel.add(Box.createVerticalStrut(20));
// ===== FEEDBACK SECTION =====
JPanel feedbackPanel = new JPanel(new BorderLayout());
feedbackPanel.setBorder(BorderFactory.createTitledBorder("Feedbacks"));
JTextArea feedbackArea = new JTextArea("Feedback will appear here...");
feedbackArea.setLineWrap(true);
feedbackArea.setWrapStyleWord(true);
feedbackArea.setEditable(false);
feedbackPanel.add(new JScrollPane(feedbackArea), BorderLayout.CENTER);
panel.add(feedbackPanel);
panel.add(Box.createVerticalStrut(20));
// ===== RETRY BUTTON =====
JButton retryButton = new JButton("Retry");
retryButton.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(retryButton);
// Add everything to outerPanel
gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 1; gbc.weighty = 1;
outerPanel.add(panel, gbc);
// ===== Logic =====
homeButton.addActionListener(e -> cardLayout.show(cardPanel, STUDENTDASH));
retryButton.addActionListener(e -> cardLayout.show(cardPanel, PRACTICEMATH));
// Store components for later update
outerPanel.putClientProperty("scoreLabel", scoreLabel);
outerPanel.putClientProperty("timeLabel", timeLabel);
outerPanel.putClientProperty("feedbackArea", feedbackArea);
return outerPanel;
}
// student profile view ongoing
// add more aggregated values to help with more insight into student performance (average scores, fastest time completion, how many practices done so far, etc)
private JPanel createViewStudentProfile(User student) {
JPanel outerPanel = new JPanel(new GridBagLayout());
outerPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
// ===== Title =====
JLabel title = new JLabel("Practice Report - " + student.getUsername(), SwingConstants.CENTER);
title.setFont(new Font("Arial", Font.BOLD, 28));
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weighty = 0;
outerPanel.add(title, gbc);
// ===== Table Model =====
Student s = (Student) currentUser;
String[] columns = {"Topic", "Score", "Time Taken (s)"};
java.util.List<Student.PracticeAttempt> history = s.getHistory();
Object[][] data = new Object[history.size()][3];
for (int i = 0; i < history.size(); i++) {
Student.PracticeAttempt attempt = history.get(i);
data[i][0] = attempt.topic;
data[i][1] = attempt.score + "/10";
data[i][2] = attempt.timeTaken;
}
JTable table = new JTable(data, columns);
table.setEnabled(false);
table.setRowHeight(25);
JScrollPane scrollPane = new JScrollPane(table);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weighty = 1;
outerPanel.add(scrollPane, gbc);
// ===== Aggregate Stats =====
double avgScore = 0;
int fastestTime = Integer.MAX_VALUE;
int practices = history.size();
if (!history.isEmpty()) {
int totalScore = 0;
for (Student.PracticeAttempt attempt : history) {
totalScore += attempt.score;
if (attempt.timeTaken < fastestTime) {
fastestTime = attempt.timeTaken;
}
}
avgScore = (double) totalScore / practices;
}
String statsText = history.isEmpty()
? "No practice attempts yet."
: String.format("Average Score: %.2f | Fastest Time: %ds | Total Practices: %d",
avgScore, fastestTime == Integer.MAX_VALUE ? 0 : fastestTime, practices);
JLabel statsLabel = new JLabel(statsText, SwingConstants.CENTER);
statsLabel.setFont(new Font("Arial", Font.ITALIC, 16));
gbc.gridx = 0;
gbc.gridy = 2;
gbc.weighty = 0;
outerPanel.add(statsLabel, gbc);
// ===== Back Button =====
JButton backButton = new JButton("Back");
backButton.addActionListener(e -> cardLayout.show(cardPanel, STUDENTDASH));
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonPanel.add(backButton);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.weighty = 0;
outerPanel.add(buttonPanel, gbc);