-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrainMenu.java
More file actions
680 lines (583 loc) · 19.2 KB
/
TrainMenu.java
File metadata and controls
680 lines (583 loc) · 19.2 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Creates a TrainMenu object. TrainMenu allows the user to practice 25 problems
* and scroll back and forth between problems. The user chooses an answer for as
* many as possible, then clicks finish test, 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 AustinLei
* @version May 23, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: docs.oracle.com for the createImageIcon method
*/
public class TrainMenu
{
/**
* 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;
/**
* The array that holds the 25 problems.
*/
private Problem[] question;
/**
* The array that holds the answers to the 25 problems.
*/
private char[] answers;
/**
* Holds the image for the problem.
*/
private JLabel problemImage;
/**
* Holds the current answer for a problem.
*/
private JLabel curAns;
/**
* Holds the timer, which increments every second.
*/
private JLabel timerDisplay;
/**
* Holds the current problem number.
*/
private int index = 0;
/**
* 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 when the time period for a certain problem started.
*/
private int timeStart;
/**
* Holds the time taken for each problem.
*/
private int[] timePerProblem;
/**
* Denotes whether the finish button has already been clicked; used to show
* answers.
*/
private boolean finished;
/**
* Constructs the TrainMenu object; creates the problem image, timer, back
* button, and buttons to select the answers.
*
* @param problem
* the ProblemDatabase that holds the problems
* @param statistics
* holds the statistics
*/
public TrainMenu( ProblemDatabase problem, Statistics statistics )
{
this.problem = problem;
stats = statistics;
question = new Problem[25];
answers = new char[25];
timePerProblem = new int[25];
boolean notEnoughProblems = true;
index = 0;
for ( int i = 0; i < 25; i++ )
{
question[i] = problem.giveRandProblem();
if ( question[i] == null )
{
notEnoughProblems = false;
}
}
if ( notEnoughProblems )
{
JButton back = new JButton( "Back" );
back.setBounds( 10, 10, 50, 50 );
back.addActionListener( new BackButtonListener() );
JButton choice1 = new JButton( "A" );
choice1.setBounds( 150, 475, 100, 25 );
choice1.addActionListener( new AButtonListener() );
JButton choice2 = new JButton( "B" );
choice2.setBounds( 250, 475, 100, 25 );
choice2.addActionListener( new BButtonListener() );
JButton choice3 = new JButton( "C" );
choice3.setBounds( 350, 475, 100, 25 );
choice3.addActionListener( new CButtonListener() );
JButton choice4 = new JButton( "D" );
choice4.setBounds( 450, 475, 100, 25 );
choice4.addActionListener( new DButtonListener() );
JButton choice5 = new JButton( "E" );
choice5.setBounds( 550, 475, 100, 25 );
choice5.addActionListener( new EButtonListener() );
JButton finish = new JButton( "Finish Test" );
finish.setBounds( 250, 500, 300, 50 );
finish.addActionListener( new FinishButtonListener() );
JButton prevProb = new JButton( "Previous Problem" );
prevProb.setBounds( 175, 425, 150, 50 );
prevProb.addActionListener( new PrevButtonListener() );
JButton nextProb = new JButton( "Next Problem" );
nextProb.setBounds( 475, 425, 150, 50 );
nextProb.addActionListener( new NextButtonListener() );
JButton seeProb = new JButton( "See a Clearer Image" );
seeProb.setBounds( 325, 425, 150, 50 );
seeProb.addActionListener( new SeeButtonListener() );
curAns = new JLabel( "Current Answer: " + answers[index] );
curAns.setBounds( 200, 400, 400, 25 );
curAns.setHorizontalAlignment( JLabel.CENTER );
timerDisplay = new JLabel( "Time Left: 75:00" );
timerDisplay.setFont( new Font( "font", Font.PLAIN, 18 ) );
timerDisplay.setBounds( 630, 10, 150, 50 );
timerDisplay.setHorizontalAlignment( JLabel.CENTER );
frame = new JFrame( "Training 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( finish );
c.add( prevProb );
c.add( nextProb );
c.add( seeProb );
c.add( curAns );
c.add( timerDisplay );
ImageIcon icon = createImageIcon( question[0].getProblemImage(), "image" );
Image image = icon.getImage();
Image newImage = image.getScaledInstance( 680, 300, Image.SCALE_DEFAULT );
icon.setImage( newImage );
int number = index + 1;
problemImage = new JLabel( "Problem " + number + " - " + question[0].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();
}
else
{
JButton back = new JButton( "Back" );
back.setBounds( 10, 10, 50, 50 );
back.addActionListener( new BackButtonListener() );
JLabel finish = new JLabel( "You need at least 25 problems." );
finish.setFont( new Font( "font", Font.PLAIN, 36 ) );
finish.setBounds( 60, 60, 680, 340 );
finish.setHorizontalAlignment( JLabel.CENTER );
frame = new JFrame( "Training Menu" );
frame.setDefaultCloseOperation( 0 );
Container c = frame.getContentPane();
frame.setLayout( null );
frame.setBounds( 0, 0, 800, 600 );
c.add( back );
c.add( finish );
frame.setResizable( false );
frame.setVisible( true );
}
}
/**
* Updates the frame to show the current problem given by the index and to
* show the current answer (if finished button not clicks) or the correct
* answer (if finished button is clicked).
*/
private void update()
{
ImageIcon icon = createImageIcon( question[index].getProblemImage(), "image1" );
Image image = icon.getImage();
Image newImage = image.getScaledInstance( 680, 300, Image.SCALE_DEFAULT );
icon.setImage( newImage );
int number = index + 1;
problemImage.setText( "Problem " + number + " - " + question[index].getName() );
problemImage.setIcon( icon );
problemImage.setVerticalTextPosition( JLabel.BOTTOM );
problemImage.setHorizontalTextPosition( JLabel.CENTER );
if ( !finished )
{
curAns.setText( "Current Answer: " + answers[index] );
}
else
{
curAns.setText( "Answer: " + question[index].getAnswer() );
}
timeStart = timerCount;
}
/**
* Updates the frame when the finish button is clicked; removes the buttons
* to enter an answer, allowing the user only to scroll between problems to
* see the solutions. Also updates scores/times in statistics.
*/
private void updateFinish()
{
int score = 0;
timer.stop();
finished = true;
timePerProblem[index] += timerCount - timeStart;
for ( int i = 0; i < 25; i++ )
{
if ( question[i].getAnswer() == answers[i] )
{
score++;
stats.addScore( 1, 1, question[i].getType() );
}
else
{
stats.addScore( 0, 1, question[i].getType() );
}
stats.addTime( question[i].getType(), timePerProblem[i] );
}
frame.dispose();
JLabel numCorrect = new JLabel( "Score: " + score + "/25" );
numCorrect.setBounds( 350, 400, 100, 50 );
numCorrect.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, 500, 150, 50 );
prevProb.addActionListener( new PrevButtonListener() );
JButton nextProb = new JButton( "Next Problem" );
nextProb.setBounds( 400, 500, 150, 50 );
nextProb.addActionListener( new NextButtonListener() );
JButton viewSol = new JButton( "See Solution" );
viewSol.setBounds( 300, 450, 200, 50 );
viewSol.addActionListener( new ViewButtonListener() );
curAns.setHorizontalAlignment( JLabel.CENTER );
curAns.setBounds( 200, 390, 400, 25 );
frame = new JFrame( "Training Menu" );
frame.setDefaultCloseOperation( 0 );
Container c = frame.getContentPane();
frame.setLayout( null );
frame.setBounds( 0, 0, 800, 600 );
index = 0;
update();
c.add( back );
c.add( prevProb );
c.add( nextProb );
c.add( viewSol );
c.add( problemImage );
c.add( numCorrect );
c.add( curAns );
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 numSecondsLeft = 75 * 60 - timerCount;
if ( numSecondsLeft < 0 )
{
updateFinish();
}
int seconds = numSecondsLeft % 60;
if ( seconds < 10 )
{
timerDisplay.setText( "Time Left: " + numSecondsLeft / 60 + ":0" + seconds );
}
else
{
timerDisplay.setText( "Time Left: " + numSecondsLeft / 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 and moves to the next problem, if not at the
* last problem. If at last problem, does not move to any problem.
*
* @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 )
{
answers[index] = 'A';
timePerProblem[index] += timerCount - timeStart;
if ( index < 24 )
{
index++;
}
update();
}
}
/**
* Handles when the B button is clicked; when clicked, sets the answer to
* the current problem to B and moves to the next problem, if not at the
* last problem. If at last problem, does not move to any problem.
*
* @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 )
{
answers[index] = 'B';
timePerProblem[index] += timerCount - timeStart;
if ( index < 24 )
{
index++;
}
update();
}
}
/**
* Handles when the C button is clicked; when clicked, sets the answer to
* the current problem to C and moves to the next problem, if not at the
* last problem. If at last problem, does not move to any problem.
*
* @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 )
{
answers[index] = 'C';
timePerProblem[index] += timerCount - timeStart;
if ( index < 24 )
{
index++;
}
update();
}
}
/**
* Handles when the D button is clicked; when clicked, sets the answer to
* the current problem to D and moves to the next problem, if not at the
* last problem. If at last problem, does not move to any problem.
*
* @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 )
{
answers[index] = 'D';
timePerProblem[index] += timerCount - timeStart;
if ( index < 24 )
{
index++;
}
update();
}
}
/**
* Handles when the E button is clicked; when clicked, sets the answer to
* the current problem to E and moves to the next problem, if not at the
* last problem. If at last problem, does not move to any problem.
*
* @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 )
{
answers[index] = 'E';
timePerProblem[index] += timerCount - timeStart;
if ( index < 24 )
{
index++;
}
update();
}
}
/**
* Handles when the previous button is clicked; when clicked, moves to the
* previous problem, 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 )
{
timePerProblem[index] += timerCount - timeStart;
if ( index != 0 )
{
index--;
update();
}
}
}
/**
* Handles when the next button is clicked; when clicked, moves to the next
* problem, 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 )
{
timePerProblem[index] += timerCount - timeStart;
if ( index != 24 )
{
index++;
update();
}
}
}
/**
* 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( question[index], false );
}
}
/**
* Handles when the Finish button is clicked; when clicked, calls
* updateFinish().
*
* @author Austin Lei
* @version May 22, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
private class FinishButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
updateFinish();
}
}
/**
* 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( question[index], 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;
}
}
}