-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuiz.java
603 lines (507 loc) · 17.8 KB
/
Quiz.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
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
package beta;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import beta.ViewAccuracy;
public class Quiz extends JPanel implements ActionListener {
private JTextField textField;
private int _level;
private int _testNo=1;
private int _attempts;
private int _fails;
private int _maxNum;
protected ArrayList<String> _voices;
private ArrayList<Word> _testList = new ArrayList<Word>();
private String _voice;
private JComboBox<String> _selectVoices;
private JPanel _main;
private int _wc;
private int _testNum;
private int _coins = 0;
private int _streak = 0;
private JButton btnListenAgain = new JButton("Repeat");
private JButton btnSubmit = new JButton("Submit");
private JButton btnSettings = new JButton("");
private JButton btnMenu = new JButton("Menu");
private JLabel lblCoin = new JLabel("Coins: 0");
private JLabel lblPleaseSpellWord = new JLabel("Please spell word 1 of 3:");
private JLabel lblAcc = new JLabel("Accuracy: 0/10");
private JLabel lblCorrect = new JLabel("<html>Oh hello there! My name is Richard Parker, and I will be helping you with your spelling today.</html>");
private JLabel lblStreak = new JLabel();
private int _correct=0;
private int incorrect;
private JFrame _frame;
private WordList _wordlist;
private int _hiddenCoins;
private File _file;
private Sound _sound;
private Timer _timer;
private int _highScore;
private String _fileName;
private String _correctness;
/**
* Create the panel.
*/
public Quiz(WordList wordlist,int level, JFrame frame, int maxNum, File file, Sound sound) throws Exception {
_level = level;
_frame = frame;
_maxNum = maxNum;
_wordlist = wordlist;
_file = file;
_sound = sound;
_fileName = _file.getName();
getAccuracy();
getCoins();
getVoices();
_voice = _voices.get(0);
//changeVoice("voice_akl_nz_jdt_diphone");
this.setBackground(new Color(255, 255, 153));
this.setLayout(null);
JLabel lblQuiz = new JLabel("Quiz");
lblQuiz.setHorizontalAlignment(SwingConstants.CENTER);
lblQuiz.setFont(new Font("Comic Sans MS", Font.PLAIN, 40));
lblQuiz.setBounds(0, 11, 850, 57);
this.add(lblQuiz);
//Level Subtitle
JLabel lblLevel = new JLabel("Level " + _level);
lblLevel.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
lblLevel.setHorizontalAlignment(SwingConstants.CENTER);
lblLevel.setBounds(10, 65, 850, 26);
this.add(lblLevel);
//Setting up GUI features
lblPleaseSpellWord.setFont(new Font("Arial", Font.PLAIN, 26));
lblPleaseSpellWord.setBounds(56, 157, 433, 49);
this.add(lblPleaseSpellWord);
textField = new JTextField();
textField.setFont(new Font("Calibri", Font.PLAIN, 20));
textField.setBounds(56, 227, 738, 49);
this.add(textField);
textField.setColumns(10);
btnSubmit.setBounds(442, 287, 157, 49);
btnSubmit.setFont(new Font("Arial Black", Font.PLAIN, 20));
btnSubmit.setBorder(new MatteBorder(1,1,1,1, new Color(0,0,0)));
btnSubmit.setBackground(new Color(255,153, 51));
btnSubmit.setForeground(new Color(255,255, 153));
this.add(btnSubmit);
btnListenAgain.setBounds(268, 287, 157, 49);
btnListenAgain.setFont(new Font("Arial Black", Font.PLAIN, 20));
btnListenAgain.setBorder(new MatteBorder(1,1,1,1, new Color(0,0,0)));
btnListenAgain.setBackground(new Color(255,153, 51));
btnListenAgain.setForeground(new Color(255,255, 153));
this.add(btnListenAgain);
lblAcc.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
lblAcc.setBounds(616, 436, 157, 49);
this.add(lblAcc);
btnSettings.setIcon(new ImageIcon("gear_ss.png"));
btnSettings.setBounds(737, 511, 57, 57);
btnSettings.setBackground(new Color(255,255,50));
btnSettings.setBorder(new MatteBorder(1,1,1,1, new Color(0,0,0)));
btnSettings.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Settings set = new Settings(_frame, "", Dialog.ModalityType.APPLICATION_MODAL);
set.setVisible(true);
}
});
this.add(btnSettings);
lblCoin.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
lblCoin.setIcon(new ImageIcon("Coin1.png"));
lblCoin.setBounds(616, 385, 148, 49);
this.add(lblCoin);
lblStreak.setFont(new Font("Calibri", Font.PLAIN, 15));
lblStreak.setBounds(580, 385, 70, 49);
this.add(lblStreak);
JLabel beelbl = new JLabel("");
beelbl.setIcon(new ImageIcon("bee_h.png"));
beelbl.setBounds(611, 55, 157, 134);
this.add(beelbl);
JLabel lblUpperBox = new JLabel("");
lblUpperBox.setIcon(new ImageIcon("uppertbox.png"));
lblUpperBox.setBounds(46, 135, 584, 92);
this.add(lblUpperBox);
btnMenu.setFont(new Font("Calibri", Font.PLAIN, 18));
btnMenu.setBounds(620, 511, 100, 57);
btnMenu.setBackground(new Color(255,255,50));
btnMenu.setBorder(new MatteBorder(1,1,1,1, new Color(0,0,0)));
btnMenu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int option = JOptionPane.showConfirmDialog (_frame, "Are you sure you want to return to the main menu?","",JOptionPane.YES_NO_OPTION);
if(option == JOptionPane.YES_OPTION){
setVisible(false);
Menu menu = new Menu(_level, _frame, _wordlist, _maxNum, _file, _sound);
_frame.getContentPane().add(menu);
menu.setVisible(true);
}
}
});
this.add(btnMenu);
JLabel lblSBee = new JLabel("");
lblSBee.setIcon(new ImageIcon("sbee2.png"));
lblSBee.setBounds(39, 492, 81, 76);
this.add(lblSBee);
lblCorrect.setBounds(140, 200, 400, 400);
lblCorrect.setFont(new Font("Arial", Font.PLAIN, 18));
this.add(lblCorrect);
JLabel lblLowerBox = new JLabel("");
lblLowerBox.setIcon(new ImageIcon("lowerbox.png"));
lblLowerBox.setBounds(130, 349, 487, 208);
this.add(lblLowerBox);
//get the wordcount (of a particular level) rom the WordList class
_wc = _wordlist.getWordCount(_level);
//Choosing the number of quiz depending on the word count
String tts = "";
if(_wc<_maxNum){
_testNum=_wc;
tts="Spell word 1 of "+_wc+": ";
lblPleaseSpellWord.setText(tts);
}else{
_testNum = _maxNum;
tts="Spell word 1 of "+_maxNum+": ";
lblPleaseSpellWord.setText(tts);
}
lblAcc.setText("Accuracy: 0" +"/"+_testNum); //set up the accuracy level
btnListenAgain.addActionListener(this);
btnSubmit.addActionListener(this);
_frame.getRootPane().setDefaultButton(btnSubmit); //make submit default button
//Word to be tested
setTestList(_wordlist);
festival("Spell " + _testList.get(_testNo-1).toString(), null);
}
public void actionPerformed(ActionEvent e) {
//Getting the word that user wrote
String word = textField.getText();
try{
//If user pressed speak button, the word
//is spoken by festival.
JButton button = (JButton) e.getSource();
if (button.equals(btnListenAgain)){
festival(_testList.get(_testNo-1).toString(), btnListenAgain);
return;
}
//If user is correct
if(_testList.get(_testNo-1).toString().equalsIgnoreCase(word)){
//Showing and telling correct message
Sound sound = new Sound("cheering.wav"); //plays a cheering sound
sound.play();
_testList.get(_testNo -1).increaseCorrect();
//update accuracy and streak
_attempts++;
_testNo++;
_correct++;
_streak++;
//get 50 coins if streak > 5, 20 coins if streak > 2
if (_streak >5) {
_coins+=50;
_hiddenCoins+=50;
lblStreak.setText("+50");
lblCorrect.setText("<html>Correct! Very Nice.</html>");
} else if (_streak > 2) {
_coins+=20;
_hiddenCoins+=20;
lblStreak.setText("+20");
lblCorrect.setText("<html>Correct! Well Done! Keep Going.</html>");
} else {
_coins+=10;
_hiddenCoins+=10;
lblStreak.setText("+10");
lblCorrect.setText("<html>Correct! <br>Awesome, you're on a roll!</html>");
}
blink(); //flash 'Streak' label text
updateCoins();
lblCoin.setText("Coins: "+ _coins);
_correctness = "Correct!!";
//Setting the new label
lblPleaseSpellWord.setText("Spell word "+(_testNo)+" of "+_maxNum+": ");
if(_wc<_maxNum)
lblPleaseSpellWord.setText("Spell word "+(_testNo)+" of "+ _wc+": ");
incorrect =0;
//If user gets incorrect
}else{
lblStreak.setText("");
//If second time failing
if(incorrect<1){
//Setting message to the user about the fault
lblCorrect.setText("Incorrect. Try again");
festival("Incorrect!! Spell"+_testList.get(_testNo-1).toString()+".", btnSubmit);
//Word is spoken again.
incorrect++;
textField.setText("");
return;
//First time failing
}else{
//Result message to user
lblCorrect.setText("Incorrect");
//increase test number and fail value
_attempts++;
_fails++;
_testList.get(_testNo -1).increaseFails();
_streak = 0; // streak resets
//Changing field as needed
_testNo++;
incorrect =0;
_correctness = "Incorrect";
//Setting new label for new quiz
lblPleaseSpellWord.setText("Spell word "+(_testNo)+" of "+_maxNum+": ");
if(_wc<_maxNum)
lblPleaseSpellWord.setText("Spell word "+(_testNo)+" of "+ _wc+": ");
}
}
//updating the values.
lblAcc.setText("Accuracy:" + _correct+"/"+_testNum);
updateAccuracy();
//Clearing the Jtext field
textField.setText("");
//If test is finished
if((_testNo==_maxNum+1)||(_wc<_testNo)){
//Telling the user the teset is finished
lblCorrect.setText(_correctness+" Quiz Finished!!");
festival(lblCorrect.getText(), null);
//Update high score
if (_correct > _highScore) {
_highScore = _correct;
updateAccuracy();
}
//opens options menu where user can choose their next action.
SubMenu sub = new SubMenu(_wordlist,_level,_correct,_testNo-1, _frame , _maxNum, _file, _sound);
_frame.getContentPane().add(sub);
this.setVisible(false);
sub.setVisible(true);
}else{
//Continue the quiz
festival(_correctness+" Spell "+_testList.get(_testNo-1).toString()+".", btnSubmit);
}
}catch(Exception excep){
excep.printStackTrace();
}
}
//Function that allows user to change the voice of festival.
private void getVoices() throws Exception{
Festival f = new Festival("","1");
_voices = f.listOfVoices();
}
//method to change the voice.
private void changeVoice(String voice) throws IOException{
File failed = new File(".festivalrc");
//If file does not exist, create new file
if(!failed.exists()) {
failed.createNewFile();
}
//Appending the word to the file
Writer output;
output = new BufferedWriter(new FileWriter(failed,false));
output.append("(set! voice_default '"+voice +")");
output.close();
}
//Method that uses festival to speak out the string passed into it
private void festival(String tts, JButton btn) throws Exception{
if (btn != null) {
btn.setEnabled(false);
}
Festival say = new Festival(tts,_voice, btn);
say.execute();
}
/*
//Speaking out the spelling of the word passed into it
private void festivalAlphabet(String tts) throws Exception{
String word="";
String[] alpha = tts.split("");
for(int i=0;i<alpha.length;i++)
word = word + alpha[i]+" ";
festival(word);
}*/
//method makes the list of words to be tested
private void setTestList(WordList wordlist) throws IOException{
_testList = wordlist.createTestList(_level,_maxNum);
}
//Obtains the accuracy values from the save file (for the current quiz level)
// then add the levels to the corresponding fields
private void getAccuracy() throws IOException {
File accuracy = new File("." + _fileName + "_" + _level);
if (! accuracy.exists()) {
accuracy.createNewFile();
} else {
FileReader fr = new FileReader(accuracy);
BufferedReader br = new BufferedReader(fr);
String str;
str = br.readLine();
_attempts = Integer.parseInt(str);
str = br.readLine();
_fails = Integer.parseInt(str);
str = br.readLine();
_highScore = Integer.parseInt(str);
}
}
//obtains the number of coins the user has from the save file
private void getCoins() throws IOException{
File file = new File(".coinSave");
if (! file.exists()) {
file.createNewFile();
} else {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String str;
str = br.readLine();
_hiddenCoins = Integer.parseInt(str);
}
}
//Writes the value from the hiddenCoin filed to the coin save file
private void updateCoins() {
File file = new File(".coinSave");
PrintWriter pw;
try {
pw = new PrintWriter(file);
pw.close();
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(_hiddenCoins + "\n");
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//Write the values from the accuracy fields (_attempts, _fails) to the corresponding
// save files
private void updateAccuracy() throws IOException {
File accuracy = new File("." + _fileName + "_" + _level);
PrintWriter pw = new PrintWriter(accuracy);
pw.close();
FileWriter fw = new FileWriter(accuracy);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(_attempts + "\n");
bw.write(_fails + "\n");
bw.write(_highScore + "\n");
bw.close();
}
//makes the streak label flash several times
private void blink() {
_timer = new Timer(50, new TimerListener());
_timer.start();
}
// this is an action listener for the timer. Flashes should stop
// after 18 'flashes'
public class TimerListener implements ActionListener {
int count = 0;
@Override
public void actionPerformed(ActionEvent e) {
if (count == 18) {
lblStreak.setText("");
_timer.stop();
}
else if(count % 2 == 0) {
lblStreak.setForeground(new Color(255,0,0));
}
else {
lblStreak.setForeground(new Color(0,0,255));
}
count++;
}
}
/**
* The class implements a mini settings dialog box
* It allows the user to control the background music as well
* as the ability to change the voice
*
*/
public class Settings extends JDialog {
private JPanel contentPane;
/**
* Create the frame.
*/
public Settings(Window owner, String title, Dialog.ModalityType modType) {
super(owner, title, modType);
//set up the GUI
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 500, 230);
setResizable(false);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.setBackground(new Color(255,255,152));
JLabel lblChangeVoice = new JLabel("Change Voice");
lblChangeVoice.setFont(new Font("Arial", Font.PLAIN, 18));
lblChangeVoice.setBounds(25, 130, 135, 24);
contentPane.add(lblChangeVoice);
String[] voices = {"New Zealander","American", "British"}; //get the list of voices installed on users computer
final JComboBox<String> comboBox = new JComboBox<String>(voices);
comboBox.setBounds(220, 125, 210, 30);
contentPane.add(comboBox);
JLabel lblSettings = new JLabel("Settings");
lblSettings.setHorizontalAlignment(SwingConstants.CENTER);
lblSettings.setFont(new Font("Calibri Light", Font.PLAIN, 25));
lblSettings.setBounds(10, 11, 500, 29);
contentPane.add(lblSettings);
JLabel lblMute = new JLabel("Stop Music");
lblMute.setFont(new Font("Arial", Font.PLAIN, 18));
lblMute.setBounds(25, 74, 135, 20);
contentPane.add(lblMute);
//This buttons stops and starts background music
JToggleButton toggleButton = new JToggleButton("");
toggleButton.setIcon(new ImageIcon("mute2.png"));
toggleButton.setBounds(220, 60, 57, 40);
if(! _sound.isActive()) {
toggleButton.setSelected(true);
}
toggleButton.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent ev) {
if(ev.getStateChange()==ItemEvent.SELECTED){
_sound.stop();
} else if(ev.getStateChange()==ItemEvent.DESELECTED){
_sound.loop();
}
}
});
contentPane.add(toggleButton);
//Upon pressing the ok button. The voice is changed.
JButton btnOK = new JButton("Ok");
btnOK.setBackground(new Color(255,255,50));
btnOK.setBorder(new MatteBorder(1,1,1,1, new Color(0,0,0)));
btnOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = (String) comboBox.getItemAt(comboBox.getSelectedIndex());
if (data.equals("New Zealander"))
_voice = _voices.get(0);
else if (data.equals("American"))
_voice = _voices.get(1);
else
_voice = _voices.get(2);
dispose();
}
});
btnOK.setBounds(350, 170, 89, 40);
contentPane.add(btnOK);
}
}
}