-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettings.java
254 lines (225 loc) · 7.72 KB
/
Settings.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
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* This class is responsible for the display of the settings screen. Any changed settings are sent to the Renderer class via the Navigation class
*
*/
@SuppressWarnings("serial")
public class Settings extends JPanel {
//class variables
private JButton play;
private JButton menu;
private JButton easy;
private JButton medium;
private JButton hard;
private JButton few;
private JButton many;
private JButton lots;
private Navigation navigator;
/**
* Constructor for settings screen.
*
* @param n Central navigation class
* @throws IOException
*/
public Settings(Navigation n) throws IOException {
// Set navigator to the one passed in
navigator = n;
// Create buttons with icons in them + hover images
ArrayList<JButton> buttonArray = new ArrayList<JButton>();//this is only to save me typing some lines of code later on --tom
play = new JButton(createImage("playP.png"));
play.setRolloverIcon(createImage("playA.png"));
buttonArray.add(play);
menu = new JButton(createImage("menuP.png"));
menu.setRolloverIcon(createImage("menuA.png"));
buttonArray.add(menu);
easy = new JButton(createImage("EasyP.png"));
easy.setRolloverIcon(createImage("EasyA.png"));
buttonArray.add(easy);
medium = new JButton(createImage("MediumP.png"));
medium.setRolloverIcon(createImage("MediumA.png"));
buttonArray.add(medium);
hard = new JButton(createImage("HardP.png"));
hard.setRolloverIcon(createImage("HardA.png"));
buttonArray.add(hard);
few = new JButton(createImage("FewP.png"));
few.setRolloverIcon(createImage("FewA.png"));
buttonArray.add(few);
many = new JButton(createImage("ManyP.png"));
many.setRolloverIcon(createImage("ManyA.png"));
buttonArray.add(many);
lots = new JButton(createImage("LotsP.png"));
lots.setRolloverIcon(createImage("LotsA.png"));
buttonArray.add(lots);
for (JButton button: buttonArray){
button.setRolloverEnabled(true);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setBackground(Color.WHITE);
button.setAlignmentX(CENTER_ALIGNMENT);
}
// Set ActionListeners for settings buttons
play.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
navigator.showGame();
}
});
menu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
navigator.showMenu();
}
});
easy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
navigator.setDiff(60);
}
});
medium.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
navigator.setDiff(30);
}
});
hard.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
navigator.setDiff(15);
}
});
few.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
navigator.setNumSen(5);
}
});
many.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
navigator.setNumSen(15);
}
});
lots.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
navigator.setNumSen(30);
}
});
//make labels for settings
JLabel eztxt = new JLabel("", JLabel.CENTER);
eztxt.setText("Choose how complicated the maze should be");
eztxt.setOpaque(true);
eztxt.setBackground(Color.GREEN);
eztxt.setForeground(Color.BLACK);
eztxt.setAlignmentX(CENTER_ALIGNMENT);
eztxt.setFont(eztxt.getFont().deriveFont((float)24));
JLabel sentxt = new JLabel("", JLabel.CENTER);
sentxt.setText("Choose how many sentries you want in the maze");
sentxt.setOpaque(true);
sentxt.setBackground(Color.GREEN);
sentxt.setForeground(Color.BLACK);
sentxt.setAlignmentX(CENTER_ALIGNMENT);
sentxt.setFont(sentxt.getFont().deriveFont((float)24));
//lay out jpanels for menu buttons
JPanel boxdiff = new JPanel();
boxdiff.setOpaque(false);
boxdiff.setLayout(new BoxLayout(boxdiff, BoxLayout.LINE_AXIS));
boxdiff.setAlignmentX(CENTER_ALIGNMENT);
boxdiff.add(Box.createHorizontalGlue());
boxdiff.add(easy);
boxdiff.add(Box.createRigidArea(new Dimension(220, 0)));
boxdiff.add(medium);
boxdiff.add(Box.createRigidArea(new Dimension(220, 0)));
boxdiff.add(hard);
JPanel boxsens = new JPanel();
boxsens.setOpaque(false);
boxsens.setLayout(new BoxLayout(boxsens, BoxLayout.LINE_AXIS));
boxsens.setAlignmentX(CENTER_ALIGNMENT);
boxsens.add(Box.createHorizontalGlue());
boxsens.add(few);
boxsens.add(Box.createRigidArea(new Dimension(250, 0)));
boxsens.add(many);
boxsens.add(Box.createRigidArea(new Dimension(250, 0)));
boxsens.add(lots);
JPanel boxButtons = new JPanel();
boxButtons.setOpaque(false);
boxButtons.setLayout(new BoxLayout(boxButtons, BoxLayout.LINE_AXIS));
boxButtons.add(Box.createHorizontalGlue());
boxButtons.add(menu);
boxButtons.add(Box.createRigidArea(new Dimension(300, 0)));
boxButtons.add(play);
// Create JPanel for title
JPanel title = new JPanel();
ImageIcon headerIcon = createImage("title.png");
JLabel headerLabel = new JLabel(headerIcon, JLabel.CENTER);
title.add(headerLabel,BorderLayout.CENTER);
title.setOpaque(false);
// Place components in a box layout
Box box = Box.createVerticalBox();
box.add(Box.createVerticalStrut(75));
box.add(title);
box.add(Box.createVerticalStrut(40));
box.add(eztxt);
box.add(boxdiff);
box.add(Box.createVerticalStrut(40));
box.add(sentxt);
box.add(boxsens);
box.add(Box.createVerticalStrut(40));
box.add(boxButtons);
add(box);
setBackground(Color.white);
// Adding box containing components
add(box, JPanel.CENTER_ALIGNMENT);
}
/**
* Helper function to create image to be drawn on JFrame.
*
* @param path Image file name
* @return Image
* @throws IOException
*/
protected static ImageIcon createImage(String path) throws IOException {
java.net.URL imgURL = Menu.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(ImageIO.read(imgURL));
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
/**
* Overridden -- Draws the images onto the JFrame.
*/
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
try {
g.drawImage(createImage("background.png").getImage(), 0, 0, null);
} catch (IOException e) {
e.printStackTrace();
}
}
}