-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameSettingGUI.java
249 lines (218 loc) · 6.47 KB
/
GameSettingGUI.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JCheckBoxMenuItem;
public class GameSettingGUI implements ActionListener
{
JFrame frame;
JPanel basePanel;
JPanel upperPanel;
JPanel lowerPanel;
JLabel player_name1;
JLabel player_name2;
JTextField name1;
JTextField name2;
JCheckBox red_p1;
JCheckBox blue_p1;
JCheckBox red_p2;
JCheckBox blue_p2;
JCheckBox opt1, opt2;
JButton confirm;
JButton reset;
GameControl control;
private GridBagConstraints gbc = new GridBagConstraints();
// contructor that initializes all the different labels and fields in the settings
public GameSettingGUI(ConfirmPressed info)
{
frame = new JFrame("Game Setting");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
basePanel = new JPanel();
upperPanel = new JPanel();
lowerPanel = new JPanel();
player_name1 = new JLabel("Player Name: ");
player_name2 = new JLabel("Player Name: ");
name1 = new JTextField(30);
name2 = new JTextField(30);
red_p1 = new JCheckBox("Red");
blue_p1 = new JCheckBox("Blue");
red_p2 = new JCheckBox("Red");
blue_p2 = new JCheckBox("Blue");
red_p1.addActionListener(this);
red_p2.addActionListener(this);
blue_p1.addActionListener(this);
blue_p2.addActionListener(this);
opt1 = new JCheckBox("8x8");
opt2 = new JCheckBox("6x6");
opt1.addActionListener(this);
opt2.addActionListener(this);
confirm = new JButton("Confirm");
confirm.addActionListener(info);
reset = new JButton("Reset");
reset.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
red_p1.setEnabled(true);
red_p1.setSelected(false);
red_p2.setEnabled(true);
red_p2.setSelected(false);
blue_p1.setEnabled(true);
blue_p1.setSelected(false);
blue_p2.setEnabled(true);
blue_p2.setSelected(false);
opt1.setEnabled(true);
opt1.setSelected(false);
opt2.setEnabled(true);
opt2.setSelected(false);
}});
basePanel.setLayout(new GridLayout(2, 0));
upperPanel.setLayout(new GridBagLayout());
lowerPanel.setLayout(new GridBagLayout());
//---------------------------------------------------
// Player Label
gbc.gridx = 0;
gbc.gridy = 0;
upperPanel.add(player_name1, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
upperPanel.add(player_name2, gbc);
//----------------------------------------------------
// Player name text field
gbc.gridx = 1;
gbc.gridy = 0;
upperPanel.add(name1, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
upperPanel.add(name2, gbc);
//----------------------------------------------------
// Player turn pick
gbc.gridx = 2;
gbc.gridy = 0;
upperPanel.add(red_p1, gbc);
gbc.gridx = 3;
gbc.gridy = 0;
upperPanel.add(blue_p1, gbc);
gbc.gridx = 2;
gbc.gridy = 1;
upperPanel.add(red_p2, gbc);
gbc.gridx = 3;
gbc.gridy = 1;
upperPanel.add(blue_p2, gbc);
//-----------------------------------------------------
// Board Options
gbc.gridx = 0;
gbc.gridy = 2;
upperPanel.add(new JLabel("Board Choice"), gbc);
gbc.gridx = 1;
gbc.gridy = 2;
upperPanel.add(opt1, gbc);
gbc.gridx = 2;
gbc.gridy = 2;
upperPanel.add(opt2, gbc);
//-----------------------------------------------------
// confirm button
gbc.gridx = 0;
gbc.gridy = 3;
lowerPanel.add(confirm, gbc);
//------------------------------------------------------
// reset button
gbc.gridx = 0;
gbc.gridx = 4;
lowerPanel.add(reset, gbc);
//-----------------------------------------------------
basePanel.add(upperPanel);
basePanel.add(lowerPanel);
frame.add(basePanel);
frame.pack();
frame.setVisible(true);
}
// returns the names of the players from fields
public String[] getPlayerName()
{
String[] names = new String[2];
names[0] = name1.getText();
names[1] = name2.getText();
return names;
}
// returns the color options of the players from the check boxes
public String[] getPlayerColor()
{
// the first element is player 1's color, the second is player2's
String[] color = new String[2];
if(red_p1.isSelected())
color[0] = "b";
else
color[0] = "w";
if(color[0].equals("b"))
color[1] = "w";
else
color[1] = "b";
return color;
}
// returns the board size from the options selected
public int getBoardSize()
{
if(opt2.isSelected())
return 6;
else
return 8;
}
// closes the window on clicking confirm
public void closeWindow()
{
frame.dispose();
}
// calls the functions that make sure that one of each check boxes is selected
public void actionPerformed(ActionEvent event)
{
boardSizeSelection();
colorSelection();
}
// disables impossible options for choosing colors
public void colorSelection()
{
if(red_p1.isSelected())
{
blue_p1.setEnabled(false);
red_p2.setEnabled(false);
}
if(red_p2.isSelected())
{
blue_p2.setEnabled(false);
red_p1.setEnabled(false);
}
if(blue_p1.isSelected())
{
blue_p2.setEnabled(false);
red_p1.setEnabled(false);
}
if(blue_p2.isSelected())
{
blue_p1.setEnabled(false);
red_p2.setEnabled(false);
}
}
// disables options for choosing sizes
public void boardSizeSelection()
{
if(opt1.isSelected())
opt2.setEnabled(false);
if(opt2.isSelected())
opt1.setEnabled(false);
}
// making sure that all options have been selected before allowing confirm button to start game
public boolean confirmFlag()
{
if(red_p1.isSelected() || red_p2.isSelected())
if(blue_p1.isSelected() || blue_p2.isSelected())
if(opt1.isSelected() || opt2.isSelected())
return true;
else
return false;
return false;
}
}