-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriviaGUI.java
130 lines (105 loc) · 4.71 KB
/
TriviaGUI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
public class TriviaGUI {
private JFrame frame;
private JTextField usernameField;
private JPasswordField passwordField;
private TriviaGame game;
private Leaderboard leaderboard = new Leaderboard();
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
try {
TriviaGUI window = new TriviaGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
});
}
public TriviaGUI() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(28, 77, 75, 16);
frame.getContentPane().add(lblUsername);
usernameField = new JTextField();
usernameField.setBounds(115, 74, 130, 26);
frame.getContentPane().add(usernameField);
usernameField.setColumns(10);
JLabel lblPassword = new JLabel("Password:");
lblPassword.setBounds(28, 122, 75, 16);
frame.getContentPane().add(lblPassword);
passwordField = new JPasswordField();
passwordField.setBounds(115, 117, 130, 26);
frame.getContentPane().add(passwordField);
JButton btnLogin = new JButton("Login");
btnLogin.setBounds(115, 155, 117, 29);
frame.getContentPane().add(btnLogin);
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
// Authenticate user and start game
User user = new User(username, password);
game = new TriviaGame(user, QuestionBank.getQuestions());
JOptionPane.showMessageDialog(frame, "Login successful. Starting game.");
frame.dispose();
startGame();
}
});
}
private void startGame() {
JFrame gameFrame = new JFrame("Trivia Game");
gameFrame.setBounds(100, 100, 450, 300);
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.getContentPane().setLayout(null);
// Add components for the game (e.g., question label, answer buttons, etc.)
gameFrame.setVisible(true);
}
private void showFinalScore() {
JFrame finalScoreFrame = new JFrame("Final Score");
finalScoreFrame.setBounds(100, 100, 450, 300);
finalScoreFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
finalScoreFrame.getContentPane().setLayout(null);
JLabel lblFinalScore = new JLabel("Your final score is: " + game.getScore());
lblFinalScore.setBounds(115, 74, 200, 26);
finalScoreFrame.getContentPane().add(lblFinalScore);
JButton btnShowLeaderboard = new JButton("Show Leaderboard");
btnShowLeaderboard.setBounds(115, 155, 200, 29);
finalScoreFrame.getContentPane().add(btnShowLeaderboard);
btnShowLeaderboard.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showLeaderboard();
}
});
finalScoreFrame.setVisible(true);
}
private void showLeaderboard() {
JFrame leaderboardFrame = new JFrame("Leaderboard");
leaderboardFrame.setBounds(100, 100, 450, 300);
leaderboardFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
leaderboardFrame.getContentPane().setLayout(null);
JLabel lblLeaderboard = new JLabel("Leaderboard:");
lblLeaderboard.setBounds(28, 20, 100, 16);
leaderboardFrame.getContentPane().add(lblLeaderboard);
JTextArea textArea = new JTextArea();
textArea.setEditable(false);
textArea.setBounds(28, 50, 400, 200);
leaderboardFrame.getContentPane().add(textArea);
StringBuilder leaderboardText = new StringBuilder();
List<Leaderboard.UserScore> topScores = leaderboard.getTopScores(10);
for (Leaderboard.UserScore score : topScores) {
leaderboardText.append(score.getUsername()).append(": ").append(score.getScore()).append("\n");
}
textArea.setText(leaderboardText.toString());
leaderboardFrame.setVisible(true);
}
}