-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddMenu.java
More file actions
657 lines (576 loc) · 20.5 KB
/
AddMenu.java
File metadata and controls
657 lines (576 loc) · 20.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
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
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;
/**
* Allows the user to add problems, checking each field to ensure that all
* fields are completed and the file pathnames are valid. It also ensures that
* no two problems are added with the same name.
*
* @author Krishnakumar Bhattaram
* @version May 19, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
public class AddMenu
{
/**
* Holds the GUI elements
*/
private JFrame frame;
/**
* The problem database for this set
*/
private ProblemDatabase problem;
/**
* The text field for the problem pathname
*/
private JTextField problempathfield;
/**
* The text field for the solution pathname
*/
private JTextField solutionpathfield;
/**
* The text field for the problem name
*/
private JTextField namepathfield;
/**
* Type of problem to add chosen by the buttons
*/
private int type;
/**
* The button to select problem type as kinematics
*/
private JButton kinematics;
/**
* The button to select problem type as newton's laws
*/
private JButton newton;
/**
* The button to select problem type as energy
*/
private JButton energy;
/**
* The button to select problem type as rotation
*/
private JButton rotation;
/**
* The button to select problem type as gravitation
*/
private JButton gravitation;
/**
* The button to select problem type as fluids
*/
private JButton fluids;
/**
* The button to select problem solution as 'a'
*/
private JButton ap;
/**
* The button to select problem solution as 'b'
*/
private JButton bp;
/**
* The button to select problem solution as 'c'
*/
private JButton cp;
/**
* The button to select problem solution as 'd'
*/
private JButton dp;
/**
* The button to select problem solution as 'e'
*/
private JButton ep;
/**
* The answer as chosen by the buttons
*/
private char ans;
/**
* The container of the frame
*/
private Container c;
/**
* The text label to warn that the fields are not completed
*/
private JLabel warntxt;
/**
* The text label to indicate that a new problem was added
*/
private JLabel newtxt;
/**
* The text label to warn that the problem name is already used
*/
private JLabel existtxt;
/**
* The text label to warn that the problem pathname doesn't exist
*/
private JLabel probnotfoundtxt;
/**
* The text label to warn that the solution pathname doesn't exist
*/
private JLabel solnotfoundtxt;
/**
* The statistics from the menu
*/
private Statistics stats;
/**
* The constructor, which makes visible the frame and adds the buttons and
* text fields
*
* @param problem
* The problem database for this set
* @param statistics
* The statistics for this set
*/
public AddMenu( ProblemDatabase problem, Statistics statistics )
{
stats = statistics;
ans = 'h';
type = -1;
this.problem = problem;
JButton back = new JButton( "Back" );
back.setBounds( 10, 10, 50, 50 );
back.addActionListener( new BackButtonListener() );
JLabel problemtxt = new JLabel( "Problem Image Pathname" );
problemtxt.setFont( new Font( "font", Font.PLAIN, 10 ) );
problemtxt.setBounds( 200, 180, 500, 20 );
problemtxt.setHorizontalAlignment( JLabel.LEFT );
problemtxt.setVerticalAlignment( JLabel.TOP );
problempathfield = new JTextField();
problempathfield.setBounds( 200, 200, 400, 20 );
JButton chooseproblem = new JButton( "Choose File" );
chooseproblem.setBounds( 600, 200, 100, 20 );
chooseproblem.addActionListener( new ProblemFileListener() );
JLabel solutiontxt = new JLabel( "Solution Image Pathname" );
solutiontxt.setFont( new Font( "font", Font.PLAIN, 10 ) );
solutiontxt.setBounds( 200, 220, 500, 20 );
solutiontxt.setHorizontalAlignment( JLabel.LEFT );
solutiontxt.setVerticalAlignment( JLabel.TOP );
solutionpathfield = new JTextField();
solutionpathfield.setBounds( 200, 240, 400, 20 );
JButton choosesolution = new JButton( "Choose File" );
choosesolution.setBounds( 600, 240, 100, 20 );
choosesolution.addActionListener( new SolutionFileListener() );
JLabel nametxt = new JLabel( "Problem Name" );
nametxt.setFont( new Font( "font", Font.PLAIN, 10 ) );
nametxt.setBounds( 200, 260, 500, 20 );
nametxt.setHorizontalAlignment( JLabel.LEFT );
nametxt.setVerticalAlignment( JLabel.TOP );
namepathfield = new JTextField();
namepathfield.setBounds( 200, 280, 400, 20 );
JLabel typetxt = new JLabel( "Choose Problem Type" );
typetxt.setFont( new Font( "font", Font.PLAIN, 10 ) );
typetxt.setBounds( 200, 300, 500, 20 );
typetxt.setHorizontalAlignment( JLabel.LEFT );
typetxt.setVerticalAlignment( JLabel.TOP );
kinematics = new JButton( "Kinematics" );
kinematics.setBounds( 100, 320, 100, 20 );
kinematics.addActionListener( new typeListener() );
newton = new JButton( "Newton's Laws" );
newton.setBounds( 200, 320, 100, 20 );
newton.addActionListener( new typeListener() );
energy = new JButton( "Energy" );
energy.setBounds( 300, 320, 100, 20 );
energy.addActionListener( new typeListener() );
rotation = new JButton( "Rotation" );
rotation.setBounds( 400, 320, 100, 20 );
rotation.addActionListener( new typeListener() );
gravitation = new JButton( "Gravitation" );
gravitation.setBounds( 500, 320, 100, 20 );
gravitation.addActionListener( new typeListener() );
fluids = new JButton( "Fluids" );
fluids.setBounds( 600, 320, 100, 20 );
fluids.addActionListener( new typeListener() );
JLabel answertxt = new JLabel( "Choose Answer:" );
answertxt.setFont( new Font( "font", Font.PLAIN, 10 ) );
answertxt.setBounds( 200, 345, 500, 20 );
answertxt.setHorizontalAlignment( JLabel.LEFT );
answertxt.setVerticalAlignment( JLabel.TOP );
ap = new JButton( "A" );
ap.setBounds( 200, 365, 80, 20 );
ap.addActionListener( new answerListener() );
bp = new JButton( "B" );
bp.setBounds( 280, 365, 80, 20 );
bp.addActionListener( new answerListener() );
cp = new JButton( "C" );
cp.setBounds( 360, 365, 80, 20 );
cp.addActionListener( new answerListener() );
dp = new JButton( "D" );
dp.setBounds( 440, 365, 80, 20 );
dp.addActionListener( new answerListener() );
ep = new JButton( "E" );
ep.setBounds( 520, 365, 80, 20 );
ep.addActionListener( new answerListener() );
JButton pathname = new JButton( "Enter" );
pathname.setBounds( 300, 400, 200, 60 );
pathname.addActionListener( new PathnameListener() );
JLabel text = new JLabel( "Add Problems" );
text.setFont( new Font( "font", Font.PLAIN, 30 ) );
text.setHorizontalAlignment( JLabel.CENTER );
text.setBounds( 100, 30, 600, 100 );
frame = new JFrame( "Fîzîk" );
frame.setDefaultCloseOperation( 0 );
c = frame.getContentPane();
frame.setLayout( null );
frame.setBounds( 0, 0, 800, 600 );
warntxt = new JLabel( "Please complete all required fields!" );
warntxt.setFont( new Font( "font", Font.PLAIN, 15 ) );
warntxt.setBounds( 200, 500, 500, 20 );
warntxt.setHorizontalAlignment( JLabel.LEFT );
warntxt.setVerticalAlignment( JLabel.TOP );
warntxt.setVisible( false );
newtxt = new JLabel( "Success! Enter a new problem or return to main menu." );
newtxt.setFont( new Font( "font", Font.PLAIN, 15 ) );
newtxt.setBounds( 200, 500, 500, 20 );
newtxt.setHorizontalAlignment( JLabel.LEFT );
newtxt.setVerticalAlignment( JLabel.TOP );
newtxt.setVisible( false );
existtxt = new JLabel( "Another problem already has this name!" );
existtxt.setFont( new Font( "font", Font.PLAIN, 15 ) );
existtxt.setBounds( 200, 500, 500, 20 );
existtxt.setHorizontalAlignment( JLabel.LEFT );
existtxt.setVerticalAlignment( JLabel.TOP );
existtxt.setVisible( false );
probnotfoundtxt = new JLabel( "Problem file not found! Please check the fields" );
probnotfoundtxt.setFont( new Font( "font", Font.PLAIN, 15 ) );
probnotfoundtxt.setBounds( 200, 500, 500, 20 );
probnotfoundtxt.setHorizontalAlignment( JLabel.LEFT );
probnotfoundtxt.setVerticalAlignment( JLabel.TOP );
probnotfoundtxt.setVisible( false );
solnotfoundtxt = new JLabel( "Solution file not found! Please check the fields" );
solnotfoundtxt.setFont( new Font( "font", Font.PLAIN, 15 ) );
solnotfoundtxt.setBounds( 200, 500, 500, 20 );
solnotfoundtxt.setHorizontalAlignment( JLabel.LEFT );
solnotfoundtxt.setVerticalAlignment( JLabel.TOP );
solnotfoundtxt.setVisible( false );
c.add( back );
c.add( text );
c.add( pathname );
c.add( warntxt );
c.add( newtxt );
c.add( solnotfoundtxt );
c.add( existtxt );
c.add( probnotfoundtxt );
c.add( problempathfield );
c.add( problemtxt );
c.add( chooseproblem );
c.add( solutionpathfield );
c.add( solutiontxt );
c.add( choosesolution );
c.add( namepathfield );
c.add( nametxt );
c.add( typetxt );
c.add( kinematics );
c.add( newton );
c.add( energy );
c.add( rotation );
c.add( gravitation );
c.add( fluids );
c.add( answertxt );
c.add( ap );
c.add( bp );
c.add( cp );
c.add( dp );
c.add( ep );
frame.setResizable( false );
frame.setVisible( true );
}
/**
* 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 19, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
private class BackButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
MainMenu mainMenu = new MainMenu( problem, stats );
frame.dispose();
}
}
/**
* Handles when the enter button is clicked; when clicked, checks if all
* conditions are satisfied (if all fields are entered and all pathnames
* exist) and displays the appropriate warnings/successes
*
* @author Krishnakumar Bhattaram
* @version May 19, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
private class PathnameListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
if ( problempathfield.getText() == "" || solutionpathfield.getText() == ""
|| namepathfield.getText() == "" || ans == 'h' || type == -1 )
{
newtxt.setVisible( false );
warntxt.setVisible( true );
existtxt.setVisible( false );
probnotfoundtxt.setVisible( false );
solnotfoundtxt.setVisible( false );
return;
}
String pathproblems = problempathfield.getText();
String solutionpath = solutionpathfield.getText();
try
{
Scanner readIn = new Scanner( new File( pathproblems ) );
}
catch ( FileNotFoundException exc )
{
newtxt.setVisible( false );
warntxt.setVisible( false );
existtxt.setVisible( false );
probnotfoundtxt.setVisible( true );
solnotfoundtxt.setVisible( false );
return;
}
try
{
Scanner readIn = new Scanner( new File( solutionpath ) );
}
catch ( FileNotFoundException exc )
{
newtxt.setVisible( false );
warntxt.setVisible( false );
existtxt.setVisible( false );
probnotfoundtxt.setVisible( false );
solnotfoundtxt.setVisible( true );
return;
}
if ( !problem.addProblem( new Problem( problempathfield
.getText(), solutionpathfield.getText(), namepathfield.getText(), type, ans ) ) )
{
newtxt.setVisible( false );
warntxt.setVisible( false );
existtxt.setVisible( true );
probnotfoundtxt.setVisible( false );
solnotfoundtxt.setVisible( false );
}
else
{
problempathfield.setText( "" );
solutionpathfield.setText( "" );
namepathfield.setText( "" );
kinematics.setEnabled( true );
newton.setEnabled( true );
energy.setEnabled( true );
rotation.setEnabled( true );
gravitation.setEnabled( true );
fluids.setEnabled( true );
ap.setEnabled( true );
bp.setEnabled( true );
cp.setEnabled( true );
dp.setEnabled( true );
ep.setEnabled( true );
ans = 'h';
type = -1;
warntxt.setVisible( false );
existtxt.setVisible( false );
newtxt.setVisible( true );
probnotfoundtxt.setVisible( false );
solnotfoundtxt.setVisible( false );
}
}
}
/**
* Handles when one of the type buttons (ex. kinematics, energy, etc.) are
* clicked; when clicked, changes the type field to match the topic chosen
* and disables the chosen type button while enabling all other type
* buttons.
*
* @author Krishnakumar Bhattaram
* @version May 24, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
private class typeListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
JButton clicked = (JButton)e.getSource();
String name = clicked.getText();
kinematics.setEnabled( true );
newton.setEnabled( true );
energy.setEnabled( true );
rotation.setEnabled( true );
gravitation.setEnabled( true );
fluids.setEnabled( true );
if ( name == null )
{
warntxt.setVisible( true );
newtxt.setVisible( false );
System.out.println( "Fatal Error!" );
return;
}
if ( name.equalsIgnoreCase( "Kinematics" ) )
{
kinematics.setEnabled( false );
type = 0;
}
else if ( name.equalsIgnoreCase( "Newton's Laws" ) )
{
newton.setEnabled( false );
type = 1;
}
else if ( name.equalsIgnoreCase( "Energy" ) )
{
energy.setEnabled( false );
type = 2;
}
else if ( name.equalsIgnoreCase( "Rotation" ) )
{
rotation.setEnabled( false );
type = 3;
}
else if ( name.equalsIgnoreCase( "Gravitation" ) )
{
gravitation.setEnabled( false );
type = 4;
}
else if ( name.equalsIgnoreCase( "Fluids" ) )
{
fluids.setEnabled( false );
type = 5;
}
}
}
/**
* Handles when one of the answer buttons (ex. 'A', 'B', etc.) are clicked;
* when clicked, changes the answer field to match the topic chosen and
* disables the chosen answer button while enabling all other answer
* buttons.
*
* @author Krishnakumar Bhattaram
* @version May 24, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
private class answerListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
JButton clicked = (JButton)e.getSource();
String name = clicked.getText();
ap.setEnabled( true );
bp.setEnabled( true );
cp.setEnabled( true );
dp.setEnabled( true );
ep.setEnabled( true );
if ( name == null )
{
warntxt.setVisible( true );
newtxt.setVisible( false );
System.out.println( "Fatal Error!" );
return;
}
if ( name.equalsIgnoreCase( "A" ) )
{
ap.setEnabled( false );
ans = 'A';
}
else if ( name.equalsIgnoreCase( "B" ) )
{
bp.setEnabled( false );
ans = 'B';
}
else if ( name.equalsIgnoreCase( "C" ) )
{
cp.setEnabled( false );
ans = 'C';
}
else if ( name.equalsIgnoreCase( "D" ) )
{
dp.setEnabled( false );
ans = 'D';
}
else if ( name.equalsIgnoreCase( "E" ) )
{
ep.setEnabled( false );
ans = 'E';
}
}
}
/**
* Opens the system's file explorer to allow selection of the directory
* which contains the image files and problem set. Only allows selection of
* PNG files.
*
* @author Krishnakumar Bhattaram
* @version May 23, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources:
* https://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html
*/
private class ProblemFileListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
JFileChooser filechooser = new JFileChooser(
FileSystemView.getFileSystemView().getHomeDirectory() );
filechooser.setDialogTitle( "Choose Problem File" );
filechooser.setAcceptAllFileFilterUsed( false );
FileNameExtensionFilter types = new FileNameExtensionFilter( "PNG images", "png" );
filechooser.addChoosableFileFilter( types );
int ret = filechooser.showOpenDialog( null );
if ( ret == JFileChooser.APPROVE_OPTION )
{
File file = filechooser.getSelectedFile();
problempathfield.setText( file.getAbsolutePath() );
}
}
}
/**
* Opens the system's file explorer to allow selection of the directory
* which contains the image files and problem set. Only allows selection of
* PNG files.
*
* @author Krishnakumar Bhattaram
* @version May 23, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources:
* https://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html
*/
private class SolutionFileListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
JFileChooser filechooser = new JFileChooser(
FileSystemView.getFileSystemView().getHomeDirectory() );
filechooser.setDialogTitle( "Choose Solution File" );
filechooser.setAcceptAllFileFilterUsed( false );
FileNameExtensionFilter types = new FileNameExtensionFilter( "PNG images", "png" );
filechooser.addChoosableFileFilter( types );
int ret = filechooser.showOpenDialog( null );
if ( ret == JFileChooser.APPROVE_OPTION )
{
File file = filechooser.getSelectedFile();
solutionpathfield.setText( file.getAbsolutePath() );
}
}
}
}