/home/sjenks/NetBeansProjects/CS2/src/frames/SV2Frame.java |
1
2
3
4 package frames;
5
6 import java.awt.BorderLayout;
7 import java.awt.Color;
8 import java.awt.Container;
9 import java.awt.Dimension;
10 import java.awt.FlowLayout;
11 import java.awt.GridLayout;
12 import java.awt.event.ActionEvent;
13 import java.awt.event.ActionListener;
14 import javax.swing.JButton;
15 import javax.swing.JFrame;
16 import javax.swing.JPanel;
17 import javax.swing.JTextArea;
18 import javax.swing.JTextField;
19 import painter.SPainter;
20 import shapes.SSquare;
21 import utilities.Random;
22
23
24
25
26 @author
27
28 public class SV2Frame extends JFrame implements ActionListener {
29
30
31 JButton blueButton;
32 JButton redButton;
33 JButton yellowButton;
34 JButton colorButton;
35 JButton mfdButton;
36 JButton mbkButton;
37 JButton mrtButton;
38 JButton mltButton;
39 JButton lrtButton;
40 JButton lltButton;
41 JButton lmfButton;
42 JButton srtButton;
43 JButton sltButton;
44 JButton nopButton;
45 JButton clearButton;
46 JButton cleanButton;
47 JButton resetButton;
48 JButton thing1Button;
49 JButton thing2Button;
50 JButton thing3Button;
51 JButton thing4Button;
52 JButton thing5Button;
53 JPanel reflector;
54 JTextArea output;
55 JTextField input;
56 SPainter painter;
57 SSquare square;
58 double side;
59
60 static private Color BLUE = new Color(51,212,255);
61 static private Color RED = new Color (225,51,100);
62 static private Color YELLOW = new Color (225,233,51);
63
64 public SV2Frame(String title) {
65 super(title);
66 setSize(900,700);
67 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
68 addComponents(getContentPane());
69 addListeners();
70 setVisible(true);
71 setResizable(false);
72 }
73
74 private void addComponents(Container contentPane) {
75
76 clearButton = new JButton("Clear");
77 cleanButton = new JButton("Clean");
78 resetButton = new JButton("Reset");
79 JPanel controlPanel = new JPanel();
80 controlPanel.setLayout(new FlowLayout());
81 controlPanel.add(resetButton);
82 controlPanel.add(cleanButton);
83 controlPanel.add(clearButton);
84
85
86 blueButton = new JButton("BLUE");
87 redButton = new JButton("RED");
88 yellowButton = new JButton("YELLOW");
89 colorButton = new JButton("COLOR");
90 mfdButton = new JButton("MFD");
91 mbkButton = new JButton("MBK");
92 mrtButton = new JButton("MRT");
93 mltButton = new JButton("MLT");
94 lrtButton = new JButton("LRT");
95 lltButton = new JButton("LLT");
96 lmfButton = new JButton("LMF");
97 srtButton = new JButton("SRT");
98 sltButton = new JButton("SLT");
99 nopButton = new JButton("NOP");
100 JPanel wControlPanel = new JPanel();
101 wControlPanel.setLayout(new GridLayout(14,1));
102 wControlPanel.add(blueButton);
103 wControlPanel.add(redButton);
104 wControlPanel.add(yellowButton);
105 wControlPanel.add(colorButton);
106 wControlPanel.add(mfdButton);
107 wControlPanel.add(mbkButton);
108 wControlPanel.add(mrtButton);
109 wControlPanel.add(mltButton);
110 wControlPanel.add(lrtButton);
111 wControlPanel.add(lltButton);
112 wControlPanel.add(lmfButton);
113 wControlPanel.add(srtButton);
114 wControlPanel.add(sltButton);
115 wControlPanel.add(nopButton);
116
117
118 thing1Button = new JButton("THING 1");
119 thing2Button = new JButton("THING 2");
120 thing3Button = new JButton("THING 3");
121 thing4Button = new JButton("THING 4");
122 thing5Button = new JButton("THING 5");
123 JPanel eControlPanel = new JPanel();
124 eControlPanel.setLayout(new GridLayout(5,1));
125 eControlPanel.add(thing1Button);
126 eControlPanel.add(thing2Button);
127 eControlPanel.add(thing3Button);
128 eControlPanel.add(thing4Button);
129 eControlPanel.add(thing5Button);
130
131
132 int width = 730; int height = 315;
133 reflector = new JPanel();
134 reflector.setSize(new Dimension(width,height));
135 painter= new SPainter(reflector);
136 side = 24;
137 square = new SSquare(side);
138 output = new JTextArea(width,height);
139 output.setBackground(Color.WHITE);
140 output.setWrapStyleWord(true);
141 output.setLineWrap(true);
142 Container centralRegion = new Container();
143 centralRegion.setLayout(new GridLayout(2,1,6,6));
144 centralRegion.add(painter);
145 centralRegion.add(output);
146
147
148 input = new JTextField();
149
150 contentPane.setLayout(new BorderLayout());
151 contentPane.add(controlPanel, BorderLayout.NORTH);
152 contentPane.add(eControlPanel, BorderLayout.EAST);
153 contentPane.add(wControlPanel, BorderLayout.WEST);
154 contentPane.add(centralRegion, BorderLayout.CENTER);
155 contentPane.add(input, BorderLayout.SOUTH);
156
157 }
158
159
160 private void addListeners() {
161 redButton.addActionListener(this);
162 blueButton.addActionListener(this);
163 yellowButton.addActionListener(this);
164 colorButton.addActionListener(this);
165 mfdButton.addActionListener(this);
166 mbkButton.addActionListener(this);
167 mrtButton.addActionListener(this);
168 mltButton.addActionListener(this);
169 lmfButton.addActionListener(this);
170 srtButton.addActionListener(this);
171 sltButton.addActionListener(this);
172 lrtButton.addActionListener(this);
173 lltButton.addActionListener(this);
174 nopButton.addActionListener(this);
175 cleanButton.addActionListener(this);
176 clearButton.addActionListener(this);
177 resetButton.addActionListener(this);
178 thing1Button.addActionListener(this);
179 thing2Button.addActionListener(this);
180 thing3Button.addActionListener(this);
181 thing4Button.addActionListener(this);
182 thing5Button.addActionListener(this);
183 input.addActionListener(this);
184 }
185
186
187 public void actionPerformed(ActionEvent event) {
188 String command = event.getActionCommand();
189 processCommand(command);
190 if (event.getSource() instanceof JTextField){
191 input.setText("");
192 }
193 }
194 private void processCommand(String command){
195 output.append(command.toUpperCase()+" ");
196 if (command.equalsIgnoreCase("RED")) {
197 red();
198 } else if (command.equalsIgnoreCase("YELLOW")) {
199 yellow();
200 } else if (command.equalsIgnoreCase("BLUE")) {
201 blue();
202 } else if (command.equalsIgnoreCase("COLOR")) {
203 color();
204 } else if (command.equalsIgnoreCase("MFD")) {
205 mfd();
206 } else if (command.equalsIgnoreCase("MBK")) {
207 mbk();
208 } else if (command.equalsIgnoreCase("MRT")) {
209 mrt();
210 } else if (command.equalsIgnoreCase("MLT")) {
211 mlt();
212 } else if (command.equalsIgnoreCase("LRT")) {
213 lrt();
214 } else if (command.equalsIgnoreCase("LLT")) {
215 llt();
216 } else if (command.equalsIgnoreCase("LMF")) {
217 lmf();
218 } else if (command.equalsIgnoreCase("SRT")) {
219 srt();
220 } else if (command.equalsIgnoreCase("SLT")) {
221 slt();
222 } else if (command.equalsIgnoreCase("NOP")) {
223 nop();
224 }else if (command.equalsIgnoreCase("CLEAN")) {
225 clean();
226 } else if (command.equalsIgnoreCase("CLEAR")) {
227 clear();
228 } else if (command.equalsIgnoreCase("RESET")) {
229 reset();
230 } else if (command.equalsIgnoreCase("THING 1")) {
231 thing1();
232 }else if (command.equalsIgnoreCase("THING 2")) {
233 thing2();
234 } else if (command.equalsIgnoreCase("THING 3")) {
235 thing3();
236 } else if (command.equalsIgnoreCase("THING 4")) {
237 thing4();
238 } else if (command.equalsIgnoreCase("THING 5")) {
239 thing5();
240 }else{
241 output.append("### UNRECOGNIZED COMMAND: " + command.toUpperCase()+ "###");
242 }
243 }
244
245 private void red() {
246 painter.setColor(RED);
247 paintSquare();
248 }
249
250 private void yellow() {
251 painter.setColor(YELLOW);
252 paintSquare();
253 }
254
255 private void blue() {
256 painter.setColor(BLUE);
257 paintSquare();
258 }
259
260 private void color() {
261 painter.setColor(Random.color());
262 paintSquare();
263 }
264
265 private void mfd() {
266 painter.mfd(square.side());
267 }
268
269 private void mbk() {
270 painter.mbk(square.side());
271 }
272
273 private void mrt() {
274 painter.mrt(square.side());
275 }
276
277 private void mlt() {
278 painter.mlt(square.side());
279 }
280
281 private void lrt() {
282 painter.mfd(square.side());
283 painter.tr();
284 painter.mfd(square.side());
285 }
286
287 private void llt() {
288 painter.mfd(square.side());
289 painter.tl();
290 painter.mfd(square.side());
291 }
292
293 private void lmf() {
294 painter.mfd((square.side())*2);
295 }
296
297 private void srt() {
298 painter.tr();
299 painter.mfd(square.side());
300 }
301
302 private void slt() {
303 painter.tl();
304 painter.mfd(square.side());
305 }
306
307 private void nop() {
308
309 }
310
311 private void clean() {
312 painter.wash();
313 }
314
315 private void clear() {
316 output.setText("");
317 }
318
319 public void reset() {
320 output.setText("");
321 painter.wash();
322 painter.moveToCenter();
323 }
324
325 private void thing1() {
326
327 painter.mlt(side*2);
328
329
330 painter.setColor(YELLOW);
331 painter.paint(square);
332 painter.setColor(RED);
333 painter.mrt(side);
334 painter.paint(square);
335 painter.mlt(side*2);
336 painter.paint(square);
337
338 painter.mfd(side);
339 painter.setColor(BLUE);
340 painter.paint(square);
341 painter.setColor(YELLOW);
342 painter.tl(side*2);
343 painter.paint(square);
344 painter.tr(side*2);
345 painter.mrt(side);
346 painter.setColor(RED);
347 painter.paint(square);
348 painter.mrt(side);
349 painter.setColor(BLUE);
350 painter.paint(square);
351 painter.tl(side*2);
352 painter.setColor(YELLOW);
353 painter.paint(square);
354 painter.tr(side*2);
355
356 painter.mbk(side*2);
357 painter.setColor(BLUE);
358 painter.paint(square);
359 painter.tl(side*2);
360 painter.setColor(YELLOW);
361 painter.paint(square);
362 painter.tr(side*2);
363 painter.mlt(side);
364 painter.setColor(RED);
365 painter.paint(square);
366 painter.mlt(side);
367 painter.setColor(BLUE);
368 painter.paint(square);
369 painter.setColor(YELLOW);
370 painter.tl(side*2);
371 painter.paint(square);
372 painter.tr(side*2);
373
374
375
376 painter.moveToCenter();
377
378
379 painter.mrt(side*2);
380 painter.setColor(YELLOW);
381 painter.paint(square);
382 painter.setColor(RED);
383 painter.mrt(side);
384 painter.paint(square);
385 painter.mlt(side*2);
386 painter.paint(square);
387
388 painter.mfd(side);
389 painter.setColor(BLUE);
390 painter.paint(square);
391 painter.setColor(YELLOW);
392 painter.tl(side*2);
393 painter.paint(square);
394 painter.tr(side*2);
395 painter.mrt(side);
396 painter.setColor(RED);
397 painter.paint(square);
398 painter.mrt(side);
399 painter.setColor(BLUE);
400 painter.paint(square);
401 painter.tl(side*2);
402 painter.setColor(YELLOW);
403 painter.paint(square);
404 painter.tr(side*2);
405
406 painter.mbk(side*2);
407 painter.setColor(BLUE);
408 painter.paint(square);
409 painter.tl(side*2);
410 painter.setColor(YELLOW);
411 painter.paint(square);
412 painter.tr(side*2);
413 painter.mlt(side);
414 painter.setColor(RED);
415 painter.paint(square);
416 painter.mlt(side);
417 painter.setColor(BLUE);
418 painter.paint(square);
419 painter.setColor(YELLOW);
420 painter.tl(side*2);
421 painter.paint(square);
422 painter.tr(side*2);
423
424
425 painter.moveToCenter();
426
427
428 }
429
430 private void thing2() {
431
432 }
433
434 private void thing3() {
435
436 }
437
438 private void thing4() {
439
440 }
441
442 private void thing5() {
443
444 }
445
446 private void paintSquare() {
447 painter.paint(square);
448 }
449 }
450
451
452
453
454
455
456