-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuizGUI.java
More file actions
1162 lines (978 loc) · 46 KB
/
QuizGUI.java
File metadata and controls
1162 lines (978 loc) · 46 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
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.Timer;
import java.util.List;
import java.awt.Toolkit;
import java.util.stream.Collectors;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.Arrays;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.GradientPaint;
import java.sql.SQLException;
public class QuizGUI extends JFrame {
private JPanel mainPanel;
private CardLayout cardLayout;
private JPanel welcomePanel;
private JPanel quizPanel;
private JPanel resultPanel;
private JPanel reviewPanel;
private JLabel questionLabel;
private JRadioButton[] optionButtons;
private JButton nextButton;
private JButton submitButton;
private JProgressBar progressBar;
private JLabel timerLabel;
private JLabel scoreLabel;
private javax.swing.Timer timer;
private int timeLeft;
private int currentQuestionIndex = 0;
private int score = 0;
private List<Question> questions;
private List<Question> answeredQuestions;
// Gen Z Color Palette - Vibrant & Modern
private Color primaryGradientStart = new Color(138, 43, 226); // BlueViolet
private Color primaryGradientEnd = new Color(30, 144, 255); // DodgerBlue
private Color secondaryGradientStart = new Color(255, 20, 147); // DeepPink
private Color secondaryGradientEnd = new Color(255, 165, 0); // Orange
private Color backgroundColor = new Color(10, 10, 20); // Ultra Dark Navy
private Color surfaceColor = new Color(25, 25, 40); // Dark Surface
private Color cardColor = new Color(35, 35, 55); // Card Background
private Color textColor = new Color(255, 255, 255); // Pure White
private Color accentColor = new Color(0, 255, 127); // SpringGreen (Success)
private Color errorColor = new Color(255, 69, 58); // SystemRed
private Color warningColor = new Color(255, 214, 10); // SystemYellow
private Color mutedText = new Color(155, 155, 170); // Muted Text
private Color hoverColor = new Color(60, 60, 80); // Hover State
// Dark mode colors (slightly different for variety)
private Color darkBg = new Color(0, 0, 0); // Pure Black
private Color darkSurface = new Color(18, 18, 18); // Almost Black
private Color darkCard = new Color(28, 28, 28); // Dark Card
private Color darkAccent = new Color(255, 0, 255); // Magenta
private boolean isDarkMode = false;
private JButton themeToggleButton;
private JButton soundToggleButton;
private JLabel difficultyLabel;
private JProgressBar timeProgressBar;
private Timer animationTimer;
private float animationProgress = 0f;
private boolean isPracticeMode = false;
private int hintsRemaining = 3;
private JButton hintButton;
private JLabel hintsLabel;
private JButton practiceModeButton;
private JComboBox<String> categoryComboBox;
private Map<String, Integer> highScores = new HashMap<>();
private JLabel highScoreLabel;
// Remove sound clip variables
private boolean soundEnabled = true;
private static final int QUESTIONS_PER_QUIZ = 10; // Number of questions to show in each quiz
public QuizGUI() {
setTitle("🔥 Quiz Beast - Level Up Your Knowledge 🚀");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(900, 700);
setLocationRelativeTo(null);
setResizable(false);
// Set custom icon and styling
try {
setIconImage(createGradientIcon());
} catch (Exception e) {
System.out.println("Could not set custom icon");
}
// Initialize components
initializeComponents();
setupLayout();
setupWelcomePanel();
setupQuizPanel();
setupResultPanel();
setupReviewPanel();
// Show welcome panel
cardLayout.show(mainPanel, "WELCOME");
}
private Image createGradientIcon() {
BufferedImage icon = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = icon.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint gradient = new GradientPaint(0, 0, primaryGradientStart, 32, 32, primaryGradientEnd);
g2d.setPaint(gradient);
g2d.fillOval(2, 2, 28, 28);
g2d.setColor(Color.WHITE);
g2d.setFont(new Font("Arial", Font.BOLD, 20));
g2d.drawString("Q", 10, 22);
g2d.dispose();
return icon;
}
private void initializeComponents() {
mainPanel = new JPanel();
cardLayout = new CardLayout();
mainPanel.setLayout(cardLayout);
welcomePanel = new JPanel();
quizPanel = new JPanel();
resultPanel = new JPanel();
reviewPanel = new JPanel();
questionLabel = new JLabel();
optionButtons = new JRadioButton[4];
nextButton = createStyledButton("Next ➡️", false);
submitButton = createStyledButton("Submit 🎯", true);
progressBar = new JProgressBar(0, 100);
styleProgressBar(progressBar);
timerLabel = new JLabel("⏰ 30s");
timerLabel.setFont(new Font("SF Pro Display", Font.BOLD, 18));
timerLabel.setForeground(textColor);
scoreLabel = new JLabel("🏆 Score: 0");
scoreLabel.setFont(new Font("SF Pro Display", Font.BOLD, 18));
scoreLabel.setForeground(accentColor);
ButtonGroup group = new ButtonGroup();
for (int i = 0; i < 4; i++) {
optionButtons[i] = createStyledRadioButton();
group.add(optionButtons[i]);
optionButtons[i].addActionListener(new OptionSelectListener(i));
}
// Theme toggle with modern styling
themeToggleButton = createIconButton(isDarkMode ? "🌞" : "🌙");
themeToggleButton.addActionListener(e -> toggleTheme());
themeToggleButton.setToolTipText("Toggle Dark/Light Mode");
// Sound toggle button
soundToggleButton = createIconButton(soundEnabled ? "🔊" : "🔇");
soundToggleButton.addActionListener(e -> toggleSound());
soundToggleButton.setToolTipText("Toggle Sound Effects");
timeProgressBar = new JProgressBar(0, 100);
styleProgressBar(timeProgressBar);
// Animation timer with smoother transitions
animationTimer = new Timer(16, e -> {
animationProgress += 0.05f;
if (animationProgress >= 1f) {
animationProgress = 0f;
((Timer)e.getSource()).stop();
}
repaint();
});
practiceModeButton = createStyledButton("🧠 Practice Mode", false);
practiceModeButton.addActionListener(e -> togglePracticeMode());
highScoreLabel = new JLabel("🎖️ High Score: 0");
highScoreLabel.setFont(new Font("SF Pro Display", Font.BOLD, 18));
highScoreLabel.setForeground(accentColor);
questions = new ArrayList<>();
answeredQuestions = new ArrayList<>();
}
private JButton createStyledButton(String text, boolean isPrimary) {
JButton button = new JButton(text);
button.setFont(new Font("SF Pro Display", Font.PLAIN, 16));
button.setForeground(Color.WHITE);
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setCursor(new Cursor(Cursor.HAND_CURSOR));
button.setOpaque(false);
button.setContentAreaFilled(false);
// Add hover effect
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
button.setCursor(new Cursor(Cursor.HAND_CURSOR));
button.setBorder(BorderFactory.createLineBorder(accentColor, 2, true));
}
@Override
public void mouseExited(MouseEvent e) {
button.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
button.setBorder(BorderFactory.createEmptyBorder());
}
});
// Custom painting for gradient buttons
button.setUI(new javax.swing.plaf.basic.BasicButtonUI() {
@Override
public void paint(Graphics g, JComponent c) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int width = c.getWidth();
int height = c.getHeight();
// Create gradient
GradientPaint gradient;
if (isPrimary) {
gradient = new GradientPaint(0, 0, primaryGradientStart, width, height, primaryGradientEnd);
} else {
gradient = new GradientPaint(0, 0, secondaryGradientStart, width, height, secondaryGradientEnd);
}
g2d.setPaint(gradient);
g2d.fillRoundRect(0, 0, width, height, 25, 25);
// Add subtle shadow effect
g2d.setColor(new Color(0, 0, 0, 30));
g2d.fillRoundRect(2, 2, width, height, 25, 25);
g2d.dispose();
super.paint(g, c);
}
});
return button;
}
private JButton createIconButton(String icon) {
JButton button = new JButton(icon);
button.setFont(new Font("Apple Color Emoji", Font.PLAIN, 24));
button.setForeground(textColor);
button.setBackground(surfaceColor);
button.setFocusPainted(false);
button.setBorder(BorderFactory.createEmptyBorder(10, 15, 10, 15));
button.setCursor(new Cursor(Cursor.HAND_CURSOR));
// Rounded corners
button.setUI(new javax.swing.plaf.basic.BasicButtonUI() {
@Override
public void paint(Graphics g, JComponent c) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(surfaceColor);
g2d.fillRoundRect(0, 0, c.getWidth(), c.getHeight(), 20, 20);
g2d.dispose();
super.paint(g, c);
}
});
return button;
}
private JRadioButton createStyledRadioButton() {
JRadioButton button = new JRadioButton();
button.setFont(new Font("SF Pro Display", Font.PLAIN, 16));
button.setForeground(textColor);
button.setBackground(cardColor);
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setOpaque(true);
button.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(60, 60, 80), 1),
BorderFactory.createEmptyBorder(15, 20, 15, 20)
));
// Hover effects
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
if (button.isEnabled()) {
button.setBackground(hoverColor);
button.setForeground(accentColor);
}
}
@Override
public void mouseExited(MouseEvent e) {
if (button.isEnabled()) {
button.setBackground(cardColor);
button.setForeground(textColor);
}
}
});
return button;
}
private void styleProgressBar(JProgressBar progressBar) {
progressBar.setStringPainted(true);
progressBar.setFont(new Font("SF Pro Display", Font.PLAIN, 12));
progressBar.setForeground(accentColor);
progressBar.setBackground(surfaceColor);
progressBar.setBorderPainted(false);
// Custom UI for rounded progress bar
progressBar.setUI(new javax.swing.plaf.basic.BasicProgressBarUI() {
@Override
protected void paintDeterminate(Graphics g, JComponent c) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int width = c.getWidth();
int height = c.getHeight();
// Background
g2d.setColor(surfaceColor);
g2d.fillRoundRect(0, 0, width, height, height, height);
// Progress
int progressWidth = (int) (width * (progressBar.getPercentComplete()));
GradientPaint gradient = new GradientPaint(0, 0, primaryGradientStart, progressWidth, 0, primaryGradientEnd);
g2d.setPaint(gradient);
g2d.fillRoundRect(0, 0, progressWidth, height, height, height);
g2d.dispose();
}
});
}
private void playSound(String soundType) {
if (!soundEnabled) return;
try {
switch (soundType) {
case "correct":
// Play system beep once for correct answer
Toolkit.getDefaultToolkit().beep();
break;
case "incorrect":
// Play system beep twice with different frequency for incorrect
Toolkit.getDefaultToolkit().beep();
try { Thread.sleep(200); } catch (InterruptedException e) {}
Toolkit.getDefaultToolkit().beep();
break;
case "submit":
// Play system beep three times with increasing frequency for submit
for (int i = 0; i < 3; i++) {
Toolkit.getDefaultToolkit().beep();
try { Thread.sleep(150); } catch (InterruptedException e) {}
}
break;
case "timeup":
// Play system beep four times with decreasing frequency for time up
for (int i = 0; i < 4; i++) {
Toolkit.getDefaultToolkit().beep();
try { Thread.sleep(100); } catch (InterruptedException e) {}
}
break;
case "click":
// Play a single beep for button clicks
Toolkit.getDefaultToolkit().beep();
break;
}
} catch (Exception e) {
System.out.println("Error playing sound: " + e.getMessage());
}
}
private void setupLayout() {
setLayout(new BorderLayout());
add(mainPanel, BorderLayout.CENTER);
// Set overall background
getContentPane().setBackground(backgroundColor);
}
private void setupWelcomePanel() {
welcomePanel.setLayout(new BorderLayout());
welcomePanel.setBackground(backgroundColor);
// Create main content panel
JPanel contentPanel = new JPanel();
contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
contentPanel.setBackground(backgroundColor);
contentPanel.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
// Title with gradient effect
JLabel titleLabel = new JLabel("QUIZ BEAST", SwingConstants.CENTER);
titleLabel.setFont(new Font("SF Pro Display", Font.BOLD, 48));
titleLabel.setForeground(textColor);
titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel subtitleLabel = new JLabel("🚀 Level up your knowledge game", SwingConstants.CENTER);
subtitleLabel.setFont(new Font("SF Pro Display", Font.PLAIN, 20));
subtitleLabel.setForeground(mutedText);
subtitleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel emojiLabel = new JLabel("🧠💯🔥", SwingConstants.CENTER);
emojiLabel.setFont(new Font("Apple Color Emoji", Font.PLAIN, 36));
emojiLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
JButton startButton = createStyledButton("🎮 START QUIZ", true);
startButton.setFont(new Font("SF Pro Display", Font.BOLD, 20));
startButton.setAlignmentX(Component.CENTER_ALIGNMENT);
startButton.setPreferredSize(new Dimension(250, 60));
startButton.addActionListener(e -> startQuiz());
// Stats panel
JPanel statsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 30, 0));
statsPanel.setBackground(backgroundColor);
statsPanel.add(highScoreLabel);
// Control buttons panel
JPanel controlsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0));
controlsPanel.setBackground(backgroundColor);
controlsPanel.add(practiceModeButton);
contentPanel.add(emojiLabel);
contentPanel.add(Box.createVerticalStrut(20));
contentPanel.add(titleLabel);
contentPanel.add(Box.createVerticalStrut(10));
contentPanel.add(subtitleLabel);
contentPanel.add(Box.createVerticalStrut(40));
contentPanel.add(startButton);
contentPanel.add(Box.createVerticalStrut(30));
contentPanel.add(statsPanel);
contentPanel.add(Box.createVerticalStrut(20));
contentPanel.add(controlsPanel);
// Theme toggle in top right
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
topPanel.setBackground(backgroundColor);
topPanel.add(soundToggleButton);
topPanel.add(themeToggleButton);
welcomePanel.add(topPanel, BorderLayout.NORTH);
welcomePanel.add(contentPanel, BorderLayout.CENTER);
mainPanel.add(welcomePanel, "WELCOME");
}
private void setupQuizPanel() {
quizPanel.setLayout(new BorderLayout(0, 20));
quizPanel.setBackground(backgroundColor);
quizPanel.setBorder(BorderFactory.createEmptyBorder(30, 40, 30, 40));
// Top panel with better spacing
JPanel topPanel = new JPanel(new BorderLayout(20, 15));
topPanel.setBackground(backgroundColor);
// Stats row
JPanel statsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 30, 0));
statsPanel.setBackground(backgroundColor);
statsPanel.add(scoreLabel);
statsPanel.add(timerLabel);
// Theme toggle
JPanel rightPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
rightPanel.setBackground(backgroundColor);
rightPanel.add(soundToggleButton);
rightPanel.add(themeToggleButton);
topPanel.add(statsPanel, BorderLayout.WEST);
topPanel.add(rightPanel, BorderLayout.EAST);
topPanel.add(progressBar, BorderLayout.SOUTH);
// Center panel with card-like styling
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
centerPanel.setBackground(cardColor);
centerPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(60, 60, 80), 1),
BorderFactory.createEmptyBorder(30, 40, 30, 40)
));
questionLabel.setFont(new Font("SF Pro Display", Font.BOLD, 22));
questionLabel.setForeground(textColor);
questionLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
questionLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 30, 0));
JPanel optionsPanel = new JPanel();
optionsPanel.setLayout(new BoxLayout(optionsPanel, BoxLayout.Y_AXIS));
optionsPanel.setBackground(cardColor);
for (JRadioButton button : optionButtons) {
optionsPanel.add(button);
optionsPanel.add(Box.createVerticalStrut(15));
}
centerPanel.add(questionLabel);
centerPanel.add(optionsPanel);
// Bottom panel
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0));
bottomPanel.setBackground(backgroundColor);
submitButton.setVisible(true);
submitButton.setPreferredSize(new Dimension(180, 50));
submitButton.addActionListener(e -> submitQuiz());
bottomPanel.add(submitButton);
quizPanel.add(topPanel, BorderLayout.NORTH);
quizPanel.add(centerPanel, BorderLayout.CENTER);
quizPanel.add(bottomPanel, BorderLayout.SOUTH);
mainPanel.add(quizPanel, "QUIZ");
}
private void setupResultPanel() {
resultPanel.setLayout(new BorderLayout());
resultPanel.setBackground(backgroundColor);
JPanel contentPanel = new JPanel();
contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
contentPanel.setBackground(backgroundColor);
contentPanel.setBorder(BorderFactory.createEmptyBorder(80, 50, 80, 50));
JLabel resultEmojiLabel = new JLabel("🎉🏆🎊", SwingConstants.CENTER);
resultEmojiLabel.setFont(new Font("Apple Color Emoji", Font.PLAIN, 48));
resultEmojiLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel resultLabel = new JLabel("QUIZ COMPLETE!", SwingConstants.CENTER);
resultLabel.setFont(new Font("SF Pro Display", Font.BOLD, 42));
resultLabel.setForeground(accentColor);
resultLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel finalScoreLabel = new JLabel();
finalScoreLabel.setFont(new Font("SF Pro Display", Font.BOLD, 28));
finalScoreLabel.setForeground(textColor);
finalScoreLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
JButton reviewButton = createStyledButton("📊 Review Answers", false);
reviewButton.setAlignmentX(Component.CENTER_ALIGNMENT);
reviewButton.setPreferredSize(new Dimension(220, 50));
reviewButton.addActionListener(e -> showReview());
JButton newQuizButton = createStyledButton("🔄 Try Again", true);
newQuizButton.setAlignmentX(Component.CENTER_ALIGNMENT);
newQuizButton.setPreferredSize(new Dimension(220, 50));
newQuizButton.addActionListener(e -> resetQuiz());
contentPanel.add(resultEmojiLabel);
contentPanel.add(Box.createVerticalStrut(20));
contentPanel.add(resultLabel);
contentPanel.add(Box.createVerticalStrut(30));
contentPanel.add(finalScoreLabel);
contentPanel.add(Box.createVerticalStrut(50));
contentPanel.add(reviewButton);
contentPanel.add(Box.createVerticalStrut(20));
contentPanel.add(newQuizButton);
resultPanel.add(contentPanel, BorderLayout.CENTER);
mainPanel.add(resultPanel, "RESULT");
}
private void setupReviewPanel() {
reviewPanel.setLayout(new BorderLayout(20, 20));
reviewPanel.setBackground(backgroundColor);
reviewPanel.setBorder(BorderFactory.createEmptyBorder(30, 40, 30, 40));
// Header panel with title and back button
JPanel headerPanel = new JPanel(new BorderLayout());
headerPanel.setBackground(backgroundColor);
JLabel reviewTitle = new JLabel("📝 Answer Review");
reviewTitle.setFont(new Font("SF Pro Display", Font.BOLD, 24));
reviewTitle.setForeground(textColor);
headerPanel.add(reviewTitle, BorderLayout.WEST);
JButton backButton = createStyledButton("⬅️ Back to Results", false);
backButton.setPreferredSize(new Dimension(200, 45));
backButton.addActionListener(e -> cardLayout.show(mainPanel, "RESULT"));
headerPanel.add(backButton, BorderLayout.EAST);
// Center panel with scrollable content
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
centerPanel.setBackground(backgroundColor);
JScrollPane scrollPane = new JScrollPane(centerPanel);
scrollPane.setBackground(backgroundColor);
scrollPane.getViewport().setBackground(backgroundColor);
scrollPane.setBorder(null);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
reviewPanel.add(headerPanel, BorderLayout.NORTH);
reviewPanel.add(scrollPane, BorderLayout.CENTER);
mainPanel.add(reviewPanel, "REVIEW");
}
private void startQuiz() {
try {
// Fetch questions from database
List<Question> allQuestions = DatabaseConnector.getQuestions();
if (allQuestions.isEmpty()) {
JOptionPane.showMessageDialog(this, "No questions available in the database.");
return;
}
// Randomly select questions for this quiz
questions = selectRandomQuestions(allQuestions, QUESTIONS_PER_QUIZ);
// Shuffle the options for each question
for (Question question : questions) {
shuffleOptions(question);
}
answeredQuestions = new ArrayList<>();
currentQuestionIndex = 0;
score = 0;
updateScoreLabel();
displayQuestion();
startTimer();
cardLayout.show(mainPanel, "QUIZ");
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Error connecting to database: " + e.getMessage());
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Error loading questions: " + e.getMessage());
}
}
private List<Question> selectRandomQuestions(List<Question> allQuestions, int count) {
List<Question> selectedQuestions = new ArrayList<>(allQuestions);
Collections.shuffle(selectedQuestions);
return selectedQuestions.subList(0, Math.min(count, selectedQuestions.size()));
}
private void shuffleOptions(Question question) {
String[] options = question.getOptions();
String correctAnswer = question.getCorrectAnswer();
// Create a list of option indices
List<Integer> indices = new ArrayList<>();
for (int i = 0; i < options.length; i++) {
indices.add(i);
}
// Shuffle the indices
Collections.shuffle(indices);
// Create new arrays for shuffled options
String[] shuffledOptions = new String[options.length];
for (int i = 0; i < indices.size(); i++) {
shuffledOptions[i] = options[indices.get(i)];
}
// Update the question with shuffled options
question.setOptions(shuffledOptions);
// Update the correct answer index
for (int i = 0; i < shuffledOptions.length; i++) {
if (shuffledOptions[i].equals(correctAnswer)) {
question.setCorrectAnswer(shuffledOptions[i]);
break;
}
}
}
private void displayQuestion() {
if (currentQuestionIndex < questions.size()) {
Question question = questions.get(currentQuestionIndex);
questionLabel.setText("<html><div style='text-align: center;'>" + question.getQuestionText() + "</div></html>");
String[] options = question.getOptions();
for (int i = 0; i < 4; i++) {
optionButtons[i].setText(options[i]);
optionButtons[i].setSelected(false);
optionButtons[i].setEnabled(true);
// Reset styling
optionButtons[i].setBackground(cardColor);
optionButtons[i].setForeground(textColor);
}
progressBar.setValue((currentQuestionIndex * 100) / questions.size());
progressBar.setString(String.format("Question %d of %d", currentQuestionIndex + 1, questions.size()));
animationProgress = 0f;
animationTimer.start();
} else {
showResults();
}
}
private void processAnswer() {
if (currentQuestionIndex >= questions.size()) {
showResults();
return;
}
Question question = questions.get(currentQuestionIndex);
String selectedAnswer = getSelectedAnswer();
if (selectedAnswer != null) {
question.setUserAnswer(selectedAnswer);
answeredQuestions.add(question);
// Visual feedback
for (JRadioButton button : optionButtons) {
button.setEnabled(false);
if (button.isSelected()) {
if (selectedAnswer.equals(question.getCorrectAnswer())) {
button.setBackground(accentColor);
button.setForeground(Color.WHITE);
playSound("correct");
} else {
button.setBackground(errorColor);
button.setForeground(Color.WHITE);
playSound("incorrect");
}
}
}
boolean isCorrect = selectedAnswer.equals(question.getCorrectAnswer());
if (isCorrect) {
score++;
updateScoreLabel();
}
currentQuestionIndex++;
if (currentQuestionIndex < questions.size()) {
javax.swing.Timer delayTimer = new javax.swing.Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
displayQuestion();
startTimer();
}
});
delayTimer.setRepeats(false);
delayTimer.start();
} else {
showResults();
}
}
}
private void submitQuiz() {
if (currentQuestionIndex < questions.size()) {
// If there are remaining questions, add them as unanswered
for (int i = currentQuestionIndex; i < questions.size(); i++) {
Question question = questions.get(i);
question.setUserAnswer("No Answer");
answeredQuestions.add(question);
}
}
playSound("submit");
showResults();
}
private void showResults() {
if (timer != null) {
timer.stop();
}
JPanel contentPanel = (JPanel) resultPanel.getComponent(0);
contentPanel.removeAll();
contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
contentPanel.setBackground(backgroundColor);
contentPanel.setBorder(BorderFactory.createEmptyBorder(80, 50, 80, 50));
JLabel resultEmojiLabel = new JLabel("🎉🏆🎊", SwingConstants.CENTER);
resultEmojiLabel.setFont(new Font("Apple Color Emoji", Font.PLAIN, 48));
resultEmojiLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel resultLabel = new JLabel("QUIZ COMPLETE!", SwingConstants.CENTER);
resultLabel.setFont(new Font("SF Pro Display", Font.BOLD, 42));
resultLabel.setForeground(accentColor);
resultLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
// Calculate answered questions count
int answeredCount = (int) answeredQuestions.stream()
.filter(q -> q.getUserAnswer() != null && !q.getUserAnswer().equals("No Answer"))
.count();
JLabel finalScoreLabel = new JLabel(String.format("Your Score: %d/%d", score, answeredCount), SwingConstants.CENTER);
finalScoreLabel.setFont(new Font("SF Pro Display", Font.BOLD, 28));
finalScoreLabel.setForeground(textColor);
finalScoreLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
// Add overview panel
JPanel overviewPanel = new JPanel();
overviewPanel.setLayout(new BoxLayout(overviewPanel, BoxLayout.Y_AXIS));
overviewPanel.setBackground(cardColor);
overviewPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(60, 60, 80), 1),
BorderFactory.createEmptyBorder(20, 20, 20, 20)
));
JLabel overviewTitle = new JLabel("📊 Quiz Overview", SwingConstants.CENTER);
overviewTitle.setFont(new Font("SF Pro Display", Font.BOLD, 20));
overviewTitle.setForeground(textColor);
overviewTitle.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel correctAnswers = new JLabel(String.format("✅ Correct Answers: %d", score), SwingConstants.CENTER);
correctAnswers.setFont(new Font("SF Pro Display", Font.PLAIN, 16));
correctAnswers.setForeground(accentColor);
correctAnswers.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel incorrectAnswers = new JLabel(String.format("❌ Incorrect Answers: %d", answeredCount - score), SwingConstants.CENTER);
incorrectAnswers.setFont(new Font("SF Pro Display", Font.PLAIN, 16));
incorrectAnswers.setForeground(errorColor);
incorrectAnswers.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel unansweredQuestions = new JLabel(String.format("⏭️ Unanswered Questions: %d", questions.size() - answeredCount), SwingConstants.CENTER);
unansweredQuestions.setFont(new Font("SF Pro Display", Font.PLAIN, 16));
unansweredQuestions.setForeground(mutedText);
unansweredQuestions.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel accuracy = new JLabel(String.format("🎯 Accuracy: %.1f%%", answeredCount > 0 ? (score * 100.0) / answeredCount : 0), SwingConstants.CENTER);
accuracy.setFont(new Font("SF Pro Display", Font.PLAIN, 16));
accuracy.setForeground(textColor);
accuracy.setAlignmentX(Component.CENTER_ALIGNMENT);
overviewPanel.add(overviewTitle);
overviewPanel.add(Box.createVerticalStrut(10));
overviewPanel.add(correctAnswers);
overviewPanel.add(Box.createVerticalStrut(5));
overviewPanel.add(incorrectAnswers);
overviewPanel.add(Box.createVerticalStrut(5));
overviewPanel.add(unansweredQuestions);
overviewPanel.add(Box.createVerticalStrut(5));
overviewPanel.add(accuracy);
JButton reviewButton = createStyledButton("📊 Review Answers", false);
reviewButton.setAlignmentX(Component.CENTER_ALIGNMENT);
reviewButton.setPreferredSize(new Dimension(220, 50));
reviewButton.addActionListener(e -> showReview());
JButton newQuizButton = createStyledButton("🔄 Try Again", true);
newQuizButton.setAlignmentX(Component.CENTER_ALIGNMENT);
newQuizButton.setPreferredSize(new Dimension(220, 50));
newQuizButton.addActionListener(e -> resetQuiz());
contentPanel.add(resultEmojiLabel);
contentPanel.add(Box.createVerticalStrut(20));
contentPanel.add(resultLabel);
contentPanel.add(Box.createVerticalStrut(30));
contentPanel.add(finalScoreLabel);
contentPanel.add(Box.createVerticalStrut(30));
contentPanel.add(overviewPanel);
contentPanel.add(Box.createVerticalStrut(50));
contentPanel.add(reviewButton);
contentPanel.add(Box.createVerticalStrut(20));
contentPanel.add(newQuizButton);
contentPanel.revalidate();
contentPanel.repaint();
// Update high score if needed
int currentHighScore = highScores.getOrDefault("default", 0);
if (score > currentHighScore) {
highScores.put("default", score);
highScoreLabel.setText("🎖️ High Score: " + score);
}
cardLayout.show(mainPanel, "RESULT");
}
private void showReview() {
JPanel centerPanel = (JPanel) ((JScrollPane) reviewPanel.getComponent(1)).getViewport().getView();
centerPanel.removeAll();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
centerPanel.setBackground(backgroundColor);
for (Question question : answeredQuestions) {
JPanel questionPanel = new JPanel();
questionPanel.setLayout(new BoxLayout(questionPanel, BoxLayout.Y_AXIS));
questionPanel.setBackground(cardColor);
questionPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(60, 60, 80), 1),
BorderFactory.createEmptyBorder(20, 20, 20, 20)
));
// Question number and text
JLabel questionNumber = new JLabel("Question " + (answeredQuestions.indexOf(question) + 1));
questionNumber.setFont(new Font("SF Pro Display", Font.BOLD, 16));
questionNumber.setForeground(accentColor);
questionNumber.setAlignmentX(Component.LEFT_ALIGNMENT);
JLabel questionLabel = new JLabel("<html><div style='text-align: left;'>" + question.getQuestionText() + "</div></html>");
questionLabel.setFont(new Font("SF Pro Display", Font.BOLD, 16));
questionLabel.setForeground(textColor);
questionLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
// User's answer
JLabel userAnswerLabel = new JLabel("Your Answer: " + (question.getUserAnswer() != null ? question.getUserAnswer() : "No Answer"));
userAnswerLabel.setFont(new Font("SF Pro Display", Font.PLAIN, 14));
userAnswerLabel.setForeground(question.getUserAnswer() != null && question.getUserAnswer().equals(question.getCorrectAnswer()) ?
accentColor : errorColor);
userAnswerLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
// Correct answer
JLabel correctAnswerLabel = new JLabel("Correct Answer: " + question.getCorrectAnswer());
correctAnswerLabel.setFont(new Font("SF Pro Display", Font.PLAIN, 14));
correctAnswerLabel.setForeground(accentColor);
correctAnswerLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
// Add status icon
JLabel statusIcon = new JLabel(question.getUserAnswer() != null && question.getUserAnswer().equals(question.getCorrectAnswer()) ?
"✅" : "❌");
statusIcon.setFont(new Font("Apple Color Emoji", Font.PLAIN, 20));
statusIcon.setAlignmentX(Component.LEFT_ALIGNMENT);
// Add all components to question panel
questionPanel.add(questionNumber);
questionPanel.add(Box.createVerticalStrut(10));
questionPanel.add(questionLabel);
questionPanel.add(Box.createVerticalStrut(15));
questionPanel.add(statusIcon);
questionPanel.add(Box.createVerticalStrut(5));
questionPanel.add(userAnswerLabel);
questionPanel.add(Box.createVerticalStrut(5));
questionPanel.add(correctAnswerLabel);
// Add question panel to center panel
centerPanel.add(questionPanel);
centerPanel.add(Box.createVerticalStrut(20));
}
// Add padding at the bottom
centerPanel.add(Box.createVerticalStrut(20));
// Refresh the panel
centerPanel.revalidate();
centerPanel.repaint();
// Show the review panel
cardLayout.show(mainPanel, "REVIEW");
}
private void resetQuiz() {
if (timer != null) {
timer.stop();
}
try {
// Fetch new questions from database
List<Question> allQuestions = DatabaseConnector.getQuestions();
if (allQuestions.isEmpty()) {
JOptionPane.showMessageDialog(this, "No questions available in the database.");
return;
}
// Reset all state variables
currentQuestionIndex = 0;
score = 0;
hintsRemaining = 3;
if (hintsLabel != null) {
hintsLabel.setText("✨ Hints: " + hintsRemaining);
}
// Clear answered questions
if (answeredQuestions == null) {
answeredQuestions = new ArrayList<>();
} else {
answeredQuestions.clear();
}
// Reset UI elements
for (JRadioButton button : optionButtons) {
button.setSelected(false);
button.setEnabled(true);
button.setBackground(cardColor);
button.setForeground(textColor);
}
// Select new random questions
questions = selectRandomQuestions(allQuestions, QUESTIONS_PER_QUIZ);
// Shuffle options for each question
for (Question question : questions) {
shuffleOptions(question);
}
// Update UI
updateScoreLabel();
displayQuestion();
startTimer();
// Show quiz panel
cardLayout.show(mainPanel, "QUIZ");