-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboardPanel.java
804 lines (687 loc) · 20.2 KB
/
boardPanel.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
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
/* @author Chris Fortier
*
* CSCI e-50b Term Project
*
* boardPanel.java
*
* File is called from GameBoard and constructs a panel of 35 squares wide by 25 squares high to use for the board. It also contains a JTextArea for all output to user. Each of the spaces and rooms are actually buttons.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class boardPanel extends JPanel
{
//class variables
myButton btn[] = new myButton[1000]; //instantiate enough myButtons for each square.
int activeMyButtons = 0; //track the amount of myButtons in use.
boolean debugBoardPanel = false; //track debug mode
private int status = 1; //track status of game
private boolean spaceSelected = false; //track if a VALID space was selected
private int newPositionColumn = -1; //used to store newPositions for GameBoard to call
private int newPositionRow = -1; //used to store newPositions for GameBoard to call
private boolean startRumor = false; //track if a new rumor was started
//create GridBagConstraints for objects in class
GridBagConstraints cons = new GridBagConstraints();
//create global button names AND store a reference to them in an array [allRooms]
JButton[] allRooms = new JButton[10];
JButton btnSpa;
JButton btnTheater;
JButton btnLivingRoom;
JButton btnObservatory;
JButton btnPatio;
JButton btnPool;
JButton btnHall;
JButton btnKitchen;
JButton btnDiningRoom;
JButton btnGuestHouse;
//create global textOut for bottom panel. Accessible to whole board
String textOut = "Welcome to Clue!";
JTextArea bottomText = new JTextArea(textOut, 5 , 1);
JScrollPane jsp = new JScrollPane(bottomText);
//default room color
Color defaultRoomColor = new Color(220, 209, 193);
Color defaultPoolColor = new Color(118, 184, 177);
/*
* main constructor
* @param debug - boolean for debug mode
*/
public boardPanel(boolean debug)
{
//initial constructor
super();
debugBoardPanel = debug;
//continue defining boardPanel
this.setLayout(new GridBagLayout());
cons.fill = GridBagConstraints.BOTH;
cons.insets = new Insets(0,0,0,0);
cons.anchor = GridBagConstraints.FIRST_LINE_START;
this.setVisible(true);
//button preferred size
Dimension btnSize = new Dimension(25,25);
// top row ------------------------------------------------------------------------------------------
//spa
btnSpa = new JButton("Spa");
allRooms[0] = btnSpa;
btnSpa.setPreferredSize(btnSize);
cons.gridx = 0;
cons.gridy = 0;
cons.gridwidth = 5;
cons.gridheight = 5;
btnSpa.setBackground(defaultRoomColor);
btnSpa.setOpaque(true);
btnSpa.setBorderPainted(false);
btnSpa.addActionListener(new myActionListener());
this.add(btnSpa, cons);
//space buttons
for (int y = 0; y < 5; y++) {
for (int x = 5; x < 10; x++) {
createBlankButtons(x, y, btnSize);
}
}
//theater
btnTheater = new JButton("Theater");
allRooms[1] = btnTheater;
btnTheater.setPreferredSize(btnSize);
cons.gridx = 10;
cons.gridy = 0;
cons.gridwidth = 5;
cons.gridheight = 5;
btnTheater.setOpaque(true);
btnTheater.setBorderPainted(false);
btnTheater.setBackground(defaultRoomColor);
btnTheater.addActionListener(new myActionListener());
this.add(btnTheater, cons);
//space buttons
for (int y = 0; y < 5; y++) {
for (int x = 15; x < 20; x++) {
createBlankButtons(x, y, btnSize);
}
}
//Living Room
btnLivingRoom = new JButton("Living Room");
allRooms[2] = btnLivingRoom;
btnLivingRoom.setPreferredSize(btnSize);
cons.gridx = 20;
cons.gridy = 0;
cons.gridwidth = 5;
cons.gridheight = 5;
btnLivingRoom.setBackground(defaultRoomColor);
btnLivingRoom.setOpaque(true);
btnLivingRoom.setBorderPainted(false);
btnLivingRoom.addActionListener(new myActionListener());
this.add(btnLivingRoom, cons);
//space buttons
for (int y = 0; y < 5; y++) {
for (int x = 25; x < 30; x++) {
createBlankButtons(x, y, btnSize);
}
}
//observatory
btnObservatory = new JButton("Observatory");
allRooms[3] = btnObservatory;
btnObservatory.setPreferredSize(btnSize);
cons.gridx = 30;
cons.gridy = 0;
cons.gridwidth = 5;
cons.gridheight = 5;
btnObservatory.setBackground(defaultRoomColor);
btnObservatory.setOpaque(true);
btnObservatory.setBorderPainted(false);
btnObservatory.addActionListener(new myActionListener());
this.add(btnObservatory, cons);
// top to middle spaces ------------------------------------------------------------------------------------------
//space buttons
for (int y = 5; y < 10; y++) {
for (int x = 0; x < 35; x++) {
createBlankButtons(x, y, btnSize);
}
}
// middle row ------------------------------------------------------------------------------------------
//Patio
btnPatio = new JButton("Patio");
allRooms[4] = btnPatio;
btnPatio.setPreferredSize(btnSize);
cons.gridx = 0;
cons.gridy = 10;
cons.gridwidth = 5;
cons.gridheight = 5;
btnPatio.setBackground(defaultRoomColor);
btnPatio.setOpaque(true);
btnPatio.setBorderPainted(false);
btnPatio.addActionListener(new myActionListener());
this.add(btnPatio, cons);
//space buttons
for (int y = 10; y < 15; y++) {
for (int x = 5; x < 10; x++) {
createBlankButtons(x, y, btnSize);
}
}
//pool
btnPool = new JButton("Pool");
allRooms[5] = btnPool;
btnPool.setPreferredSize(btnSize);
cons.gridx = 10;
cons.gridy = 10;
cons.gridwidth = 15;
cons.gridheight = 5;
btnPool.setBackground(defaultPoolColor);
btnPool.setOpaque(true);
btnPool.setBorderPainted(false);
btnPool.addActionListener(new myActionListener());
this.add(btnPool, cons);
//space buttons
for (int y = 10; y < 15; y++) {
for (int x = 25; x < 30; x++) {
createBlankButtons(x, y, btnSize);
}
}
//Hall
btnHall = new JButton("Hall");
allRooms[6] = btnHall;
btnHall.setPreferredSize(btnSize);
cons.gridx = 30;
cons.gridy = 10;
cons.gridwidth = 5;
cons.gridheight = 5;
btnHall.setBackground(defaultRoomColor);
btnHall.setOpaque(true);
btnHall.setBorderPainted(false);
btnHall.addActionListener(new myActionListener());
this.add(btnHall, cons);
// middle to bottom spaces ------------------------------------------------------------------------------------------
//space buttons
for (int y = 15; y < 20; y++) {
for (int x = 0; x < 35; x++) {
createBlankButtons(x, y, btnSize);
}
}
// bottom row ------------------------------------------------------------------------------------------
//Kitchen
btnKitchen = new JButton("Kitchen");
allRooms[7] = btnKitchen;
btnKitchen.setPreferredSize(btnSize);
cons.gridx = 0;
cons.gridy = 20;
cons.gridwidth = 5;
cons.gridheight = 5;
btnKitchen.setBackground(defaultRoomColor);
btnKitchen.setOpaque(true);
btnKitchen.setBorderPainted(false);
btnKitchen.addActionListener(new myActionListener());
this.add(btnKitchen, cons);
//space buttons
for (int y = 20; y < 25; y++) {
for (int x = 5; x < 10; x++) {
createBlankButtons(x, y, btnSize);
}
}
//dining room
btnDiningRoom = new JButton("Dining Room");
allRooms[8] = btnDiningRoom;
btnPool.setPreferredSize(btnSize);
cons.gridx = 10;
cons.gridy = 20;
cons.gridwidth = 15;
cons.gridheight = 5;
btnDiningRoom.setBackground(defaultRoomColor);
btnDiningRoom.setOpaque(true);
btnDiningRoom.setBorderPainted(false);
btnDiningRoom.addActionListener(new myActionListener());
this.add(btnDiningRoom, cons);
//space buttons
for (int y = 20; y < 25; y++) {
for (int x = 25; x < 30; x++) {
createBlankButtons(x, y, btnSize);
}
}
//Guest House
btnGuestHouse = new JButton("Guest House");
allRooms[9] = btnGuestHouse;
btnGuestHouse.setPreferredSize(btnSize);
cons.gridx = 30;
cons.gridy = 20;
cons.gridwidth = 5;
cons.gridheight = 5;
btnGuestHouse.setBackground(defaultRoomColor);
btnGuestHouse.setOpaque(true);
btnGuestHouse.setBorderPainted(false);
btnGuestHouse.addActionListener(new myActionListener());
this.add(btnGuestHouse, cons);
// textArea ------------------------------------------------------------------------------------------
//create textarea for output
cons.gridx = 0;
cons.gridy = 25;
cons.gridwidth = 35;
this.add(jsp, cons);
}
//listener that listens to all buttons and outputs which button was pressed
class myActionListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
//method variable for output and button pressed
String output = "";
String buttonPressed = "";
int btnColumn = 0;
int btnRow = 0;
//if game status = 2 / dice has been rolled. Determine which button was pressed
if (status == 2) {
//iterates through every btn[i] and determines which one was pressed
for (int i = 0; i <= activeMyButtons; i++) {
if (e.getSource() == btn[i]) {
buttonPressed = "btn[" + i +"]";
btnColumn = btn[i].positionX;
btnRow = btn[i].positionY;
//check if button is blue, meaning it is available
if ((btn[i].getBackground() == Color.blue) && (spaceSelected == false)) {
btn[i].setBackground(Color.red);
spaceSelected = true;
setOutput("You selected: btn[" + i +"]");
//set new positions so that control loop will move the position
newPositionColumn = btnColumn;
newPositionRow = btnRow;
//send output for debug
if (debugBoardPanel) { System.out.println("newPositionColumn: " + newPositionColumn + ", newPositionRow: " + newPositionRow); }
} else if (spaceSelected == true) {
setOutput("You have already made a selection");
}else {
setOutput("btn[" + i + "]: That space is not available. Please select another.");
}
}
}
//iterate through each of the named buttons and assign to index tracker selectedRoom.
int selectedRoom = -1;
if (e.getSource() == btnSpa) {
selectedRoom = 0;
buttonPressed = "btnSpa";
btnColumn = 4;
btnRow = 4;
}
if (e.getSource() == btnTheater) {
selectedRoom = 1;
buttonPressed = "btnTheater";
btnColumn = 10;
btnRow = 4;
}
if (e.getSource() == btnLivingRoom) {
selectedRoom = 2;
buttonPressed = "btnLivingRoom";
btnColumn = 10;
btnRow = 4;
}
if (e.getSource() == btnObservatory) {
selectedRoom = 3;
buttonPressed = "btnObservatory";
btnColumn = 30;
btnRow = 4;
}
if (e.getSource() == btnPatio) {
selectedRoom = 4;
buttonPressed = "btnPatio";
btnColumn = 4;
btnRow = 12;
}
if (e.getSource() == btnPool) {
selectedRoom = 5;
buttonPressed = "btnPool";
btnColumn = 17;
btnRow = 12;
}
if (e.getSource() == btnHall) {
selectedRoom = 6;
buttonPressed = "btnHall";
btnColumn = 30;
btnRow = 12;
}
if (e.getSource() == btnKitchen) {
selectedRoom = 7;
buttonPressed = "btnKitchen";
btnColumn = 4;
btnRow = 20;
}
if (e.getSource() == btnDiningRoom) {
selectedRoom = 8;
buttonPressed = "btnDiningRoom";
btnColumn = 17;
btnRow = 20;
}
if (e.getSource() == btnGuestHouse) {
selectedRoom = 9;
buttonPressed = "btnGuestHouse";
btnColumn = 30;
btnRow = 20;
}
//if a room is selected, apply actions to that room
if (selectedRoom > -1) {
//if space is available
if ((allRooms[selectedRoom].getBackground() == Color.cyan) && spaceSelected == false) {
spaceSelected = true;
setOutput("You selected: " + buttonPressed);
newPositionColumn = btnColumn;
newPositionRow = btnRow;
startRumor = true;
} else if (spaceSelected == true) {
setOutput("You have already made a selection.");
} else {
setOutput(buttonPressed + " is not available. Please select another.");
}
}
//create output string and pass to setOutput()
//output = buttonPressed + " was pressed.";
//output += " [positionColumn: " + btnColumn + "]" + "[positionRow: " + btnRow + "]";
//setOutput(output);
}
}
}
/*
* custom class to extend JButton and add references to current position, button number, color, and toolTipText. Used extensively for the spaces.
*/
class myButton extends JButton
{
/*
* instance variables for each button
*/
int positionX;
int positionY;
int button;
private Color defaultColor = Color.white;
private String defaultToolTipText = "";
/*
* default constructor
*/
public myButton()
{
super();
}
/*
* overloaded constructor accepting String as button text
*/
public myButton(String s)
{
super(s);
}
/*
* @return button number
*/
public int getButton()
{
return button;
}
/*
* reset background color to default
*/
public void resetColor()
{
this.setBackground(defaultColor);
}
/*
* @param c - set default color
*/
public void setDefaultColor(Color c)
{
defaultColor = c;
}
/*
* @return defaultColor
*/
public Color getDefaultColor()
{
return defaultColor;
}
/*
* @param s - sets defaultToolTopText
*/
public void setDefaultToolTipText(String s)
{
defaultToolTipText = s;
}
/*
* @return defaultToolTipText
*/
public String getDefaultToolTipText()
{
return defaultToolTipText;
}
/*
* reset toolTipText to default
*/
public void resetToolTipText()
{
this.setToolTipText(defaultToolTipText);
}
/*
* reset text to null
*/
public void resetText()
{
this.setText("");
}
/*
* reset button to defaults
*/
public void reset()
{
this.resetColor();
this.resetToolTipText();
this.resetText();
}
}
/*
* helper class to initialize empty spaces
* @param int x - column of button
* @param int y - row of button
* @param Dimension btnSize - sets the size of the button
*/
public void createBlankButtons(int x, int y, Dimension btnSize)
{
String output = "";
Font clueFont = new Font("Arial", Font.PLAIN, 8);
//initialize next myButton
btn[activeMyButtons] = new myButton();
String toolTip = ("" + activeMyButtons);
btn[activeMyButtons].setDefaultToolTipText(toolTip);
btn[activeMyButtons].setToolTipText(toolTip);
//get next available myButton and assign current position
btn[activeMyButtons].positionX = x;
btn[activeMyButtons].positionY = y;
btn[activeMyButtons].button = activeMyButtons;
btn[activeMyButtons].setPreferredSize(btnSize);
btn[activeMyButtons].setFont(clueFont);
//assign GridBagConstraints for //position
cons.gridx = x;
cons.gridy = y;
cons.gridwidth = 1;
cons.gridheight = 1;
cons.ipadx = 0;
cons.ipady = 0;
//determine position and set alternating color
if ( ((x % 2 == 0) && (y % 2 == 0)) || ((x % 2 != 0) && (y % 2 != 0)) ) {
btn[activeMyButtons].setBackground(new Color(152,86,112));
btn[activeMyButtons].setDefaultColor(new Color(152,86,112));
} else {
btn[activeMyButtons].setBackground(new Color(128,48,82));
btn[activeMyButtons].setDefaultColor(new Color(128,48,82));
}
btn[activeMyButtons].setOpaque(true);
btn[activeMyButtons].setBorderPainted(false);
//add button to boardPanel
this.add(btn[activeMyButtons], cons);
btn[activeMyButtons].addActionListener(new myActionListener());
validate();
//pass output to setOutput()
output = "activeMyButtons: " + activeMyButtons + " " + btn[activeMyButtons].getDefaultColor();
//setOutput(output);
activeMyButtons++;
}
/*
* @param String s - send s to output textArea
*/
public void setOutput(String s)
{
//if (debugBoardPanel) {
bottomText.append("\n" + s);
bottomText.setCaretPosition(bottomText.getDocument().getLength());
//}
}
/*
* method to reset boardPanel to default
*/
public void resetBoard()
{
//iterates through every btn[i] and reset
for (int i = 0; i < activeMyButtons; i++) {
//reset button if it is not occupied
if (btn[i].getText() == "") {
btn[i].reset();
}
}
//reset rooms
for (int i = 0; i < allRooms.length - 1; i++) {
allRooms[i].setBackground(defaultRoomColor);
}
}
/*
* @param char[][] gameBoard - iterate through each button and highlight if it is available
*/
public void redrawBoard(char[][] gameBoard)
{
if (debugBoardPanel) { System.out.println("[492] boardPanel.redrawBoard(): Started."); }
//iterate through each button
for (int i = 0; i < activeMyButtons; i++) {
//switch case for value of btn
switch (gameBoard[btn[i].positionY][btn[i].positionX]) {
case 'A': btn[i].setBackground(Color.blue); //space is available
break;
case 'M': btn[i].setBackground(Color.yellow); //Mustard
btn[i].setText("M");
btn[i].setToolTipText("Colonel Mustard");
break;
case 'S': btn[i].setBackground(Color.red); //Scarlett
btn[i].setText("S");
btn[i].setToolTipText("Miss Scarlett");
break;
case 'W': btn[i].setBackground(Color.white); //White
btn[i].setText("W");
btn[i].setToolTipText("Mrs. White");
break;
case 'B': btn[i].setBackground(Color.blue); //Peacock
btn[i].setText("B");
btn[i].setToolTipText("Peacock");
break;
case 'G': btn[i].setBackground(Color.green); //Green
btn[i].setText("G");
btn[i].setToolTipText("Green");
break;
case 'P': btn[i].setBackground(Color.magenta); //Purple
btn[i].setText("P");
btn[i].setToolTipText("Plum");
break;
case ' ': btn[i].reset();
break;
}
//if (debugBoardPanel) { System.out.println("boardPanel.redrawBoard(): btn[" + i +"]"); }
/*
if (gameBoard[btn[i].positionY][btn[i].positionX] == 'A') {
btn[i].setBackground(Color.blue);
}*/
}
//see if any of the rooms are available
for (int row = 0; row < 25; row++) {
for (int col = 0; col < 35; col++) {
if (gameBoard[row][col] == 'A'){
//spa
if (row < 5 && col < 5) {
btnSpa.setBackground(Color.cyan);
}
//theater
if (row < 5 && (col > 9 && col < 15)) {
btnTheater.setBackground(Color.cyan);
}
//livingRoom
if (row < 5 && (col > 19 && col < 25)) {
btnLivingRoom.setBackground(Color.cyan);
}
//observatory
if (row < 5 && col > 29) {
btnObservatory.setBackground(Color.cyan);
}
//patio
if ((row > 9 && row < 15) && col < 5) {
btnPatio.setBackground(Color.cyan);
}
//pool - don't code yet
//hall
if ((row > 9 && row < 15) && col > 29) {
btnHall.setBackground(Color.cyan);
}
//kitchen
if (row > 19 && col < 5) {
btnKitchen.setBackground(Color.cyan);
}
//diningRoom
if (row > 19 && (col > 10 && col < 26)) {
btnDiningRoom.setBackground(Color.cyan);
}
//guestHouse
if (row > 19 && col > 29) {
btnGuestHouse.setBackground(Color.cyan);
}
}
}
}
if (debugBoardPanel) { System.out.println("[514] boardPanel.redrawBoard(): Complete."); }
}
/*
* @param boardStatus - set status of game
*/
public void setStatus(int boardStatus)
{
status = boardStatus;
}
/*
* @param i - accepts int i for person number
* @return - new positions
*/
public int[] getNewPosition(int i)
{
//logic to auto select for computer player:
if (i != 0)
{
//while space is not selected, randomly select rooms and spaces.
while (spaceSelected == false) {
//generate random number
Random generator = new Random();
int room = generator.nextInt(10);
//random select a room
allRooms[room].doClick();
//generate random space
int space = generator.nextInt(524);
//randomly select a space
btn[space].doClick();
}
}
//if (debugBoardPanel) { System.out.println("[526] boardPanel.getNewPosition(): Called.") ; }
int[] newPositions = {newPositionColumn, newPositionRow};
//if (debugBoardPanel) { System.out.println("[530] boardPanel.getNewPosition().newPositions: " + newPositions[0] + ", " + newPositions[1]); }
return newPositions;
}
/*
* reset everything for nextRoll
*/
public void nextRoll()
{
spaceSelected = false;
newPositionColumn = -1;
newPositionRow = -1;
status = 1;
startRumor = false;
}
/*
* @return startRumor - status of rumor
*/
public boolean getStartRumor()
{
return startRumor;
}
}