-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreatePanel.java
More file actions
234 lines (212 loc) · 8.61 KB
/
CreatePanel.java
File metadata and controls
234 lines (212 loc) · 8.61 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
/**
* This class displays a panel that allows the user to place start,
* target, and block tiles into the grid to create his/her own level
* and generate a text file to store the level. It displays 1 label,
* 3 radio buttons, and 1 button, and a BlankGridPanel.
* <p>
* Priscilla implemented this class on her own.
*
* @author Priscilla Lee
* @version December 17 2014
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CreatePanel extends JPanel{
//private instance vars
private BlankGridPanel blankPanel;
private GamePanel gamePanel;
private JPanel sidePanel;
private JPanel borderedBlocks, borderedTargets, borderedStart;
private JRadioButton setStart, setTargets, setBlocks;
private JButton generateFile, mainMenu;
private JLabel createLevel, warningLabel;
private int width, height;
/**
* Constructor that takes in a GamePanel and the dimensions of
* the desired grid as input.
*
* @param gp the GamePanel that will contain this panel
* @param w the width of the desired grid
* @param h the height of the desired grid
*/
public CreatePanel(GamePanel gp, int w, int h) {
gamePanel = gp;
width = w;
height = h;
//setting background & size
setPreferredSize(new Dimension(700,500));
setMinimumSize(new Dimension(700,500));
setBackground(ColorScheme.background());
//initialize panels (side and grid)
sidePanel = new JPanel();
blankPanel = new BlankGridPanel(gamePanel, this, width, height);
//adjust layout/design for the different panels
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
sidePanel.setLayout(new GridLayout(7,1));
blankPanel.setBorder(BorderScheme.padded());
//initialize createLevel label, adjust design, add to sidePanel
createLevel = new JLabel("<html><font color='white'>LEVEL CREATOR</font><html>");
createLevel.setFont(new Font("Calibri", Font.BOLD, 20));
createLevel.setOpaque(true);
createLevel.setBackground(ColorScheme.labels());
createLevel.setBorder(BorderScheme.padding());
createLevel.setHorizontalAlignment(JLabel.CENTER);
createLevel.setPreferredSize(new Dimension(200,30));
createLevel.setMinimumSize(new Dimension(150,0));
sidePanel.add(createLevel);
//initialize mainMenu label, adjust design, add to sidePanel
mainMenu = new JButton("<html><font color='white'>MAIN MENU</font></html>");
mainMenu.setFont(new Font("Calibri", Font.BOLD, 20));
mainMenu.setOpaque(true);
mainMenu.setBackground(ColorScheme.main());
mainMenu.addActionListener(new ButtonListener());
mainMenu.setBorder(BorderScheme.padding());
sidePanel.add(mainMenu);
//initialize warning label, adjust design, add to sidePanel
warningLabel = new JLabel();
warningLabel.setFont(new Font("Calibri", Font.BOLD, 20));
warningLabel.setOpaque(true);
warningLabel.setBackground(ColorScheme.background());
warningLabel.setBorder(BorderScheme.padding());
warningLabel.setHorizontalAlignment(JLabel.CENTER);
sidePanel.add(warningLabel);
//intialize setBlocks radio button, adjust design, add to sidePanel
setBlocks = new JRadioButton("<html><font color='white'>SET BLOCKS</font></html>");
setBlocks.setFont(new Font("Calibri", Font.BOLD, 20));
setBlocks.setOpaque(true);
setBlocks.setBackground(ColorScheme.unselected());
borderedBlocks = new JPanel();
borderedBlocks.setLayout(new GridLayout(1,1));
borderedBlocks.add(setBlocks);
borderedBlocks.setBorder(BorderScheme.padding());
borderedBlocks.setOpaque(true);
borderedBlocks.setBackground(ColorScheme.unselected());
sidePanel.add(borderedBlocks);
//intialize setTargets radio button, adjust design, add to sidePanel
setTargets = new JRadioButton("<html><font color='white'>SET TARGETS</font></html>");
setTargets.setFont(new Font("Calibri", Font.BOLD, 20));
setTargets.setOpaque(true);
setTargets.setBackground(ColorScheme.unselected());
borderedTargets = new JPanel();
borderedTargets.setLayout(new GridLayout(1,1));
borderedTargets.add(setTargets);
borderedTargets.setBorder(BorderScheme.padding());
borderedTargets.setOpaque(true);
borderedTargets.setBackground(ColorScheme.unselected());
sidePanel.add(borderedTargets);
//initialize setStart radio button, adjust design, add to sidePanel
setStart = new JRadioButton("<html><font color='white'>SET START</font></html>", true);
setStart.setFont(new Font("Calibri", Font.BOLD, 20));
setStart.setOpaque(true);
setStart.setBackground(ColorScheme.character());
borderedStart = new JPanel();
borderedStart.setLayout(new GridLayout(1,1));
borderedStart.add(setStart);
borderedStart.setBorder(BorderScheme.padding());
borderedStart.setOpaque(true);
borderedStart.setBackground(ColorScheme.unselected());
sidePanel.add(borderedStart);
//group the radio buttons
ButtonGroup group = new ButtonGroup();
group.add(setBlocks);
group.add(setTargets);
group.add(setStart);
setBlocks.addActionListener(new ChoiceListener());
setTargets.addActionListener(new ChoiceListener());
setStart.addActionListener(new ChoiceListener());
//initialize generateFile button, adjust design, add to sidePanel
generateFile = new JButton("<html><font color='white'>GENERATE FILE</font></html>");
generateFile.setFont(new Font("Calibri", Font.BOLD, 20));
generateFile.setOpaque(true);
generateFile.setBackground(ColorScheme.generate());
generateFile.addActionListener(new ButtonListener());
generateFile.setBorder(BorderScheme.padding());
sidePanel.add(generateFile);
//add panels to this LevelPanel
add(sidePanel);
add(blankPanel);
}
/**
* Returns the radio button that was selected.
*
* @return a String that indicates which JRadioButton is currently
* selected ("block", "target", or "start")
*/
public String getSelected() {
if (setBlocks.isSelected())
return "block";
else if (setTargets.isSelected())
return "target";
else //setStart
return "start";
}
/**
* Updates the warning label with the given warning message.
*
* @param msg the warning message to display on the warning label
*/
public void setWarning(String msg) {
warningLabel.setText("<html><font color='white'>"+msg+"</font></html>");
warningLabel.setBackground(ColorScheme.warning());
}
/**
* Resets the warning label to be blank.
*/
public void resetWarning() {
warningLabel.setText("");
warningLabel.setBackground(ColorScheme.background());
}
/**
* 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() == generateFile)
blankPanel.generate();
else { //mainMenu
gamePanel.add(new MainMenuPanel(gamePanel));
gamePanel.remove(CreatePanel.this);
gamePanel.revalidate();
gamePanel.repaint();
revalidate();
repaint();
}
}
}
/**
* Private inner class that indicates the appropriate action when a
* radio button is clicked
*/
private class ChoiceListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
CreatePanel.this.resetWarning();
Object source = event.getSource();
if (source == setStart) {
setStart.setBackground(ColorScheme.character());
borderedStart.setBackground(ColorScheme.character());
setTargets.setBackground(ColorScheme.unselected());
borderedTargets.setBackground(ColorScheme.unselected());
setBlocks.setBackground(ColorScheme.unselected());
borderedBlocks.setBackground(ColorScheme.unselected());
}
else if (source == setTargets) {
setTargets.setBackground(ColorScheme.target());
borderedTargets.setBackground(ColorScheme.target());
setStart.setBackground(ColorScheme.unselected());
borderedStart.setBackground(ColorScheme.unselected());
setBlocks.setBackground(ColorScheme.unselected());
borderedBlocks.setBackground(ColorScheme.unselected());
}
else { //setBlocks
setBlocks.setBackground(ColorScheme.block());
borderedBlocks.setBackground(ColorScheme.block());
setStart.setBackground(ColorScheme.unselected());
borderedStart.setBackground(ColorScheme.unselected());
setTargets.setBackground(ColorScheme.unselected());
borderedTargets.setBackground(ColorScheme.unselected());
}
}
}
}