-
Notifications
You must be signed in to change notification settings - Fork 0
/
BeatRepeatGUI.java
741 lines (607 loc) · 19.6 KB
/
BeatRepeatGUI.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
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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
package beatRepeatGUI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import round.Grid;
import round.Round;
public class BeatRepeatGUI extends JFrame
{
private static JToggleButton[][] uniqueFreeTileButton;
private static boolean correct;
private static boolean welcomeDone;
private static boolean multipleChoicesDone;
private static boolean freeTileDone;
private static boolean endScreenDone;
private static boolean backToMain = true;
private static boolean tryAgain;
private static final int SCORE_EASY = 50;
private static final int SCORE_MEDIUM = 100;
private static final int SCORE_HARD = 150;
private static final int SCORE_SIZE1 = 50;
private static final int SCORE_SIZE2 = 100;
private static final int SCORE_SIZE3 = 150;
private static final JPanel difficultyButtons = new JPanel(new GridLayout(4, 1));
private static final JPanel sizeButtons = new JPanel(new GridLayout(4, 1));
private static final String EASY = "Easy";
private static final String MEDIUM = "Medium";
private static final String HARD = "Hard";
private static int[] freeTileSelection;
private static int multipleChoiceSelection; //
private static int row;
private static int column;
private static String difficultyMode;
private static Round currentRound;
private static Container welcomeFrame;
private static Container endFrame;
private static Container buttonGridFrame;
private static Container multipleChoicesFrame;
private static Container pictureGridFrame;
private static Container listenFrame;
private static int numberOfScore = 0;
public BeatRepeatGUI()
{
tryAgain = true;
welcomeDone = false;
correct = true;
welcome();
uniqueFreeTileButton = new JToggleButton[row][column];
}
public void welcome()
{
dispose();
welcomeFrame = getContentPane();
welcomeFrame.setLayout(new BorderLayout());
setPreferredSize(new Dimension(500,500));
JLabel titleLabel = new JLabel("Welcome to BeatRepeat", SwingConstants.CENTER);
titleLabel.setFont(new Font("Arial", Font.BOLD, 40));
JLabel difficultyLabel = new JLabel("Difficulty", SwingConstants.CENTER);
difficultyLabel.setFont(new Font("Arial", Font.PLAIN, 20));
difficultyButtons.add(difficultyLabel);
JToggleButton easy = new JToggleButton(EASY);
JToggleButton medium = new JToggleButton(MEDIUM);
JToggleButton hard = new JToggleButton(HARD);
easy.setBackground(Color.WHITE);
medium.setBackground(Color.WHITE);
hard.setBackground(Color.WHITE);
ButtonGroup difficultyButtonGroup = new ButtonGroup();
difficultyButtonGroup.add(easy);
difficultyButtonGroup.add(medium);
difficultyButtonGroup.add(hard);
easy.addActionListener(createDifficultyButtonListener());
medium.addActionListener(createDifficultyButtonListener());
hard.addActionListener(createDifficultyButtonListener());
difficultyButtons.add(easy);
difficultyButtons.add(medium);
difficultyButtons.add(hard);
JLabel sizeLabel = new JLabel("Size", SwingConstants.CENTER);
sizeLabel.setFont(new Font("Arial", Font.PLAIN, 20));
sizeButtons.add(sizeLabel);
JToggleButton size1 = new JToggleButton("3 x 4");
JToggleButton size2 = new JToggleButton("4 x 5");
JToggleButton size3 = new JToggleButton("6 x 6");
size1.setBackground(Color.WHITE);
size2.setBackground(Color.WHITE);
size3.setBackground(Color.WHITE);
ButtonGroup sizeButtonGroup = new ButtonGroup();
sizeButtonGroup.add(size1);
sizeButtonGroup.add(size2);
sizeButtonGroup.add(size3);
size1.addActionListener(createSizeButtonListener());
size2.addActionListener(createSizeButtonListener());
size3.addActionListener(createSizeButtonListener());
sizeButtons.add(size1);
sizeButtons.add(size2);
sizeButtons.add(size3);
JPanel buttons = new JPanel(new GridLayout(1, 2));
buttons.add(difficultyButtons);
buttons.add(sizeButtons);
JButton goButton = new JButton("Go");
goButton.setBackground(Color.GREEN);
welcomeFrame.add(titleLabel, BorderLayout.NORTH);
welcomeFrame.add(buttons, BorderLayout.CENTER);
welcomeFrame.add(goButton, BorderLayout.SOUTH);
goButton.addActionListener(createGoButtonListener());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("BeatRepeat");
pack();
setVisible(true);
while(!welcomeDone)
{
System.out.print("");
//empty
}
}
public static ActionListener createSizeButtonListener()
{
return new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
AbstractButton abstractButton = (AbstractButton)event.getSource();
if(abstractButton.getModel().isSelected())
{
row = Character.getNumericValue(event.getActionCommand().charAt(0));
column = Character.getNumericValue(event.getActionCommand().charAt(4));
}
}
};
}
public static ActionListener createDifficultyButtonListener()
{
return new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
AbstractButton abstractButton = (AbstractButton)event.getSource();
if(abstractButton.getModel().isSelected())
{
difficultyMode = event.getActionCommand();
}
}
};
}
public static ActionListener createGoButtonListener()
{
return new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
welcomeDone = true;
welcomeFrame.removeAll();
}
};
}
public void endScreen()
{
endScreenDone = true;
endFrame = getContentPane();
setPreferredSize(new Dimension(500,500));
endFrame.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 20));
JPanel titlePanel = new JPanel(new BorderLayout());
JLabel titleLabel = new JLabel("BeatRepeat", SwingConstants.CENTER);
titleLabel.setFont(new Font("Arial", Font.BOLD, 40));
JLabel empty = new JLabel(" ");
empty.setFont(new Font("Arial", Font.PLAIN, 40));
JLabel scoreTitle = new JLabel("Your score is: ", SwingConstants.CENTER);
scoreTitle.setFont(new Font("Arial", Font.PLAIN, 25));
titlePanel.add(titleLabel, BorderLayout.NORTH);
titlePanel.add(empty, BorderLayout.CENTER);
titlePanel.add(scoreTitle, BorderLayout.SOUTH);
JLabel scoreLabel = new JLabel("" + numberOfScore, SwingConstants.CENTER);
scoreLabel.setFont(new Font("Arial", Font.PLAIN, 30));
JButton tryAgain = new JButton("Try Again");
JButton mainMenu = new JButton("Main Menu");
endFrame.add(titlePanel, BorderLayout.NORTH);
endFrame.add(scoreLabel, BorderLayout.CENTER);
buttonPanel.add(tryAgain);
buttonPanel.add(mainMenu);
endFrame.add(buttonPanel, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("BeatRepeat");
pack();
setVisible(true);
mainMenu.addActionListener(createBackToMenuButtonListener());
tryAgain.addActionListener(createTryAgainButtonListener());
while(endScreenDone)
{
System.out.print("");
}
dispose();
}
public static ActionListener createBackToMenuButtonListener()
{
return new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
backToMain = true;
tryAgain = false;
endFrame.removeAll();
endScreenDone = false;
}
};
}
public static ActionListener createTryAgainButtonListener()
{
return new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
backToMain = false;
tryAgain = true;
correct = true;
endFrame.removeAll();
endScreenDone = false;
}
};
}
public void buttonGrid()
{
freeTileDone = false;
freeTileSelection = new int[column];
buttonGridFrame = getContentPane();
JPanel buttonGrid = new JPanel(new GridLayout(row, column));
ButtonGroup[] columnGroup = new ButtonGroup[column];
for(int index = 0; index < columnGroup.length; index++)
{
columnGroup[index] = new ButtonGroup();
}
for(int rowIndex = 0; rowIndex < row; rowIndex++)
{
for(int columnIndex = 0; columnIndex < column; columnIndex++)
{
uniqueFreeTileButton[rowIndex][columnIndex] = new JToggleButton();
uniqueFreeTileButton[rowIndex][columnIndex].setBackground(Grid.DEFAULT_COLOR);
uniqueFreeTileButton[rowIndex][columnIndex].setBorder(BorderFactory.createLineBorder(Color.black));
buttonGrid.add(uniqueFreeTileButton[rowIndex][columnIndex]);
uniqueFreeTileButton[rowIndex][columnIndex].addActionListener(createGridButtonListener(rowIndex,
columnIndex));
columnGroup[columnIndex].add(uniqueFreeTileButton[rowIndex][columnIndex]);
}
}
buttonGridFrame.add(buttonGrid);
JButton submitButton = new JButton("Submit");
submitButton.setBackground(Color.GREEN);
buttonGrid.add(submitButton);
buttonGridFrame.add(submitButton, BorderLayout.SOUTH);
submitButton.addActionListener(createFreeTileSubmitButtonListener());
setPreferredSize(new Dimension(500,500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("BeatRepeat");
pack();
setVisible(true);
while(!freeTileDone)
{
System.out.print("");
}
}
public static ActionListener createGridButtonListener(final int row, final int column)
{
return new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
AbstractButton abstractButton = (AbstractButton)event.getSource();
if(abstractButton.getModel().isSelected())
{
freeTileSelection[column] = row;
playSound(currentRound.getNote(row), 100);
}
}
};
}
public static ActionListener createFreeTileSubmitButtonListener()
{
return new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
String stringSelection = "";
for(int index = 0; index < freeTileSelection.length; index++)
{
stringSelection += freeTileSelection[index];
}
correct = stringSelection.equals(currentRound.getQuestionPictureToString());
if(correct)
{
numberOfScore += SCORE_HARD;
if(row == 3)
{
numberOfScore += SCORE_SIZE1;
}
else if(row == 4)
{
numberOfScore += SCORE_SIZE2;
}
else if(row == 6)
{
numberOfScore += SCORE_SIZE3;
}
}
buttonGridFrame.removeAll();
freeTileDone = true;
}
};
}
public void multipleChoicesGrid()
{
multipleChoicesFrame = getContentPane();
multipleChoicesFrame.setLayout(new BorderLayout());
multipleChoicesDone = false;
JPanel[] choicePicture = new JPanel[4];
for(int choiceIndex = 0; choiceIndex < choicePicture.length; choiceIndex++)
{
choicePicture[choiceIndex] = new JPanel(new GridLayout(currentRound.getRow(),
currentRound.getColumn()));
for(int rowIndex = 0; rowIndex < currentRound.getRow(); rowIndex++)
{
for(int columnIndex = 0; columnIndex < currentRound.getColumn(); columnIndex++)
{
JLabel coloredTile = new JLabel();
coloredTile.setOpaque(true);
coloredTile.setBackground(currentRound.getMultipleChoicesTileColor(rowIndex,
columnIndex,
choiceIndex));
coloredTile.setBorder(BorderFactory.createLineBorder(Color.black));
choicePicture[choiceIndex].add(coloredTile);
}
}
}
JPanel multipleChoice = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
ButtonGroup group = new ButtonGroup();
JToggleButton[] selection = new JToggleButton[4];
c.insets = new Insets (5, 0, 0, 0);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.weighty = 1;
c.ipadx = 75;
c.ipady = 200;
c.gridx = 0;
c.gridy = 0;
multipleChoice.add(choicePicture[0], c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.gridx = 1;
c.gridy = 0;
multipleChoice.add(choicePicture[1], c);
c.gridx = 0;
c.gridy = 1;
c.ipady = 5;
selection[0] = new JToggleButton("Option " + 1);
multipleChoice.add(selection[0], c);
c.gridx = 1;
c.gridy = 1;
c.ipady = 5;
selection[1] = new JToggleButton("Option " + 2);
multipleChoice.add(selection[1], c);
c.gridx--;
c.gridy++;
c.ipadx = 75;
c.ipady = 200;
multipleChoice.add(choicePicture[2], c);
c.gridx++;
multipleChoice.add(choicePicture[3], c);
c.gridx--;
c.gridy++;
c.ipady = 10;
selection[2] = new JToggleButton("Option " + 3);
multipleChoice.add(selection[2], c);
c.gridx++;
selection[3] = new JToggleButton("Option " + 4);
multipleChoice.add(selection[3], c);
JButton submit = new JButton("Submit");
submit.setBackground(Color.GREEN);
multipleChoicesFrame.add(multipleChoice, BorderLayout.CENTER);
multipleChoicesFrame.add(submit, BorderLayout.SOUTH);
group.add(selection[0]);
group.add(selection[1]);
group.add(selection[2]);
group.add(selection[3]);
selection[0].addActionListener(createOptionButtonListener(0));
selection[1].addActionListener(createOptionButtonListener(1));
selection[2].addActionListener(createOptionButtonListener(2));
selection[3].addActionListener(createOptionButtonListener(3));
submit.addActionListener(createMultipleChoicesSubmitButtonListener());
setPreferredSize(new Dimension(500,500));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setTitle("BeatRepeat");
pack();
setVisible(true);
while(!multipleChoicesDone)
{
System.out.print("");
}
}
public static ActionListener createOptionButtonListener(int option)
{
return new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
multipleChoiceSelection = option;
}
};
}
public static ActionListener createMultipleChoicesSubmitButtonListener()
{
return new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
correct
= currentRound.
getMultipleChoicesPictureToString(multipleChoiceSelection).
equals(currentRound.getQuestionPictureToString());
if(correct)
{
if(difficultyMode == EASY)
{
numberOfScore += SCORE_EASY;
}
else
{
numberOfScore += SCORE_MEDIUM;
}
if(row == 3)
{
numberOfScore += SCORE_SIZE1;
}
else if(row == 4)
{
numberOfScore += SCORE_SIZE2;
}
else if(row == 6)
{
numberOfScore += SCORE_SIZE3;
}
}
multipleChoicesFrame.removeAll();
multipleChoicesDone = true;
}
};
}
public void pictureGrid()
{
pictureGridFrame = getContentPane();
pictureGridFrame.setLayout(new BorderLayout());
JPanel gridFrame = new JPanel(new GridLayout(row, column));
for(int index1 = 0; index1 < row; index1++)
{
for(int index2 = 0; index2 < column; index2++)
{
JLabel coloredTiles = new JLabel();
coloredTiles.setOpaque(true);
coloredTiles.setBackground(currentRound.getQuestionTileColor(index1, index2));
coloredTiles.setBorder(BorderFactory.createLineBorder(Color.black));
gridFrame.add(coloredTiles);
}
}
gridFrame.setPreferredSize(new Dimension(400,400));
JLabel round = new JLabel("Round: " + Round.numberOfRounds, SwingConstants.LEFT);
round.setFont(new Font("Arial", Font.PLAIN, 20));
JLabel score = new JLabel("Score: " + numberOfScore, SwingConstants.LEFT);
score.setFont(new Font("Arial", Font.PLAIN, 20));
JLabel empty = new JLabel(" ");
JLabel empty1 = new JLabel(" ");
pictureGridFrame.add(round, BorderLayout.NORTH);
pictureGridFrame.add(empty1, BorderLayout.WEST);
pictureGridFrame.add(gridFrame, BorderLayout.CENTER);
pictureGridFrame.add(empty, BorderLayout.EAST);
pictureGridFrame.add(score, BorderLayout.SOUTH);
if(difficultyMode == "Easy")
{
setPreferredSize(new Dimension(500, 450));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("BeatRepeat");
pack();
setVisible(true);
}
else
{
listenCarefully();
}
playMelody(currentRound.getQuestionPictureToString());
try
{
Thread.sleep(3000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
setVisible(false);
pictureGridFrame.removeAll();
}
public void playMelody(String melody)
{
for(int noteIndex = 0; noteIndex < melody.length(); noteIndex++ )
{
playSound(currentRound.getNote(Character.getNumericValue(melody.charAt(noteIndex))),
1000);
}
}
private static void playSound(File Sound, long timeSleep)
{
try
{
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(Sound));
clip.start();
Thread.sleep(timeSleep);
}
catch(Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
}
}
public void listenCarefully()
{
listenFrame = getContentPane();
listenFrame.setLayout(new BorderLayout());
JLabel roundLabel = new JLabel("Round: " + Round.numberOfRounds, SwingConstants.CENTER);
JLabel listenLabel = new JLabel("Listen Carefully...", SwingConstants.CENTER);
JLabel scoreLabel = new JLabel("Score: " + numberOfScore, SwingConstants.CENTER);
roundLabel.setFont(new Font("Arial", Font.PLAIN, 25));
listenLabel.setFont(new Font("Arial", Font.PLAIN, 40));
scoreLabel.setFont(new Font("Arial", Font.PLAIN, 25));
listenFrame.add(roundLabel, BorderLayout.NORTH);
listenFrame.add(listenLabel, BorderLayout.CENTER);
listenFrame.add(scoreLabel, BorderLayout.SOUTH);
setPreferredSize(new Dimension(500,500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("BeatRepeat");
pack();
setVisible(true);
}
public void nextRound()
{
currentRound = new Round(row, column);
}
public static void main(String[] args)
{
boolean coninue = true;
BeatRepeatGUI newGame;
while(coninue)
{
newGame = new BeatRepeatGUI();
while(BeatRepeatGUI.tryAgain)
{
if(BeatRepeatGUI.difficultyMode != BeatRepeatGUI.HARD)
{
while(BeatRepeatGUI.correct)
{
newGame.nextRound();
newGame.pictureGrid();
newGame.multipleChoicesGrid();
}
}
else
{
while(BeatRepeatGUI.correct)
{
newGame.nextRound();
newGame.pictureGrid();
newGame.buttonGrid();
}
}
newGame.endScreen();
Round.numberOfRounds = 0;
BeatRepeatGUI.numberOfScore = 0;
coninue = BeatRepeatGUI.backToMain;
}
System.out.println("Back to main");
}
}
}