-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMultipleChoice.java
More file actions
347 lines (291 loc) · 12 KB
/
MultipleChoice.java
File metadata and controls
347 lines (291 loc) · 12 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
package src.classSrc;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.MouseInputAdapter;
import java.awt.event.MouseEvent;
import java.awt.Component.*;
public class MultipleChoice extends JFrame implements TestFrameImplement{
private ImageIcon backgroundImg;
private JLabel backgroundLabel;
private JLabel answerLabel;
private JPanel backgroundPanel;
private JTextArea quizArea;
private JTextField answerMessage;
private JButton menuButton;
private JButton submitButton;
private JButton buttonA;
private JButton buttonB;
private JButton buttonC;
private JButton buttonD;
private ActionListener ansListener = new AnswerEventListener();
private ActionListener cmdHandler = new ButtonEventListener();
private SortInfoReader quizReader;
private SortInfoReader ansReader;
private Random random = new Random();
private ArrayList<String> quizAnswer = new ArrayList<String>();
private int[][] visit;
private int quizNumber;
private int score;
private String quiz;
private String answer;
private String correctAns;
public MultipleChoice(int[][] visit, int quizNumber, int score) {
// init GUI
super("Quiz" + quizNumber);
setSize(1000, 600);
setLayout(null);
// set background
backgroundImg = new ImageIcon("src/imageSrc/Background.png");
backgroundLabel = new JLabel(backgroundImg);
backgroundLabel.setBounds(0, 0, getWidth(), getHeight());
backgroundPanel = (JPanel) this.getContentPane();
backgroundPanel.setOpaque(false);
getLayeredPane().add(backgroundLabel, Integer.valueOf(Integer.MIN_VALUE));
// set answer image
JTextField textField = new JTextField();
textField.setOpaque( false );
answerLabel = new JLabel();
answerLabel.setBounds(90, 485, 400, 50);
answerLabel.add( textField );
// set textArea
quizArea = new JTextArea();
quizArea.setFont(new Font("", Font.BOLD, 14));
quizArea.setBackground(Color.BLACK);
quizArea.setForeground(Color.WHITE);
quizArea.setEditable(false);
quizArea.setBounds(90, 50, 800, 300);
// set button
menuButton = new JButton(new ImageIcon("src/imageSrc/uncheckedMenu.png"));
menuButton.setBounds(780, 510, 100, 50);
menuButton.setActionCommand("menu");
menuButton.setOpaque(false);
menuButton.setContentAreaFilled(false);
menuButton.setFocusPainted(false);
menuButton.setBorderPainted(false);
menuButton.addActionListener(cmdHandler);
submitButton = new JButton(new ImageIcon("src/imageSrc/uncheckedSubmit.png"));
submitButton.setBounds(880, 510, 100, 50);
submitButton.setActionCommand("submit");
submitButton.setOpaque(false);
submitButton.setContentAreaFilled(false);
submitButton.setFocusPainted(false);
submitButton.setBorderPainted(false);
submitButton.addActionListener(cmdHandler);
submitButton.setEnabled(false);
buttonA = new JButton(new ImageIcon("src/imageSrc/uncheckedA.png"));
buttonA.setBounds(90, 400, 100, 50);
buttonA.setActionCommand("A");
buttonA.setOpaque(false);
buttonA.setContentAreaFilled(false);
buttonA.setFocusPainted(false);
buttonA.setBorderPainted(false);
buttonA.addActionListener(ansListener);
buttonB = new JButton(new ImageIcon("src/imageSrc/B.png"));
buttonB.setBounds(323, 400, 100, 50);
buttonB.setActionCommand("B");
buttonB.setOpaque(false);
buttonB.setContentAreaFilled(false);
buttonB.setFocusPainted(false);
buttonB.setBorderPainted(false);
buttonB.addActionListener(ansListener);
buttonC = new JButton(new ImageIcon("src/imageSrc/C.png"));
buttonC.setBounds(556, 400, 100, 50);
buttonC.setActionCommand("C");
buttonC.setOpaque(false);
buttonC.setContentAreaFilled(false);
buttonC.setFocusPainted(false);
buttonC.setBorderPainted(false);
buttonC.addActionListener(ansListener);
buttonD = new JButton(new ImageIcon("src/imageSrc/D.png"));
buttonD.setBounds(790, 400, 100, 50);
buttonD.setActionCommand("D");
buttonD.setOpaque(false);
buttonD.setContentAreaFilled(false);
buttonD.setFocusPainted(false);
buttonD.setBorderPainted(false);
buttonD.addActionListener(ansListener);
// set mouse listener
ButtonListener buttonListener = new ButtonListener();
menuButton.addMouseMotionListener(buttonListener);
submitButton.addMouseMotionListener(buttonListener);
OptionListener optionListener = new OptionListener();
buttonA.addMouseMotionListener(optionListener);
buttonB.addMouseMotionListener(optionListener);
buttonC.addMouseMotionListener(optionListener);
buttonD.addMouseMotionListener(optionListener);
addMouseMotionListener(buttonListener);
addMouseMotionListener(optionListener);
// add new elements to frame
add(quizArea);
add(menuButton);
add(submitButton);
add(buttonA);
add(buttonB);
add(buttonC);
add(buttonD);
add(answerLabel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
setVisit(visit);
setQuizNumber(quizNumber);
setScore(score);
setTest();
}
// set question & answer
public void setTest() {
int number;
while(true) {
number = random.nextInt(21) + 1;
if (visit[0][number] != 1)
break;
}
// read
quizReader = new SortInfoReader("src/testSrc/Single/" + number + ".txt", "UTF-8");
quiz = quizReader.getContent();
// read answer
ansReader = new SortInfoReader("src/answerSrc/Single/" + number + ".txt", "UTF-8");
correctAns = ansReader.getContent();
// set textArea
quizArea.setText(quiz);
// add visited
visit[0][number] = 1;
}
private void setVisit(int[][] newVisit) {
visit = newVisit;
}
public void setQuizNumber(int newNumber) {
this.quizNumber = newNumber;
}
public int getQuizNumber() {
return quizNumber;
}
public void setScore(int newScore) {
this.score = newScore;
}
public int getScore() {
return score;
}
private class ButtonListener extends MouseInputAdapter {
public void mouseMoved(MouseEvent e) {
if (e.getSource() == menuButton) {
menuButton.setIcon(new ImageIcon("src/imageSrc/Menu.png"));
} else {
menuButton.setIcon(new ImageIcon("src/imageSrc/uncheckedMenu.png"));
}
if (e.getSource() == submitButton) {
submitButton.setIcon(new ImageIcon("src/imageSrc/Submit.png"));
} else {
submitButton.setIcon(new ImageIcon("src/imageSrc/uncheckedSubmit.png"));
}
}
}
private class OptionListener extends MouseInputAdapter {
public void mouseMoved(MouseEvent e) {
if (e.getSource() == buttonA) {
buttonA.setIcon(new ImageIcon("src/imageSrc/A.png"));
} else {
buttonA.setIcon(new ImageIcon("src/imageSrc/uncheckedA.png"));
}
if (e.getSource() == buttonB) {
buttonB.setIcon(new ImageIcon("src/imageSrc/B.png"));
} else {
buttonB.setIcon(new ImageIcon("src/imageSrc/uncheckedB.png"));
}
if (e.getSource() == buttonC) {
buttonC.setIcon(new ImageIcon("src/imageSrc/C.png"));
} else {
buttonC.setIcon(new ImageIcon("src/imageSrc/uncheckedC.png"));
}
if (e.getSource() == buttonD) {
buttonD.setIcon(new ImageIcon("src/imageSrc/D.png"));
} else {
buttonD.setIcon(new ImageIcon("src/imageSrc/uncheckedD.png"));
}
}
}
private class AnswerEventListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
String option = event.getActionCommand();
submitButton.setEnabled(true);
if (option == "A") {
answer = "A";
} else if (option == "B") {
answer = "B";
} else if (option == "C") {
answer = "C";
} else if (option == "D") {
answer = "D";
}
answerLabel.setIcon(new ImageIcon("src/imageSrc/Option" + answer + ".png"));
}
}
private class ButtonEventListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd == "menu") {
JLabel x = new JLabel("You want to back to Menu?");
x.setFont(new Font("Times New Roman", Font.BOLD, 12));
int result = JOptionPane.showConfirmDialog(null, x, "Warning",
JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, new ImageIcon("src/imageSrc/Thinking.png"));
if (result == JOptionPane.YES_OPTION) { // yes button
new Menu();
setVisible(false);
}
} else if (cmd == "submit") {
int TorF;
String[] options = {"return", "next"};
System.out.println(answer);
if (answer.equals(correctAns) == false) { // wrong
JLabel y = new JLabel("oh-oooh, the answer is " + correctAns);
y.setFont(new Font("Times New Roman", Font.BOLD, 12));
TorF = JOptionPane.showOptionDialog(null, y, "You're wrong, dude...",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, new ImageIcon("src/imageSrc/Wrong.png"), options, options[0]);
} else {
setScore(getScore() + 20); // correct
JLabel y = new JLabel("Correct! Keep going!");
y.setFont(new Font("Times New Roman", Font.BOLD, 12));
TorF = JOptionPane.showOptionDialog(null, y, "Correct!",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, new ImageIcon("src/imageSrc/Right.png"), options, options[0]);
}
System.out.println("score = " + score);
if (TorF == 0) {
new Menu();
setVisible(false);
return;
}
if (quizNumber == 5) {
JOptionPane.showMessageDialog(null,
"Congratulation!!! You get " + score + " points in this test, well done!!!");
new Menu();
setVisible(false);
return;
}
int nextQuizType = random.nextInt(3) + 1;
//nextQuizType = 1;
System.out.println(nextQuizType);
switch(nextQuizType) {
case 1: // single
new MultipleChoice(visit, ++quizNumber, score);
setVisible(false);
break;
case 2: // yes/no
new TrueOrFalse(visit, ++quizNumber, score);
setVisible(false);
break;
case 3: // insert
new Cloze(visit, ++quizNumber, score);
setVisible(false);
break;
}
}
}
}
}