-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQQMenu.java
More file actions
688 lines (594 loc) · 20.3 KB
/
QQMenu.java
File metadata and controls
688 lines (594 loc) · 20.3 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
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
/**
* Creates a QQMenu object. QQMenu allows the user to as many problems as
* wanted, one at a time, and scroll back and forth between problems, of a
* specific topic or all topics. The user clicks an answer choice for a problem,
* which allows the user see their score, scroll back and forth between
* problems, and see solutions for problems. At any time, the user can go back
* to MainMenu with the back button. This class is called from MainMenu.
*
* @author Austin Lei
* @version May 23, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
public class QQMenu
{
/**
* Holds all of the GUI elements.
*/
private JFrame frame;
/**
* The ProblemDatabase to get all of the problems.
*/
private ProblemDatabase problem;
/**
* Holds all of the problem statistics for QuickQuiz.
*/
private Statistics stats;
/**
* Holds all of the problems to give when the back button is clicked.
*/
private Stack<Problem> backStack;
/**
* Holds all of the problems to give when the forward button is clicked.
*/
private Stack<Problem> forwardStack;
/**
* Holds the image for the problem.
*/
private JLabel problemImage;
/**
* Holds the timer, which increments every second.
*/
private JLabel timerDisplay;
/**
* Displays the score, updates every time score changes.
*/
private JLabel scoreDisplay;
/**
* Handles the timing, making sure the timer increments every second.
*/
private Timer timer;
/**
* Holds the number of seconds in the timer.
*/
private int timerCount;
/**
* Holds the score; 100 points for problems solved correctly in less than 3
* minutes, and 50 points for problems solved correctly after 3 minutes.
*/
private int totalScore;
/**
* Holds the type of problems to do training; -1 denotes all types of
* problems.
*/
private int problemType;
/**
* Constructs the QQMenu; takes in the problemDatabase, Statistics, and type
* of problem, and calls updateProblem based on these parameters.
*
* @param problem
* the problemDatabase that holds all of the problems
* @param type
* the type of problem to do; -1 denotes all types
* @param statistics
* holds the statistics about the solved problems
*/
public QQMenu( ProblemDatabase problem, int type, Statistics statistics )
{
this.problem = problem;
backStack = new Stack<Problem>();
forwardStack = new Stack<Problem>();
problemType = type;
stats = statistics;
updateProblem();
}
/**
* Restarts the timer and calls a new random problem of a certain type; if
* no more problems, shows that no more problems exist and allows user to
* exit to MainMenu through the back button. If problem exists, changes the
* display to show the problem, shows the answer choice buttons, and creates
* a back button for user go back to MainMenu.
*/
private void updateProblem()
{
while ( !forwardStack.isEmpty() )
{
backStack.push( forwardStack.pop() );
}
if ( frame != null )
{
frame.dispose();
}
Problem prb;
if ( problemType == -1 )
{
prb = problem.giveRandProblem();
}
else
{
prb = problem.giveRandProblem( problemType );
}
if ( prb == null )
{
updateFinish();
}
else
{
forwardStack.push( prb );
JButton back = new JButton( "Back" );
back.setBounds( 10, 10, 50, 50 );
back.addActionListener( new BackButtonListener() );
JButton choice1 = new JButton( "A" );
choice1.setBounds( 150, 500, 100, 50 );
choice1.addActionListener( new AButtonListener() );
JButton choice2 = new JButton( "B" );
choice2.setBounds( 250, 500, 100, 50 );
choice2.addActionListener( new BButtonListener() );
JButton choice3 = new JButton( "C" );
choice3.setBounds( 350, 500, 100, 50 );
choice3.addActionListener( new CButtonListener() );
JButton choice4 = new JButton( "D" );
choice4.setBounds( 450, 500, 100, 50 );
choice4.addActionListener( new DButtonListener() );
JButton choice5 = new JButton( "E" );
choice5.setBounds( 550, 500, 100, 50 );
choice5.addActionListener( new EButtonListener() );
JButton seeProb = new JButton( "See a Clearer Image" );
seeProb.setBounds( 325, 450, 150, 50 );
seeProb.addActionListener( new SeeButtonListener() );
timerDisplay = new JLabel( "Time: 0:00" );
timerDisplay.setFont( new Font( "font", Font.PLAIN, 18 ) );
timerDisplay.setBounds( 650, 10, 150, 50 );
timerDisplay.setHorizontalAlignment( JLabel.CENTER );
scoreDisplay = new JLabel( "Score: " + totalScore );
scoreDisplay.setBounds( 300, 400, 200, 50 );
scoreDisplay.setHorizontalAlignment( JLabel.CENTER );
frame = new JFrame( "Quick Quiz Menu" );
frame.setDefaultCloseOperation( 0 );
Container c = frame.getContentPane();
frame.setLayout( null );
frame.setBounds( 0, 0, 800, 600 );
c.add( back );
c.add( choice1 );
c.add( choice2 );
c.add( choice3 );
c.add( choice4 );
c.add( choice5 );
c.add( scoreDisplay );
c.add( timerDisplay );
c.add( seeProb );
ImageIcon icon = createImageIcon( forwardStack.peek().getProblemImage(), "image" );
Image image = icon.getImage();
Image newImage = image.getScaledInstance( 680, 300, Image.SCALE_DEFAULT );
icon.setImage( newImage );
problemImage = new JLabel( "Problem " + ( this.backStack.size() + 1 ) + " - "
+ forwardStack.peek().getName(), icon, JLabel.CENTER );
problemImage.setFont( new Font( "font", Font.PLAIN, 18 ) );
problemImage.setBounds( 60, 60, 680, 340 );
problemImage.setVerticalTextPosition( JLabel.BOTTOM );
problemImage.setHorizontalTextPosition( JLabel.CENTER );
c.add( problemImage );
frame.setResizable( false );
frame.setVisible( true );
TimerListener time = new TimerListener();
timer = new Timer( 1000, time );
timer.start();
}
}
/**
* Allow user to see the solutions page for a certain problem; displays the
* image of the problem, the score, the correct answer, buttons to go back
* and forth between problems, a back button, another button to see the
* solution, and another button to see a new problem. Also adds the time
* used for the problem into statistics.
*
* @param prob
* the problem to display
*/
private void updateSolutionsPage( Problem prob )
{
frame.dispose();
stats.addTime( forwardStack.peek().getType(), timerCount );
timerCount = 0;
scoreDisplay.setText( "Score: " + totalScore );
scoreDisplay.setBounds( 300, 400, 200, 25 );
scoreDisplay.setHorizontalAlignment( JLabel.CENTER );
JButton back = new JButton( "Back" );
back.setBounds( 10, 10, 50, 50 );
back.addActionListener( new BackButtonListener() );
JButton prevProb = new JButton( "Previous Problem" );
prevProb.setBounds( 250, 475, 150, 25 );
prevProb.addActionListener( new PrevButtonListener() );
JButton nextProb = new JButton( "Next Problem" );
nextProb.setBounds( 400, 475, 150, 25 );
nextProb.addActionListener( new NextButtonListener() );
JButton viewSol = new JButton( "See Solution" );
viewSol.setBounds( 300, 450, 200, 25 );
viewSol.addActionListener( new ViewButtonListener() );
JButton continueButton = new JButton( "Continue to New Problem" );
continueButton.setBounds( 300, 500, 200, 50 );
continueButton.addActionListener( new ContinueButtonListener() );
frame = new JFrame( "Quick Quiz Menu" );
frame.setDefaultCloseOperation( 0 );
Container c = frame.getContentPane();
frame.setLayout( null );
frame.setBounds( 0, 0, 800, 600 );
c.add( back );
c.add( viewSol );
c.add( prevProb );
c.add( nextProb );
c.add( continueButton );
c.add( problemImage );
c.add( scoreDisplay );
JLabel answer = new JLabel( "Answer: " + forwardStack.peek().getAnswer() );
answer.setHorizontalAlignment( JLabel.CENTER );
answer.setBounds( 200, 425, 400, 25 );
ImageIcon icon = createImageIcon( forwardStack.peek().getProblemImage(), "image" );
Image image = icon.getImage();
Image newImage = image.getScaledInstance( 680, 300, Image.SCALE_DEFAULT );
icon.setImage( newImage );
problemImage.setText(
"Problem " + ( backStack.size() + 1 ) + " - " + forwardStack.peek().getName() );
problemImage.setIcon( icon );
problemImage.setVerticalTextPosition( JLabel.BOTTOM );
problemImage.setHorizontalTextPosition( JLabel.CENTER );
c.add( answer );
c.add( problemImage );
frame.setVisible( true );
frame.setResizable( false );
}
/**
* Updates the score and records the scores in statistics.
*
* @param correct
* whether the problem was correct or not
* @param time
* the time taken for the problem
*/
private void updateScore( boolean correct, int time )
{
if ( correct )
{
stats.addScore( 1, 1, forwardStack.peek().getType() );
if ( time <= 180 )
{
totalScore += 100;
}
else
{
totalScore += 50;
}
}
else
{
stats.addScore( 0, 1, forwardStack.peek().getType() );
}
}
/**
* Automatically called when all problem of a type exhausted; gives a back
* button for user to return to main menu.
*/
private void updateFinish()
{
if ( frame == null ) // no problems of a single type
{
frame = new JFrame( "Quick Quiz Menu" );
frame.setDefaultCloseOperation( 0 );
Container c = frame.getContentPane();
frame.setLayout( null );
frame.setBounds( 0, 0, 800, 600 );
JButton back = new JButton( "Back" );
back.setBounds( 10, 10, 50, 50 );
back.addActionListener( new BackButtonListener() );
JLabel finish = new JLabel( "There's no problems of this type!" );
finish.setFont( new Font( "font", Font.PLAIN, 36 ) );
finish.setBounds( 60, 60, 680, 340 );
finish.setHorizontalAlignment( JLabel.CENTER );
c.add( back );
c.add( finish );
frame.setVisible( true );
frame.setResizable( false );
}
else
{
frame.dispose();
frame = new JFrame( "Quick Quiz Menu" );
frame.setDefaultCloseOperation( 0 );
Container c = frame.getContentPane();
frame.setLayout( null );
frame.setBounds( 0, 0, 800, 600 );
JButton back = new JButton( "Back" );
back.setBounds( 10, 10, 50, 50 );
back.addActionListener( new BackButtonListener() );
JLabel finish = new JLabel( "That's all of the problems!" );
finish.setFont( new Font( "font", Font.PLAIN, 36 ) );
finish.setBounds( 60, 60, 680, 340 );
finish.setHorizontalAlignment( JLabel.CENTER );
c.add( back );
c.add( finish );
frame.setVisible( true );
frame.setResizable( false );
}
}
/**
* Deals with timing; every second, decrements the timer by one second,
* starting at 75 minutes. When the timer reaches 0, automatically calls
* updateFinish() to allow the user to see the problem solutions.
*
* @author Austin Lei
* @version May 22, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
private class TimerListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
timerCount++;
int seconds = timerCount % 60;
if ( seconds < 10 )
{
timerDisplay.setText( "Time: " + timerCount / 60 + ":0" + seconds );
}
else
{
timerDisplay.setText( "Time: " + timerCount / 60 + ":" + seconds );
}
}
}
/**
* Handles when the back button is clicked; when clicked, returns to
* MainMenu by closing all of TypeMenu and creating a MainMenu object.
*
* @author Austin Lei
* @version May 22, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
private class BackButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
problem.reset();
MainMenu mainMenu = new MainMenu( problem, stats );
frame.dispose();
}
}
/**
* Handles when the A button is clicked; when clicked, sets the answer to
* the current problem to A, updates the score, and updates the page to show
* the solutions.
*
* @author Austin Lei
* @version May 22, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
private class AButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
timer.stop();
updateScore( forwardStack.peek().getAnswer() == 'A', timerCount );
updateSolutionsPage( forwardStack.peek() );
}
}
/**
* Handles when the B button is clicked; when clicked, sets the answer to
* the current problem to B, updates the score, and updates the page to show
* the solutions.
*
* @author Austin Lei
* @version May 22, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
private class BButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
timer.stop();
updateScore( forwardStack.peek().getAnswer() == 'B', timerCount );
updateSolutionsPage( forwardStack.peek() );
}
}
/**
* Handles when the C button is clicked; when clicked, sets the answer to
* the current problem to C, updates the score, and updates the page to show
* the solutions.
*
* @author Austin Lei
* @version May 22, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
private class CButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
timer.stop();
updateScore( forwardStack.peek().getAnswer() == 'C', timerCount );
updateSolutionsPage( forwardStack.peek() );
}
}
/**
* Handles when the D button is clicked; when clicked, sets the answer to
* the current problem to D, updates the score, and updates the page to show
* the solutions.
*
* @author Austin Lei
* @version May 22, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
private class DButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
timer.stop();
updateScore( forwardStack.peek().getAnswer() == 'D', timerCount );
updateSolutionsPage( forwardStack.peek() );
}
}
/**
* Handles when the E button is clicked; when clicked, sets the answer to
* the current problem to E, updates the score, and updates the page to show
* the solutions.
*
* @author Austin Lei
* @version May 22, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
private class EButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
timer.stop();
updateScore( forwardStack.peek().getAnswer() == 'E', timerCount );
updateSolutionsPage( forwardStack.peek() );
}
}
/**
* Handles when the previous button is clicked; when clicked, moves to the
* previous problem and shows its solution, if possible. If not, does
* nothing.
*
* @author Austin Lei
* @version May 22, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
private class PrevButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
if ( !backStack.isEmpty() )
{
forwardStack.push( backStack.pop() );
updateSolutionsPage( forwardStack.peek() );
}
}
}
/**
* Handles when the next button is clicked; when clicked, moves to the next
* problem and shows its solution, if possible. If not, does nothing.
*
* @author Austin Lei
* @version May 22, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
private class NextButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
if ( forwardStack.size() > 1 )
{
backStack.push( forwardStack.pop() );
updateSolutionsPage( forwardStack.peek() );
}
}
}
/**
* Handles when the View Solutions button is clicked; when clicked, creates
* ImageMenu to display the solution image for the problem.
*
* @author Austin Lei
* @version May 23, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
private class ViewButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
ImageMenu sol = new ImageMenu( forwardStack.peek(), false );
}
}
/**
* Handles when the Continue to New Problem button is clicked; when clicked,
* calls updateProblem.
*
* @author Austin Lei
* @version May 22, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
private class ContinueButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
updateProblem();
}
}
/**
* Handles when the See a Clearer Image button is clicked; when clicked,
* creates a ImageMenu object to display a better problem image.
*
* @author Austin Lei
* @version May 23, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
private class SeeButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
ImageMenu menu = new ImageMenu( forwardStack.peek(), true );
}
}
/**
* Method was taken from docs.oracle.com
*
* @param path
* is the pathname of the image
* @param description
* is the name of the image
* @return ImageIcon, a fixed-size picture
*/
protected ImageIcon createImageIcon( String path, String description )
{
if ( path != null )
{
return new ImageIcon( path, description );
}
else
{
System.err.println( "Couldn't find file: " + path );
return null;
}
}
}