-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCloze.java
More file actions
329 lines (259 loc) · 12.5 KB
/
Cloze.java
File metadata and controls
329 lines (259 loc) · 12.5 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
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 Cloze extends JFrame implements TestFrameImplement {
private ImageIcon backgroundImg;
private JLabel backgroundLabel;
private JPanel backgroundPanel;
private JTextArea quizArea;
private JButton menuButton;
private JButton submitButton;
private String[] code;
private JTextField textField1;
private JTextField textField2;
private JTextField textField3;
private SortInfoReader quizReader;
private SortInfoReader ansReader;
private Random random = new Random();
// private ActionListener ansListener = new AnswerEventListener();
private ActionListener cmdHandler = new ButtonEventListener();
private ArrayList<String> quizAnswer = new ArrayList<String>();
private int[][] visit;
private int quizNumber;
private int score;
private String quiz;
private String correctAns;
public Cloze(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 textArea
quizArea = new JTextArea();
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.setVisible(true);
submitButton.setEnabled(true);
// set btn listener
ButtonListener buttonListener = new ButtonListener();
menuButton.addMouseMotionListener(buttonListener);
submitButton.addMouseMotionListener(buttonListener);
addMouseMotionListener(buttonListener);
// set text field1
textField1 = new JTextField();
textField1.setBounds(90,375,800,50);
textField1.setVisible(true);
textField1.setFont(new Font("Serif", Font.PLAIN, 18));
// add new elements to frame
add(quizArea);
add(menuButton);
add(submitButton);
add(textField1);
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(14) + 1;
if (visit[2][number] != 1)
break;
}
// read
quizReader = new SortInfoReader("src/testSrc/Insert/" + number + ".txt", "UTF-8");
quiz = quizReader.getContent();
// read answer
ansReader = new SortInfoReader("src/answerSrc/Insert/" + number + ".txt", "UTF-8");
correctAns = ansReader.getContent();
code = correctAns.split(" ");
if (code.length > 1) {
textField2 = new JTextField();
textField2.setBounds(90,425,800,50);
textField2.setVisible(true);
textField2.setFont(new Font("Serif", Font.PLAIN, 18));
add(textField2);
System.out.println(code[0]);
System.out.println(code[1]);
} else {
System.out.println(code[0]);
}
// set textArea
quizArea.setText(quiz);
// add visited
visit[2][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 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/Thinking4.png"));
if (result == JOptionPane.YES_OPTION) { // yes button
new Menu();
setVisible(false);
}
} else if (cmd == "submit") {
int TorF;
String ans = textField1.getText();
String[] options = { "return", "next" };
if (code.length == 2) { // 2 ans
String ans2 = textField2.getText();
System.out.println(ans);
System.out.println(ans2);
if (ans.equalsIgnoreCase(code[1]) == true && ans2.equalsIgnoreCase(code[0]) == true ) { // ans 顛倒
setScore(getScore() + 20);
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]);
}else if (ans.equalsIgnoreCase(code[0]) == false && ans2.equalsIgnoreCase(code[1]) == false ) { // wrong
JLabel y = new JLabel("oh-oooh, the answer is " + code[0] + " and " + code[1]);
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 if (ans.equalsIgnoreCase(code[0]) == true && ans2.equalsIgnoreCase(code[1]) == false) { // ans2
// wrong
setScore(getScore() + 10);
JLabel y = new JLabel("oh-oooh, the answer2 is " + code[1] + " but your answer2 is " + ans2);
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 if (ans.equalsIgnoreCase(code[0]) == false && ans2.equalsIgnoreCase(code[1]) == true) { // ans1
// wrong
setScore(getScore() + 10);
JLabel y = new JLabel("oh-oooh, the answer1 is " + code[0] + " but your answer1 is " + ans);
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 { // correct
setScore(getScore() + 20);
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]);
}
} else { // 1 ans
if (ans.equalsIgnoreCase(code[0]) == 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 { // correct
setScore(getScore() + 20);
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;
int nextQuizType = random.nextInt(3) + 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;
}
}
}
}
}