-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathRiskView.java
1039 lines (844 loc) · 26.7 KB
/
RiskView.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
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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.util.ArrayList;
import java.lang.StringBuilder;
import java.io.OutputStream;
import java.io.PrintStream;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Font;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultListModel;
import javax.swing.ListSelectionModel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ImageIcon;
import javax.swing.text.DefaultCaret;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
* These classes set up the GUI of the Risk program.
* The main menu, dialog for setting player count, dialog for name/color settings for each
* player, the Risk game board, and a menu used during a Risk game session are included.
* @author Ted Mader
* @version Alpha
* @date 5/02/14
**/
public class RiskView extends JFrame {
private JPanel mainPanel;
private GridLayout mainLayout;
private JButton newGameButton;
private JButton loadGameButton;
private JButton quitButton;
private String newGameButtonName = "newGameBtn";
private String loadGameButtonName = "loadGameBtn";
private String quitButtonName = "quitBtn";
/**
* Constructs the main menu.
**/
protected RiskView()
{
setTitle("Java-Risk");
setPreferredSize(new Dimension(300, 300));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
add(mainMenu());
pack();
setVisible(true);
toFront();
}
private JPanel mainMenu()
{
// Creates the panel
mainPanel = new JPanel();
// Sets Layout
mainLayout = new GridLayout(3, 1, 5, 5);
mainPanel.setLayout(mainLayout);
// Creates buttons
newGameButton = new JButton("New Game");
loadGameButton = new JButton("Load Game");
quitButton = new JButton("Quit");
// Sets button commands
newGameButton.setActionCommand(newGameButtonName);
loadGameButton.setActionCommand(loadGameButtonName);
quitButton.setActionCommand(quitButtonName);
// Adds buttons to mainPanel
mainPanel.add(newGameButton);
mainPanel.add(loadGameButton);
mainPanel.add(quitButton);
return mainPanel;
}
// Action listeners for riskView
protected void riskViewActionListeners(ActionListener evt)
{
newGameButton.addActionListener(evt);
loadGameButton.addActionListener(evt);
quitButton.addActionListener(evt);
}
}
class PlayerCountDialog extends JDialog {
private JPanel playerCountPanel;
private GridLayout playerCountLayout;
private JLabel playerCountLabel;
private JButton threePlayersBtn;
private JButton fourPlayersBtn;
private JButton fivePlayersBtn;
private JButton sixPlayersBtn;
private JButton backBtn;
private String threePlayersBtnName = "threePlayersBtn";
private String fourPlayersBtnName = "fourPlayersBtn";
private String fivePlayersBtnName = "fivePlayersBtn";
private String sixPlayersBtnName = "sixPlayersBtn";
private String backBtnName = "backBtn";
/**
* Constructs the dialog for player count selection.
**/
protected PlayerCountDialog(RiskView owner, boolean modality)
{
super(owner, modality);
setTitle("Java-Risk");
setPreferredSize(new Dimension(150, 280));
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setResizable(false);
add(playerCountPanel());
setLocationRelativeTo(null);
pack();
}
/**
* The main panel.
**/
private JPanel playerCountPanel()
{
playerCountPanel = new JPanel();
playerCountLayout = new GridLayout(6, 1, 5, 5);
playerCountPanel.setLayout(playerCountLayout);
playerCountLabel = new JLabel("Number of Leaders:");
threePlayersBtn = new JButton("Three");
fourPlayersBtn = new JButton("Four");
fivePlayersBtn = new JButton("Five");
sixPlayersBtn = new JButton("Six");
backBtn = new JButton ("Back");
threePlayersBtn.setActionCommand(threePlayersBtnName);
fourPlayersBtn.setActionCommand(fourPlayersBtnName);
fivePlayersBtn.setActionCommand(fivePlayersBtnName);
sixPlayersBtn.setActionCommand(sixPlayersBtnName);
backBtn.setActionCommand(backBtnName);
playerCountPanel.add(playerCountLabel);
playerCountPanel.add(threePlayersBtn);
playerCountPanel.add(fourPlayersBtn);
playerCountPanel.add(fivePlayersBtn);
playerCountPanel.add(sixPlayersBtn);
playerCountPanel.add(backBtn);
return playerCountPanel;
}
/**
* Adds the action listeners.
**/
protected void addActionListeners(ActionListener evt)
{
threePlayersBtn.addActionListener(evt);
fourPlayersBtn.addActionListener(evt);
fivePlayersBtn.addActionListener(evt);
sixPlayersBtn.addActionListener(evt);
backBtn.addActionListener(evt);
}
}
class PlayerSettingsDialog extends JDialog {
private JPanel playerNamesPanel;
private JPanel playerTypesPanel;
private GridLayout mainLayout;
private GridLayout playerNamesLayout;
private GridLayout playerTypesLayout;
private JButton startBtn;
private JButton backBtn;
private String startBtnName = "startBtn";
private String backBtnName = "backBtn";
private JTextField player1TextField;
private JTextField player2TextField;
private JTextField player3TextField;
private JTextField player4TextField;
private JTextField player5TextField;
private JTextField player6TextField;
private JComboBox player1ComboBox;
private JComboBox player2ComboBox;
private JComboBox player3ComboBox;
private JComboBox player4ComboBox;
private JComboBox player5ComboBox;
private JComboBox player6ComboBox;
private int playerCount;
private String[] types = { "Human", "AI" };
public PlayerSettingsDialog(PlayerCountDialog owner, boolean modality, int playerCount)
{
super(owner, modality);
setTitle("Java-Risk");
this.playerCount = playerCount;
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setResizable(false);
mainLayout = new GridLayout(1, 2, 5, 5); // Make second parameter '2' if including playerTypesPanel
setLayout(mainLayout);
add(playerNamesPanel());
add(playerTypesPanel());
setLocationRelativeTo(null);
pack();
}
private JPanel playerNamesPanel() {
playerNamesPanel = new JPanel();
playerNamesPanel.setPreferredSize(new Dimension(200, playerCount * 40 + 40));
playerNamesLayout = new GridLayout(playerCount + 1, 1, 5, 5);
playerNamesPanel.setLayout(playerNamesLayout);
player1TextField = new JTextField("Theodoric");
player2TextField = new JTextField("Napoleon");
player3TextField = new JTextField("Alexander");
playerNamesPanel.add(player1TextField);
playerNamesPanel.add(player2TextField);
playerNamesPanel.add(player3TextField);
if (playerCount > 3) {
player4TextField = new JTextField("William");
playerNamesPanel.add(player4TextField);
}
if (playerCount > 4) {
player5TextField = new JTextField("Georgy");
playerNamesPanel.add(player5TextField);
}
if (playerCount > 5) {
player6TextField = new JTextField("Cyrus");
playerNamesPanel.add(player6TextField);
}
backBtn = new JButton ("Back");
backBtn.setActionCommand(backBtnName);
playerNamesPanel.add(backBtn);
return playerNamesPanel;
}
private JPanel playerTypesPanel() {
playerTypesPanel = new JPanel();
playerTypesPanel.setPreferredSize(new Dimension(200, playerCount * 40 + 40));
playerTypesLayout = new GridLayout(playerCount + 1, 1, 5, 5);
playerTypesPanel.setLayout(playerTypesLayout);
player1ComboBox = new JComboBox(types);
player2ComboBox = new JComboBox(types);
player3ComboBox = new JComboBox(types);
playerTypesPanel.add(player1ComboBox);
playerTypesPanel.add(player2ComboBox);
playerTypesPanel.add(player3ComboBox);
if (playerCount > 3) {
player4ComboBox = new JComboBox(types);
playerTypesPanel.add(player4ComboBox);
}
if (playerCount > 4) {
player5ComboBox = new JComboBox(types);
playerTypesPanel.add(player5ComboBox);
}
if (playerCount > 5) {
player6ComboBox = new JComboBox(types);
playerTypesPanel.add(player6ComboBox);
}
startBtn = new JButton ("Start Game");
startBtn.setActionCommand(startBtnName);
playerTypesPanel.add(startBtn);
return playerTypesPanel;
}
public void addActionListeners(ActionListener evt)
{
startBtn.addActionListener(evt);
backBtn.addActionListener(evt);
}
// Get methods for text fields
protected String getPlayerTextField(int playerNum)
{
if (playerNum == 1)
{
return player1TextField.getText();
}
else if (playerNum == 2)
{
return player2TextField.getText();
}
else if (playerNum == 3)
{
return player3TextField.getText();
}
else if (playerNum == 4)
{
return player4TextField.getText();
}
else if (playerNum == 5)
{
return player5TextField.getText();
}
else
{
return player6TextField.getText();
}
}
// Get methods for combo boxes
protected String getPlayerComboBox(int playerNum)
{
if (playerNum == 1)
{
return player1ComboBox.getSelectedItem().toString();
}
else if (playerNum == 2)
{
return player2ComboBox.getSelectedItem().toString();
}
else if (playerNum == 3)
{
return player3ComboBox.getSelectedItem().toString();
}
else if (playerNum == 4)
{
return player4ComboBox.getSelectedItem().toString();
}
else if (playerNum == 5)
{
return player5ComboBox.getSelectedItem().toString();
}
else
{
return player6ComboBox.getSelectedItem().toString();
}
}
}
class BoardView extends JDialog {
private int i;
private String[] countriesArray;
private JPanel messagePanel;
private JPanel mapPanel;
private JPanel actionPanel;
private JPanel countryInfoPanel;
private JPanel northAmericaPanel;
private JPanel southAmericaPanel;
private JPanel europePanel;
private JPanel africaPanel;
private JPanel asiaPanel;
private JPanel australiaPanel;
private GridBagConstraints c;
private GridBagLayout mainLayout;
private GridBagLayout messageLayout;
private GridBagLayout actionLayout;
private JLabel selectedLabel;
private JLabel targetLabel;
private JLabel continentLabel;
private String menuBtnName = "menuBtn";
private String reinforceBtnName = "reinforceBtn";
private String attackBtnName = "attackBtn";
private String fortifyBtnName = "fortifyBtn";
private String turnInBtnName = "turnInBtn";
private String endTurnBtnName = "endTurnBtn";
private JButton menuBtn;
private JButton reinforceBtn;
private JButton attackBtn;
private JButton fortifyBtn;
private JButton turnInBtn;
private JButton endTurnBtn;
private JTextArea printTextArea;
private JComboBox selectedComboBox;
private JComboBox targetComboBox;
private JList cardsList;
private JList countryAList;
private JList countryBList;
private JScrollPane mapScrollPane;
private JScrollPane messageScrollPane;
private JScrollPane cardsListScrollPane;
private JScrollPane countryBScrollPane;
private JScrollPane countryAScrollPane;
private DefaultCaret caret;
private ImageIcon mapImageIcon;
private RiskModel model;
private RiskListModel cardsListModel;
private RiskListModel countryAListModel;
private RiskListModel countryBListModel;
private CountryLabel countryLabel;
/**
* Constructs the Risk game board.
**/
public BoardView(PlayerSettingsDialog owner, boolean modality, RiskModel model) {
super(owner, modality);
setTitle("Java-Risk");
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setResizable(false);
this.model = model;
// GridBagLayout allows a flexible sizing of components
mainLayout = new GridBagLayout();
setLayout(mainLayout);
c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.LINE_START;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 0;
add(messagePanel());
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.CENTER;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 8;
c.weighty = 0.5;
c.gridx = 1;
c.gridy = 0;
add(mapPanel());
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.LINE_END;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 2;
c.gridy = 0;
add(actionPanel());
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.LINE_END;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 2;
c.gridy = 0;
add(countryInfoPanel(model.getBoard()));
setLocationRelativeTo(null);
pack();
}
/**
* Constructor for loading the game.
**/
public BoardView(RiskView owner, boolean modality, RiskModel model) {
super(owner, modality);
setTitle("Java-Risk");
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setResizable(false);
this.model = model;
// GridBagLayout allows a flexible sizing of components
mainLayout = new GridBagLayout();
setLayout(mainLayout);
c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.LINE_START;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 0;
add(messagePanel());
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.CENTER;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 8;
c.weighty = 0.5;
c.gridx = 1;
c.gridy = 0;
add(mapPanel());
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.LINE_END;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 2;
c.gridy = 0;
add(actionPanel());
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.LINE_END;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 2;
c.gridy = 0;
add(countryInfoPanel(model.getBoard()));
setLocationRelativeTo(null);
pack();
}
/**
* The panel for the card display and turn-in button.
**/
private JPanel messagePanel() {
messagePanel = new JPanel();
messagePanel.setPreferredSize(new Dimension(300, 980));
messageLayout = new GridBagLayout();
messagePanel.setLayout(messageLayout);
c = new GridBagConstraints();
printTextArea = new JTextArea();
System.setOut(new PrintStream(new TextAreaOutputStream(printTextArea)));
printTextArea.setFocusable(false);
printTextArea.setLineWrap(true);
printTextArea.setWrapStyleWord(true);
caret = (DefaultCaret)printTextArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
messageScrollPane = new JScrollPane(printTextArea);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 14;
c.gridx = 0;
c.gridy = 0;
messagePanel.add(messageScrollPane, c);
return messagePanel;
}
/**
* The panel containing the scrollable Risk map image.
**/
private JPanel mapPanel() {
mapPanel = new JPanel();
mapPanel.setLayout(new GridLayout(1, 1, 5, 5));
mapImageIcon = new ImageIcon("Map.jpg");
mapScrollPane = new JScrollPane(new JLabel(mapImageIcon));
mapScrollPane.setPreferredSize(new Dimension(1080, 980));
mapPanel.add(mapScrollPane);
return mapPanel;
}
/**
* The panel for the buttons which allow the user to perform actions.
**/
private JPanel actionPanel() {
actionPanel = new JPanel();
actionPanel.setPreferredSize(new Dimension(200, 980));
actionLayout = new GridBagLayout();
actionPanel.setLayout(actionLayout);
selectedLabel = new JLabel("Selected Territory:");
targetLabel = new JLabel("Adjacent Territory:");
menuBtn = new JButton("Menu");
turnInBtn = new JButton("Turn In Cards");
reinforceBtn = new JButton("Place Reinforcements");
attackBtn = new JButton("Attack!");
fortifyBtn = new JButton("Fortify");
endTurnBtn = new JButton("End Turn");
menuBtn.setActionCommand(menuBtnName);
turnInBtn.setActionCommand(turnInBtnName);
reinforceBtn.setActionCommand(reinforceBtnName);
attackBtn.setActionCommand(attackBtnName);
fortifyBtn.setActionCommand(fortifyBtnName);
endTurnBtn.setActionCommand(endTurnBtnName);
cardsListModel = new RiskListModel(model, "cards");
countryAListModel = new RiskListModel(model, "countryA");
countryBListModel = new RiskListModel(model, "countryB");
model.addObserver((RiskListModel)cardsListModel);
model.addObserver((RiskListModel)countryAListModel);
model.addObserver((RiskListModel)countryBListModel);
cardsList = new JList(cardsListModel);
cardsList.setLayoutOrientation(JList.VERTICAL_WRAP);
cardsList.setVisibleRowCount(6);
countryAList = new JList(countryAListModel);
countryAList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
countryAList.setLayoutOrientation(JList.VERTICAL_WRAP);
countryAList.setVisibleRowCount(42);
countryBList = new JList(countryBListModel);
countryBList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
countryBList.setLayoutOrientation(JList.VERTICAL_WRAP);
countryBList.setVisibleRowCount(6);
countryAScrollPane = new JScrollPane(countryAList);
countryBScrollPane = new JScrollPane(countryBList);
c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 0;
actionPanel.add(menuBtn, c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 5;
c.gridx = 0;
c.gridy = 2;
actionPanel.add(cardsList, c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 3;
actionPanel.add(turnInBtn, c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 4;
actionPanel.add(selectedLabel, c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 10;
c.gridx = 0;
c.gridy = 5;
actionPanel.add(countryAScrollPane, c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 6;
actionPanel.add(reinforceBtn, c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 7;
actionPanel.add(targetLabel, c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 10;
c.gridx = 0;
c.gridy = 8;
actionPanel.add(countryBScrollPane, c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 9;
actionPanel.add(attackBtn, c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 10;
actionPanel.add(fortifyBtn, c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 11;
actionPanel.add(endTurnBtn, c);
return actionPanel;
}
private JPanel countryInfoPanel(Board board) {
countryInfoPanel = new JPanel();
countryInfoPanel.setPreferredSize(new Dimension(320, 980));
countryInfoPanel.setLayout(new GridBagLayout());
northAmericaPanel = new JPanel();
northAmericaPanel.setPreferredSize(new Dimension(320, 100));
northAmericaPanel.setLayout(new GridLayout(5, 2, 5, 5));
for (i = 0; i < board.getMemberCountries("North America").size(); i++) {
countryLabel = new CountryLabel(model, board.getMemberCountries("North America").get(i));
countryLabel.setFont(new Font("Arial", Font.PLAIN, 10));
northAmericaPanel.add(countryLabel);
model.addObserver((CountryLabel) countryLabel);
}
southAmericaPanel = new JPanel();
southAmericaPanel.setPreferredSize(new Dimension(320, 60));
southAmericaPanel.setLayout(new GridLayout(3, 2, 5, 5));
for (i = 0; i < board.getMemberCountries("South America").size(); i++) {
countryLabel = new CountryLabel(model, board.getMemberCountries("South America").get(i));
countryLabel.setFont(new Font("Arial", Font.PLAIN, 10));
southAmericaPanel.add(countryLabel);
model.addObserver((CountryLabel) countryLabel);
}
europePanel = new JPanel();
europePanel.setPreferredSize(new Dimension(320, 80));
europePanel.setLayout(new GridLayout(4, 2, 5, 5));
for (i = 0; i < board.getMemberCountries("Europe").size(); i++) {
countryLabel = new CountryLabel(model, board.getMemberCountries("Europe").get(i));;
countryLabel.setFont(new Font("Arial", Font.PLAIN, 10));
europePanel.add(countryLabel);
model.addObserver((CountryLabel) countryLabel);
}
africaPanel = new JPanel();
africaPanel.setPreferredSize(new Dimension(320, 60));
africaPanel.setLayout(new GridLayout(3, 2, 5, 5));
for (i = 0; i < board.getMemberCountries("Africa").size(); i++) {
countryLabel = new CountryLabel(model, board.getMemberCountries("Africa").get(i));
countryLabel.setFont(new Font("Arial", Font.PLAIN, 10));
africaPanel.add(countryLabel);
model.addObserver((CountryLabel) countryLabel);
}
asiaPanel = new JPanel();
asiaPanel.setPreferredSize(new Dimension(320, 120));
asiaPanel.setLayout(new GridLayout(6, 2, 5, 5));
for (i = 0; i < board.getMemberCountries("Asia").size(); i++) {
countryLabel = new CountryLabel(model, board.getMemberCountries("Asia").get(i));
countryLabel.setFont(new Font("Arial", Font.PLAIN, 10));
asiaPanel.add(countryLabel);
model.addObserver((CountryLabel) countryLabel);
}
australiaPanel = new JPanel();
australiaPanel.setPreferredSize(new Dimension(320, 60));
australiaPanel.setLayout(new GridLayout(3, 2, 5, 5));
for (i = 0; i < board.getMemberCountries("Australia").size(); i++) {
countryLabel = new CountryLabel(model, board.getMemberCountries("Australia").get(i));
countryLabel.setFont(new Font("Arial", Font.PLAIN, 10));
australiaPanel.add(countryLabel);
model.addObserver((CountryLabel) countryLabel);
}
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 0;
countryInfoPanel.add(new JLabel("North America"), c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 1;
countryInfoPanel.add(northAmericaPanel, c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 2;
countryInfoPanel.add(new JLabel("South America"), c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 3;
countryInfoPanel.add(southAmericaPanel, c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 4;
countryInfoPanel.add(new JLabel("Europe"), c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 5;
countryInfoPanel.add(europePanel, c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 6;
countryInfoPanel.add(new JLabel("Africa"), c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 7;
countryInfoPanel.add(africaPanel, c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 8;
countryInfoPanel.add(new JLabel("Asia"), c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 9;
countryInfoPanel.add(asiaPanel, c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 10;
countryInfoPanel.add(new JLabel("Australia"), c);
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 11;
countryInfoPanel.add(australiaPanel, c);
return countryInfoPanel;
}
/**
* Adds the action listeners for the buttons and lists.
**/
protected void addActionListeners(ActionListener evt1, ListSelectionListener evt2) {
menuBtn.addActionListener(evt1);
turnInBtn.addActionListener(evt1);
reinforceBtn.addActionListener(evt1);
attackBtn.addActionListener(evt1);
fortifyBtn.addActionListener(evt1);
endTurnBtn.addActionListener(evt1);
countryAList.addListSelectionListener(evt2);
}
/**
* Used for adjusting the country B list according to the country A selecion.
* @return the integer of the selected index in the country A list.
**/
protected int getCountryAIndex() {
return countryAList.getSelectedIndex();
}
/**
* Passes the indices of the cards to remove from the current player's hand.
* @return the array of selected indices in the cards list.
**/
protected int[] getCardsToRemove() {
return cardsList.getSelectedIndices();
}
/**
* Passes countryA for the model.
* @return the String of the selected value in country A list.
**/
protected String getCountryA() {
return countryAList.getSelectedValue().toString();
}
/**
* Passes countryB for the model.
* @return the String of the selected value in country B list.
**/
protected String getCountryB() {
return countryBList.getSelectedValue().toString();
}
}
class MenuDialog extends JDialog {
private JPanel menuPanel;
private GridLayout menuLayout;
private JButton returnBtn;
private JButton saveBtn;
private JButton quitBtn;
private String returnBtnName = "returnBtn";
private String saveBtnName = "saveBtn";
private String quitBtnName = "quitBtn";
public MenuDialog(BoardView owner, boolean modality)
{
super(owner, modality);
setTitle("Java-Risk");
setPreferredSize(new Dimension(200, 120));