-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToddlerPanel
More file actions
377 lines (327 loc) · 7.71 KB
/
ToddlerPanel
File metadata and controls
377 lines (327 loc) · 7.71 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package uwstout.cs144.projects.project3.toddlerGame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
/**
* This class is the panels where all of the games are located.
*
* @author flowersC
* @version 2016-12-13
*
*/
public class ToddlerPanel extends JPanel implements StampList {
/**
* This is the combobox of colors.
*/
private JComboBox<String> colors;
/**
* This is the stamp color.
*/
private Color stampColor;
/**
* This is the shape of the stamp.
*/
private String stampShape;
/**
* This is the location of the mouse on the panel.
*/
private Point mousePoint;
/**
* This is the list of stamps that are drawn on the panel.
*/
private List<Stamp> stamps;
/**
* This is the x coordinate of the mouse.
*/
private int mouseX;
/**
* This is the y coordinate of the mouse.
*/
private int mouseY;
/**
* This is the number of stamps currently on the panel.
*/
private int stampCount = 0;
/**
* This is the options for colors in the ComboBox.
*/
private static final String[] STAMP_COLORS = { "Blue", "Red", "Yellow" };
/**
* This is the Circle radio button.
*/
private JRadioButton circleRadio;
/**
* This is the Square radio button.
*/
private JRadioButton squareRadio;
/**
* This is the Rectangle radio button.
*/
private JRadioButton rectRadio;
/**
* This is the ToddlerGame JFrame.
*/
private static JFrame theGame;
/**
* This is the constructor for the ToddlerPanel.
*
* @param j
* the ToddlerGame JFrame
*/
public ToddlerPanel(JFrame j) {
BorderLayout bord = new BorderLayout();
setLayout(bord);
this.stamps = new ArrayList<Stamp>();
theGame = j;
setPreferredSize(new Dimension(800, 600));
add(createStampStyle(), BorderLayout.EAST);
this.addMouseListener(new GameListener());
}
/**
* This method creates a panel with the selection tools to create a shape.
*
* @return a panel with all of the selection tools
*/
private JPanel createStampStyle() {
JPanel panel = new JPanel();
GridLayout grid = new GridLayout(12, 1);
panel.setLayout(grid);
// create radio buttons
JButton clearButton = new JButton("Clear");
JButton quitButton = new JButton("Quit");
circleRadio = new JRadioButton("Circle", true);
squareRadio = new JRadioButton("Square");
rectRadio = new JRadioButton("Rectangle");
colors = new JComboBox<String>(STAMP_COLORS);
// these two lines correlate.
colors.setSelectedIndex(2);
stampColor = Color.yellow;
stampShape = "Circle";
colors.setMaximumSize(colors.getPreferredSize());
colors.setAlignmentY(RIGHT_ALIGNMENT);
ActionListener colorActionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedIndex = colors.getSelectedIndex();
System.out.println(selectedIndex);
switch (selectedIndex) {
case 0:
stampColor = Color.blue;
break;
case 1:
stampColor = Color.red;
break;
case 2:
stampColor = Color.yellow;
break;
default:
System.out.println("bad things");
break;
}
}
};
colors.addActionListener(colorActionListener);
ActionListener shapeActionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JRadioButton button = (JRadioButton) e.getSource();
if (button == circleRadio) {
stampShape = "Circle";
} else if (button == squareRadio) {
System.out.println(mousePoint);
stampShape = "Square";
} else if (button == rectRadio) {
stampShape = "Rectangle";
}
}
};
circleRadio.addActionListener(shapeActionListener);
squareRadio.addActionListener(shapeActionListener);
rectRadio.addActionListener(shapeActionListener);
// add to panel
panel.add(circleRadio);
panel.add(squareRadio);
panel.add(rectRadio);
panel.add(colors);
panel.add(clearButton);
clearButton.addActionListener(new Clear());
panel.add(quitButton);
quitButton.addActionListener(new Quit());
ButtonGroup group = new ButtonGroup();
// group the JRadioButtons together
group.add(circleRadio);
group.add(squareRadio);
group.add(rectRadio);
group.add(clearButton);
group.add(quitButton);
panel.setAlignmentY(RIGHT_ALIGNMENT);
return panel;
}
/**
* This class is used to handle my clear button actions.
*/
private class Clear implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
removeAllStamps();
// repaint();
}
}
/**
* This class is used to handle my quit button actions.
*
* @author flowersC
* @version 2016-12-13
*
*/
private static class Quit implements ActionListener {
/**
* This method overrides its parents actionPerformed.
*/
@Override
public void actionPerformed(ActionEvent e) {
theGame.dispose();
}
}
/**
* This is the mouse listener that handles where to draw objects.
*
* @author flowersC
* @version 2016-12-13
*
*/
private class GameListener implements MouseListener {
/**
* This method is not used.
*/
@Override
public void mouseClicked(MouseEvent e) {
// not used
}
/**
* This method is not used.
*/
@Override
public void mouseEntered(MouseEvent e) {
// not used
}
/**
* This method is not used.
*/
@Override
public void mouseExited(MouseEvent e) {
// not used
}
/**
* This method is not used.
*/
@Override
public void mousePressed(MouseEvent e) {
// not used
}
/**
* This method creates a stamp at the location of the mouse.
*/
@Override
public void mouseReleased(MouseEvent e) {
System.out.println(mousePoint);
mouseX = e.getX();
mouseY = e.getY();
if (stampShape.equals("Circle")) {
mouseX -= 15;
mouseY -= 15;
mousePoint = new Point(mouseX, mouseY);
} else {
if (stampShape.equals("Square")) {
mouseX -= 10;
mouseY -= 10;
mousePoint = new Point(mouseX, mouseY);
} else {
mouseX -= 15;
mouseY -= 30;
mousePoint = new Point(mouseX, mouseY);
}
}
switch (stampShape) {
case ("Square"):
Stamp stamp = new SquareStamp(mousePoint, stampColor);
addStamp(stamp);
break;
case ("Rectangle"):
Stamp stamp2 = new RectangleStamp(mousePoint, stampColor);
addStamp(stamp2);
break;
case ("Circle"):
Stamp stamp3 = new CircleStamp(mousePoint, stampColor);
addStamp(stamp3);
break;
default:
System.out.println("Bad things occured");
break;
}
repaint();
}
}
/**
* This method draws the stamps to the panel.
*
* @param g
* the graphics
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < stampCount; i++) {
stamps.get(i).draw(g);
}
}
/**
* This method gets the number of stamps on the panel.
*/
@Override
public int getStampCount() {
return stampCount;
}
/**
* This method gets a stamp at a specified location.
*/
@Override
public Stamp getStamp(int pos) {
if (pos < stamps.size() && pos >= 0) {
return stamps.get(pos);
} else {
return null;
}
}
/**
* This method adds a stamp to the list of stamps.
*/
@Override
public void addStamp(Stamp s) {
stamps.add(s);
stampCount++;
}
/**
* This method removes all stamps from the list.
*/
@Override
public void removeAllStamps() {
stamps.clear();
stampCount = 0;
repaint();
}
}