-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
866 lines (804 loc) · 30.6 KB
/
GUI.java
File metadata and controls
866 lines (804 loc) · 30.6 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
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.ArrayList;
/**
* GUI is the main class for this game. It builds and displays the application
* GUI and initialises all other components.
*
*/
public class GUI extends JFrame {
static final long serialVersionUID = 1;
private JFrame frame;
private JFrame difficultyFrame;
public JFrame gameFrame;
public JLabel level = new JLabel("level: 0");
public JLabel difficultyLabel = new JLabel("Difficulty: 1");
public Game game;
public HashMap<Integer, JButton> buttonList = new HashMap<>();
public JButton one;
public JButton two;
public JButton three;
public JButton four;
public JButton five;
public JButton six;
public JButton seven;
public JButton eight;
public JButton nine;
public JButton ten;
public JButton eleven;
public JButton twelve;
public JButton play;
private JButton start;
private int oldLevel;
private boolean running;
public int difficulty = 0;
public Color purple = new Color(84, 46, 145);
public Color yellow = new Color(253, 220, 6);
/**
* Constructor for objects of class GUI also creates a Game object that handles
* the game logic creates another thread to handle scoring as to not halt the
* main thread
*/
public static void main(String[] args) {
GUI GUI = new GUI();
}
public void setDifficulty(int d) {
game.setDifficulty(d);
difficulty = d;
// set difficulty 3* on each stage
}
public GUI() {
game = new Game();
running = true;
// setDifficulty(3);//THIS IS AN ISSUE BEING HERE
startGame();
new Thread(new Runnable() {
public void run() {
while (running) {
level.setText("Current Level: " + game.returnlevel());
difficultyLabel.setText("Difficulty: " + difficulty);
level.setForeground(Color.WHITE);
difficultyLabel.setForeground(Color.WHITE);
if (game.win() == 0) {
// if level below zero, tell user they lost
game.setLevel(0);
exit();
}
if (game.win() == 1) {
// if level is 10, tell user they win
game.setLevel(0);
winner();
}
if (game.win() == 2) {
checkLevel();
/**
* if(oldLevel > currentLevel) //lost a round { for (JButton button :
* buttonList.values()) { Color back = button.getBackground();
* button.setEnabled(false); button.setOpaque(true);
* button.setBackground(Color.BLUE); try { Thread.sleep(300); //1000
* milliseconds is one second. } catch(InterruptedException ex) {
* Thread.currentThread().interrupt(); } button.setBackground(back); } setLevel
* = oldLevel--; } if(oldLevel < currentLevel) //win a round { for (JButton
* button : buttonList.values()) { Color back = button.getBackground();
* button.setEnabled(false); button.setOpaque(true);
* button.setBackground(yellow); try { Thread.sleep(300); //1000
* milliseconds is one second. } catch(InterruptedException ex) {
* Thread.currentThread().interrupt(); } button.setBackground(back); } setLevel
* = oldLevel++; }
*
* oldLevel = setLevel; }
*
* try { Thread.sleep(300); //1000 milliseconds is one second. }
* catch(InterruptedException ex) { Thread.currentThread().interrupt(); }
*/
}
}
}
}).start();
}
private void checkLevel() {
int repeat = 5;
if (game.returnlevel() < oldLevel) {
for (int i = 0; i < repeat; i++) {
level.getParent().setBackground(Color.RED);
try {
Thread.sleep(150);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
level.getParent().setBackground(purple);
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
} else if (game.returnlevel() > oldLevel) {
for (int i = 0; i < repeat; i++) {
level.getParent().setBackground(Color.GREEN);
try {
Thread.sleep(150);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
level.getParent().setBackground(purple);
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
oldLevel = game.returnlevel();
}
/**
* Creates a pop up dialog box to let the user know that they lost Removes all
* the frames and goes back to the default interface
*/
private void exit() {
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
play.setEnabled(false);
// disable buttons whilst flashing
for (JButton button : buttonList.values()) {
button.setEnabled(false);
button.setOpaque(true);
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
button.setBackground(Color.RED);
repaint();
}
play.setEnabled(true);
try {
Thread.sleep(150);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
JOptionPane.showMessageDialog(gameFrame, "You've Lost\n", "Message", JOptionPane.INFORMATION_MESSAGE);
for (JButton button : buttonList.values()) {
button.setEnabled(false);
button.setOpaque(true);
button.setBackground(purple);
repaint();
}
int option = endGameOptions("You've Lost", "LOSER");
if (option == 0) {
game.newGame();
} else if (option == 1) {
gameFrame.setVisible(false);
chooseDifficulty(gameFrame);
// frame.removeAll();
// frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
} else if (option == 2 || option == -1) {
quit();
}
}
/**
* Creates a pop up dialog box to let the user know that they win Removes all
* the frames and goes back to the default interface
*/
private void winner() {
play.setEnabled(false);
// disable buttons whilst flashing
for (JButton button : buttonList.values()) {
button.setEnabled(false);
button.setOpaque(true);
button.setBackground(yellow);
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
play.setEnabled(true);
try {
Thread.sleep(150);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
for (JButton button : buttonList.values()) {
button.setEnabled(false);
button.setOpaque(true);
button.setBackground(purple);
repaint();
}
int option = endGameOptions("You've Won!", "WINNER");
if (option == 0) {
game.newGame();
} else if (option == 1) {
gameFrame.setVisible(false);
makeDifficultyFrame();
// frame.removeAll();
// frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
} else if (option == 2 || option == -1) {
quit();
}
}
private int endGameOptions(String message, String title) {
Object[] options = { "Restart Game", "Change Difficulty", "Quit" };
int n = JOptionPane.showOptionDialog(gameFrame, // parent container of JOptionPane
message, title, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, // do not use a
// custom Icon
options, // the titles of buttons
options[2]);// default button title
return n;
}
/**
* Creates a dialog box about the game
*/
private void about(JFrame frame) {
frame.setBackground(yellow);
JOptionPane.showMessageDialog(frame, "Memory Game\n", "About Memory Game", JOptionPane.INFORMATION_MESSAGE);
}
/**
* Creates a dialog box to give the user help about the game
*/
private void help(JFrame frame) {
frame.setBackground(yellow);
JOptionPane.showMessageDialog(frame, "Repeat the challenge shown to you \n Click play to advance", "Help",
JOptionPane.INFORMATION_MESSAGE);
}
/**
* Create the main frame's menu bar.
*
* @param frame The frame that the menu bar should be added to.
*/
public void makeMenu(JFrame frame) {
JMenuBar menubar = new JMenuBar();
menubar.setBackground(yellow);
frame.setJMenuBar(menubar);
// create the File menu
JMenu fileMenu = new JMenu("File");
menubar.add(fileMenu);
JMenuItem restart = new JMenuItem("Main Menu");
restart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
startGame();
}
});
fileMenu.add(restart);
JMenuItem quit = new JMenuItem("Quit");
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
quit();
}
});
fileMenu.add(quit);
// create the File menu
JMenu helpMenu = new JMenu("Help");
menubar.add(helpMenu);
JMenuItem about = new JMenuItem("About");
about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
about(frame);
}
});
helpMenu.add(about);
JMenuItem help = new JMenuItem("Help");
help.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
help(frame);
}
});
helpMenu.add(help);
}
/**
* Create the Swing frame and its content for the menu screen.
*/
private void startGame() {
game.newGame();
frame = new JFrame("Remember Remember - Main Menu");
// Creates the panel for the 3 JButtons
JPanel p = new JPanel(new GridLayout(1, 3));
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
// Adds the JPanel to the content Pane
makeMenu(frame);
contentPane.add(p, BorderLayout.CENTER);
ActionListener game = new ActionListener() {
public void actionPerformed(ActionEvent e) {
reset();
}
};
// button 1
if (difficulty == 0) {
start = new JButton("New Game | Select a difficulty");
} else {
start = new JButton("New Game | Difficulty: " + difficulty);
}
start.setBackground(purple);
start.setForeground(Color.WHITE);
start.addActionListener(game);
p.add(start);
ActionListener diffListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
chooseDifficulty(frame);
}
};
// button 2
JButton chooseDifficulty = new JButton("Set Difficulty");
chooseDifficulty.addActionListener(diffListener);
chooseDifficulty.setForeground(Color.WHITE);
chooseDifficulty.setBackground(purple);
p.add(chooseDifficulty);
ActionListener quitListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
quit();
}
};
// button 3
JButton quit = new JButton("QUIT");
quit.addActionListener(quitListener);
quit.setBackground(purple);
quit.setForeground(Color.WHITE);
p.add(quit);
if (difficulty == 0) {
start.setEnabled(false);
}
frame.setLocation(500, 400);
frame.pack();
frame.setSize(800, 200);
frame.setVisible(true);
}
private void chooseDifficulty(JFrame frame) {
frame.setVisible(false);
makeDifficultyFrame();
frame.dispose();
}
/**
* Starts a clean game
*/
private void reset() {
frame.removeAll();
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
makeFrame();
}
private void restart() {
frame.removeAll();
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
startGame();
}
/**
* Removes all frames and closes down the game
*/
public void quit() {
if (frame != null) {
frame.dispose();
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
if (gameFrame != null) {
gameFrame.dispose();
gameFrame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
if (difficultyFrame != null) {
difficultyFrame.dispose();
difficultyFrame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
return;
}
/**
* Creates a swing frame and its content for the game
*/
private void makeFrame() {
// creates the frame for the grid
gameFrame = new JFrame("Remember Remember Game");
// Creates the panel used for 4 JButtons that will flash
JPanel p = new JPanel(new GridLayout(1, 3));
if (difficulty == 2) {
p = new JPanel(new GridLayout(2, 3));
} else if (difficulty == 3) {
p = new JPanel(new GridLayout(3, 3));
} else if (difficulty == 4) {
p = new JPanel(new GridLayout(4, 3));
}
// Creates the panel used for 2 JButtons for options
JPanel pane = new JPanel(new GridLayout(1, 2));
Container contentPane = gameFrame.getContentPane();
// Creates a container
contentPane.setLayout(new BorderLayout());
JPanel container = new JPanel(new GridLayout(1, 1));
container.setOpaque(true);
container.setBackground(purple);
container.add(level);
container.add(difficultyLabel);
// adds the level JLabel to the content pane
contentPane.add(container, BorderLayout.NORTH);
// contentPane.add(level, BorderLayout.NORTH);
// contentPane.add(difficultyLabel, BorderLayout.SOUTH);
// Adds the JPanel into the content pane
contentPane.add(p, BorderLayout.CENTER);
makeMenu(gameFrame);
// button 1
one = new JButton();
one.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// adds number referencing this button to an arrayList
playerClick(1);
game.playerchallenge(1);
// disables buttons if user has finished entering the challenge
buttonDisable();
}
});
p.add(one);
// button 2
two = new JButton();
two.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// adds number referencing this button to an arrayList
playerClick(2);
game.playerchallenge(2);
// disables buttons if user has finished entering the challenge
buttonDisable();
}
});
p.add(two);
// button 3
three = new JButton();
three.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// adds number referencing this button to an arrayList
playerClick(3);
game.playerchallenge(3);
// disables buttons if user has finished entering the challenge
buttonDisable();
}
});
p.add(three);
if (difficulty == 2 || difficulty == 3 || difficulty == 4) {
// button 4
four = new JButton();
four.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// adds number referencing this button to an arrayList
game.playerchallenge(4);
playerClick(4);
// disables buttons if user has finished entering the challenge
buttonDisable();
}
});
p.add(four);
// button 5
five = new JButton();
five.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// adds number referencing this button to an arrayList
game.playerchallenge(5);
playerClick(5);
// disables buttons if user has finished entering the challenge
buttonDisable();
}
});
p.add(five);
// button 6
six = new JButton();
six.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// adds number referencing this button to an arrayList
game.playerchallenge(6);
playerClick(6);
// disables buttons if user has finished entering the challenge
buttonDisable();
}
});
p.add(six);
}
if (difficulty == 3 || difficulty == 4) {
// button 7
seven = new JButton();
seven.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// adds number referencing this button to an arrayList
game.playerchallenge(7);
playerClick(7);
// disables buttons if user has finished entering the challenge
buttonDisable();
}
});
p.add(seven);
// button 8
eight = new JButton();
eight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// adds number referencing this button to an arrayList
game.playerchallenge(8);
playerClick(8);
// disables buttons if user has finished entering the challenge
buttonDisable();
}
});
p.add(eight);
// button 9
nine = new JButton();
nine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// adds number referencing this button to an arrayList
game.playerchallenge(9);
playerClick(9);
// disables buttons if user has finished entering the challenge
buttonDisable();
}
});
p.add(nine);
}
if (difficulty == 4) {
// button 10
ten = new JButton();
ten.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// adds number referencing this button to an arrayList
game.playerchallenge(10);
playerClick(10);
// disables buttons if user has finished entering the challenge
buttonDisable();
}
});
p.add(ten);
// button 11
eleven = new JButton();
eleven.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// adds number referencing this button to an arrayList
game.playerchallenge(11);
playerClick(11);
// disables buttons if user has finished entering the challenge
buttonDisable();
}
});
p.add(eleven);
// button 7
twelve = new JButton();
twelve.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// adds number referencing this button to an arrayList
game.playerchallenge(12);
playerClick(12);
// disables buttons if user has finished entering the challenge
buttonDisable();
}
});
p.add(twelve);
}
// adds buttons with a refence to a HashMap
buttonList();
// disable all the buttons in the buttonList
for (JButton button : buttonList.values()) {
button.setBackground(purple);
button.setEnabled(false);
}
// add the JPanel pane to the contentPane
contentPane.add(pane, BorderLayout.SOUTH);
// JButton called play
play = new JButton("Play");
play.setOpaque(true);
play.setBackground(yellow);
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// initialse the random challenge in the game class
game.challenge();
// make the buttons flash
blink();
}
});
pane.add(play);
JButton quit = new JButton("Quit");
quit.setOpaque(true);
quit.setBackground(yellow);
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
quit();
}
});
pane.add(quit);
gameFrame.setBackground(Color.RED);
gameFrame.setLocation(frame.getX(), frame.getY());
//gameFrame.pack();
gameFrame.setSize(350, (150 * difficulty));
gameFrame.setVisible(true);
gameFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
// gameFrame = new GameFrame();
}
private void makeDifficultyFrame() {
// creates the frame for the grid
// Creates the panel used for 4 JButtons that will flash
JPanel p = new JPanel(new GridLayout(1, 4));
// Adds the JPanel into the content pane
difficultyFrame = new JFrame("Choose Difficulty");
difficultyFrame.setBackground(yellow);
Container contentPane = difficultyFrame.getContentPane();
contentPane.setBackground(yellow);
contentPane.setLayout(new BorderLayout());
// Adds the JPanel to the content Pane
makeMenu(difficultyFrame);
contentPane.add(p, BorderLayout.CENTER);
ActionListener oneListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
setDifficulty(1);
difficultyFrame.dispose();
startGame();
}
};
// button 1
JButton one = new JButton("1");
one.setOpaque(true);
one.setBackground(yellow);
one.addActionListener(oneListener);
p.add(one);
ActionListener twoListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
setDifficulty(2);
difficultyFrame.dispose();
startGame();
}
};
// button 1
JButton two = new JButton("2");
two.setBackground(purple);
two.addActionListener(twoListener);
two.setForeground(Color.WHITE);
p.add(two);
ActionListener threeListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
setDifficulty(3);
difficultyFrame.dispose();
startGame();
}
};
// button 1
JButton three = new JButton("3");
three.setBackground(yellow);
three.addActionListener(threeListener);
p.add(three);
ActionListener fourListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
setDifficulty(4);
difficultyFrame.dispose();
startGame();
}
};
// button 1
JButton four = new JButton("4");
four.setOpaque(true);
four.setForeground(Color.WHITE);
four.setBackground(purple);
four.addActionListener(fourListener);
p.add(four);
difficultyFrame.setLocation(frame.getX(), frame.getY());
difficultyFrame.pack();
difficultyFrame.setSize(800, 200);
difficultyFrame.setVisible(true);
}
/**
* disables the buttons if the user has entered the correct amount of inputs
*/
public void buttonDisable() {
if (game.finished()) {
for (JButton button : buttonList.values()) {
button.setEnabled(false);
}
} else {
for (JButton button : buttonList.values()) {
button.setEnabled(true);
}
}
}
/**
* Adds the buttons into a hashMap with a number reference
*/
public void buttonList() {
buttonList.put(1, one);
buttonList.put(2, two);
buttonList.put(3, three);
if (difficulty == 2 || difficulty == 3 || difficulty == 4) {
buttonList.put(4, four);
buttonList.put(5, five);
buttonList.put(6, six);
}
if (difficulty == 3 || difficulty == 4) {
buttonList.put(7, seven);
buttonList.put(8, eight);
buttonList.put(9, nine);
}
if (difficulty == 4) {
buttonList.put(10, ten);
buttonList.put(11, eleven);
buttonList.put(12, twelve);
}
}
/**
* Make the buttons change colour creates another thread to handle flashing as
* to not halt the main thread
*/
public void blink() {
play.setEnabled(false);
// disable buttons whilst flashing
for (JButton button : buttonList.values()) {
button.setEnabled(false);
}
new Thread(new Runnable() {
public void run() {
// iterate through the original arrayList
for (int Integer : game.returnOriginalchallenge()) {
// retrieve the JButton in accordance with the number in the HashMap
JButton currentButton = buttonList.get(Integer);
// assign the colour of the current button to a runningiable called back
// currentButton.setOpaque(true);
Color newColor = yellow.brighter().brighter();
currentButton.setBackground(newColor);
// currentButton.setBorder(new LineBorder(Color.BLACK, 10));
// currentButton.setContentAreaFilled(false);
// refresh
repaint();
// sleep for 1/2 sec
try {
Thread.sleep(600);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
// set colour to original colour
// currentButton.setBorder(new LineBorder(Color.BLACK, 1));
currentButton.setBackground(purple);
// currentButton.setContentAreaFilled(true);
// update
repaint();
// sleep for 0.3 ms to give appearance of blinking if same button comes on twice
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
// enable buttons
for (JButton button : buttonList.values()) {
button.setEnabled(true);
}
play.setEnabled(true);
}
}).start();
}
public void playerClick(Integer i) {
// for (JButton button : buttonList.values()) {
// button.setEnabled(false);
// }
JButton currentButton = buttonList.get(i);
currentButton.setEnabled(false);
new Thread(new Runnable() {
public void run() {
// iterate through the original arrayList
// retrieve the JButton in accordance with the number in the HashMap
// assign the colour of the current button to a runningiable called back
// currentButton.setContentAreaFilled(true);
currentButton.setOpaque(true);
Color newColor = yellow.brighter().brighter();
currentButton.setBackground(newColor);
// currentButton.setBorder(new LineBorder(Color.BLACK, 10));
// refresh
repaint();
// sleep for 1/2 sec
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
// set colour to original colour
// currentButton.setBorder(new LineBorder(Color.BLACK, 1));
// currentButton.setContentAreaFilled(false);
// currentButton.setBorder(new LineBorder(Color.BLACK, 0));
currentButton.setBackground(purple);
// update
repaint();
}
}).start();
currentButton.setEnabled(true);
}
}