-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainMenuPanel.java
More file actions
359 lines (310 loc) · 12.4 KB
/
MainMenuPanel.java
File metadata and controls
359 lines (310 loc) · 12.4 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
/**
* This class displays the main menu of the game. It contains a
* levelSelect panel, a randomSelect panel, and an instructions button.
* <p>
* Priscilla was primarily responsible for the implementation of this class.
*
* @author Priscilla Lee
* @version December 11 2014
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MainMenuPanel extends JPanel {
//private final vars
private final int numberOfLevels = 26;
//private instance vars
private JPanel levelSelect, randomSelect, createSelect, customPlay;
private JLabel level, randomSize, customSize, custom, error;
private JButton instructions, random, play, create, playCustom;
private JTextField fileNameInput;
private JComboBox numbers, randomSizes, customSizes;
private GamePanel gamePanel;
/**
* Constructor that takes in a GamePanel as input.
*
* @param gp the GamePanel that will contain this panel
*/
public MainMenuPanel(GamePanel gp) {
//store containing panel
gamePanel = gp;
//setting background & layout & size
setPreferredSize(new Dimension(500,300));
setMaximumSize(new Dimension(500,300));
setBackground(ColorScheme.background());
setLayout(new GridLayout(6,1));
//initialize instructions, design, add
instructions = new JButton("<html><font color='white'>INSTRUCTIONS</font><html>");
instructions.setFont(new Font("Calibri", Font.BOLD, 30));
instructions.setOpaque(true);
instructions.setBackground(ColorScheme.instructions());
instructions.setBorder(BorderScheme.padding());
instructions.addActionListener(new ButtonListener());
add(instructions);
//initialize level label, adjust design
level = new JLabel("<html><font color='white'>LEVEL</font><html>");
level.setFont(new Font("Calibri", Font.BOLD, 30));
level.setHorizontalAlignment(JLabel.CENTER);
level.setOpaque(true);
level.setBackground(ColorScheme.labels());
level.setBorder(BorderScheme.horzPadding());
//initialize numbers, adjust design
String[] choices = new String[numberOfLevels];
for (int i = 0; i < choices.length; i++)
choices[i] = "" + (i+1);
numbers = new JComboBox(choices);
numbers.setFont(new Font("Calibri", Font.BOLD, 30));
numbers.setOpaque(true);
numbers.setBackground(Color.WHITE);
numbers.setBorder(BorderScheme.horzPadding());
//initialize play button, adjust design
play = new JButton("<html><font color='white'>PLAY</font><html>");
play.setFont(new Font("Calibri", Font.BOLD, 30));
play.setOpaque(true);
play.setBackground(ColorScheme.play());
play.setBorder(BorderScheme.horzPadding());
play.addActionListener(new ButtonListener());
//initialize levelSelect panel, which contains label + drop-down + button
levelSelect = new JPanel();
levelSelect.setBackground(ColorScheme.background());
levelSelect.setLayout(new GridBagLayout());
levelSelect.setBorder(BorderScheme.padding());
add(levelSelect);
//use grid bag constraints to add label+dropdown+button
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
//add level label
c.weightx = 0.37;
c.gridx = 0;
c.gridy = 0;
c.ipady = 10;
levelSelect.add(level,c);
//add numbers drop-down combo box
c.weightx = 0.1;
c.gridx = 1;
levelSelect.add(numbers,c);
//add play button
c.weightx = 0.53;
c.gridx = 2;
levelSelect.add(play,c);
//initialize randomSize label, adjust design
randomSize = new JLabel("<html><font color='white'>SIZE</font><html>");
randomSize.setFont(new Font("Calibri", Font.BOLD, 30));
randomSize.setHorizontalAlignment(JLabel.CENTER);
randomSize.setOpaque(true);
randomSize.setBackground(ColorScheme.labels());
randomSize.setBorder(BorderScheme.horzPadding());
//initialize randomSizes combobox, adjust design
String[] randomOptions = new String[11];
for (int i = 0; i < randomOptions.length; i++)
randomOptions[i] = "" + (i+10);
randomSizes = new JComboBox(randomOptions);
randomSizes.setFont(new Font("Calibri", Font.BOLD, 30));
randomSizes.setOpaque(true);
randomSizes.setBackground(Color.WHITE);
randomSizes.setBorder(BorderScheme.horzPadding());
//initialize random button, adjust design
random = new JButton("<html><font color='white'>RANDOM</font><html>");
random.setFont(new Font("Calibri", Font.BOLD, 30));
random.setOpaque(true);
random.setBackground(ColorScheme.random());
random.setBorder(BorderScheme.horzPadding());
random.addActionListener(new ButtonListener());
//initialize randomSelect panel, which contains label + dropdown + button
randomSelect = new JPanel();
randomSelect.setBackground(ColorScheme.background());
randomSelect.setLayout(new GridBagLayout());
randomSelect.setBorder(BorderScheme.padding());
add(randomSelect);
//use grid bag constraints to add label + dropdown + button
GridBagConstraints d = new GridBagConstraints();
d.fill = GridBagConstraints.BOTH;
//add size label
d.weightx = 0.47;
d.gridx = 0;
d.gridy = 0;
d.ipady = 10;
randomSelect.add(randomSize,d);
//add sizes drop-down combo box
d.weightx = 0.12;
d.gridx = 1;
randomSelect.add(randomSizes,d);
//add play button
d.weightx = 0.41;
d.gridx = 2;
randomSelect.add(random,d);
//initialize customSize label, adjust design
customSize = new JLabel("<html><font color='white'>SIZE</font><html>");
customSize.setFont(new Font("Calibri", Font.BOLD, 30));
customSize.setHorizontalAlignment(JLabel.CENTER);
customSize.setOpaque(true);
customSize.setBackground(ColorScheme.labels());
customSize.setBorder(BorderScheme.horzPadding());
//initialize customSizes, adjust design
String[] customOptions = new String[11];
for (int i = 0; i < customOptions.length; i++)
customOptions[i] = "" + (i+10);
customSizes = new JComboBox(customOptions);
customSizes.setFont(new Font("Calibri", Font.BOLD, 30));
customSizes.setOpaque(true);
customSizes.setBackground(Color.WHITE);
customSizes.setBorder(BorderScheme.horzPadding());
//initialize create button, adjust design
create = new JButton("<html><font color='white'>CREATE</font><html>");
create.setFont(new Font("Calibri", Font.BOLD, 30));
create.setOpaque(true);
create.setBackground(ColorScheme.create());
create.setBorder(BorderScheme.horzPadding());
create.addActionListener(new ButtonListener());
//initialize createSelect panel, which contains label + drop-down + button
createSelect = new JPanel();
createSelect.setBackground(ColorScheme.background());
createSelect.setLayout(new GridBagLayout());
createSelect.setBorder(BorderScheme.padding());
add(createSelect);
//use grid bag constraints to add label+dropdown+button
GridBagConstraints e = new GridBagConstraints();
e.fill = GridBagConstraints.BOTH;
//add level label
e.weightx = 0.46;
e.gridx = 0;
e.gridy = 0;
e.ipady = 10;
createSelect.add(customSize,e);
//add numbers drop-down combo box
e.weightx = 0.12;
e.gridx = 1;
createSelect.add(customSizes,e);
//add play button
e.weightx = 0.46;
e.gridx = 2;
createSelect.add(create,e);
//initialize custom label, adjust design
custom = new JLabel("<html><font color='white'>CUSTOM LEVEL</font><html>");
custom.setFont(new Font("Calibri", Font.BOLD, 30));
custom.setHorizontalAlignment(JLabel.CENTER);
custom.setOpaque(true);
custom.setBackground(ColorScheme.labels());
custom.setBorder(BorderScheme.horzPadding());
//initialize fileNameInput, adjust design
fileNameInput = new JTextField("Enter the level name", 13);
fileNameInput.setFont(new Font("Calibri", Font.BOLD, 20));
fileNameInput.setOpaque(true);
fileNameInput.setBackground(Color.WHITE);
fileNameInput.setBorder(BorderScheme.horzPadded());
//initialize playCustom button, adjust design
playCustom = new JButton("<html><font color='white'>PLAY</font><html>");
playCustom.setFont(new Font("Calibri", Font.BOLD, 30));
playCustom.setOpaque(true);
playCustom.setBackground(ColorScheme.playCustom());
playCustom.setBorder(BorderScheme.horzPadding());
playCustom.addActionListener(new ButtonListener());
//initialize customPlay panel, which contains label + text field + button
customPlay = new JPanel();
customPlay.setBackground(ColorScheme.background());
customPlay.setLayout(new GridBagLayout());
customPlay.setBorder(BorderScheme.padding());
add(customPlay);
//use grid bag constraints to add label+dropdown+button
GridBagConstraints f = new GridBagConstraints();
f.fill = GridBagConstraints.BOTH;
//add level label
f.weightx = 0.25;
f.gridx = 0;
f.gridy = 0;
f.ipady = 15;
customPlay.add(custom,f);
//add numbers drop-down combo box
f.weightx = 0.6;
f.gridx = 1;
customPlay.add(fileNameInput,f);
//add play button
f.weightx = 0.2;
f.gridx = 2;
customPlay.add(playCustom,f);
//initialize error label, adjust design, add
error = new JLabel();
error.setFont(new Font("Calibri", Font.BOLD, 20));
error.setOpaque(true);
error.setBackground(ColorScheme.background());
error.setBorder(BorderScheme.padding());
error.setHorizontalAlignment(JLabel.CENTER);
add(error);
}
/**
* Private helper method that sets the text on the error label given
* the error message as input.
*
* @param msg the message to be displayed on the error label
*/
private void setError(String msg) {
error.setText("<html><font color='white'>"+msg+"</font></html>");
error.setBackground(ColorScheme.bad());
}
/**
* Private inner class that indicates the appropriate action when a button
* is clicked.
*/
private class ButtonListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
if (event.getSource() == play) {
try { //play button pressed --> construct new level panel (level 1)
int level = numbers.getSelectedIndex() + 1;
GridGraph gg = new GridGraph(level);
LevelPanel levelPan = new LevelPanel(gamePanel, gg);
gamePanel.add(levelPan);
gamePanel.remove(MainMenuPanel.this);
gamePanel.revalidate();
gamePanel.repaint();
revalidate();
repaint();
} catch (FileNotFoundException ex) {
System.out.println(ex);
}
} else if (event.getSource() == random) {
//random button pressed, generate random levelpanel
int size = randomSizes.getSelectedIndex() + 10;
GridGraph gg = new GridGraph(size,size);
LevelPanel levelPan = new LevelPanel(gamePanel, gg);
gamePanel.add(levelPan);
gamePanel.remove(MainMenuPanel.this);
gamePanel.revalidate();
gamePanel.repaint();
revalidate();
repaint();
} else if (event.getSource() == instructions) {
//instructions button is pressed, switch screens to instructions panel
gamePanel.add(new InstructionsPanel(gamePanel));
gamePanel.remove(MainMenuPanel.this);
gamePanel.revalidate();
gamePanel.repaint();
revalidate();
repaint();
} else if (event.getSource() == create) {
int dimension = customSizes.getSelectedIndex() + 10;
gamePanel.add(new CreatePanel(gamePanel, dimension, dimension));
gamePanel.remove(MainMenuPanel.this);
gamePanel.revalidate();
gamePanel.repaint();
revalidate();
repaint();
} else { //customPlay
String fileName = fileNameInput.getText();
try { //try constructing grid with file name given
GridGraph gg = new GridGraph(fileName + ".txt");
LevelPanel levelPan = new LevelPanel(gamePanel, gg);
gamePanel.add(levelPan);
gamePanel.remove(MainMenuPanel.this);
gamePanel.revalidate();
gamePanel.repaint();
revalidate();
repaint();
} catch (FileNotFoundException ex) {
setError("The level \"" + fileName + "\" does not exist. " +
"<br> Please try another level name.");
}
}
}
}
}