-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditor.java
More file actions
338 lines (304 loc) · 10.6 KB
/
Editor.java
File metadata and controls
338 lines (304 loc) · 10.6 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
/**
* Editor.java
* An applet for an object-oriented graphical editor.
* This class implements the GUI for the editor.
*
* This is a provided file with parts to be filled in.
*
* Written by THC for CS 5 Lab Assignment 3.
*
* @author Thomas H. Cormen
* @author Jasper Bingham
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Editor extends JApplet {
private static final long serialVersionUID = 1L;
private final int APPLET_WIDTH = 700, APPLET_HEIGHT = 500;
private final Color initialColor = Color.red; // default color starts as red
private Command cmd; // the command being executed
private Drawing dwg; // the drawing: shapes in order
private ColorIndicator colorBox; // a GUI component to show the current default color
/**
* Set up the buttons and canvas and register the listeners.
*/
public void init() {
cmd = new Command(); // all methods in Command are empty
dwg = new Drawing(initialColor); // make an empty drawing
// The drawing will appear in a white CanvasPanel.
CanvasPanel canvasPanel = new CanvasPanel();
canvasPanel.setBackground(Color.white);
// Make JButton objects for all the command buttons.
JButton rectButton = new JButton("Rectangle");
JButton ellipseButton = new JButton("Ellipse");
JButton lineButton = new JButton("Line");
JButton moveButton = new JButton("Move");
JButton deleteButton = new JButton("Delete");
JButton frontButton = new JButton("Front");
JButton backButton = new JButton("Back");
JButton exchangeButton = new JButton("Exchange");
JButton redButton = new JButton("Red");
JButton greenButton = new JButton("Green");
JButton blueButton = new JButton("Blue");
// Add listeners for all the command buttons.
rectButton.addActionListener(new RectButtonListener());
ellipseButton.addActionListener(new EllipseButtonListener());
lineButton.addActionListener(new LineButtonListener());
moveButton.addActionListener(new MoveButtonListener());
deleteButton.addActionListener(new DeleteButtonListener());
frontButton.addActionListener(new FrontButtonListener());
backButton.addActionListener(new BackButtonListener());
exchangeButton.addActionListener(new ExchangeButtonListener());
redButton.addActionListener(new RedButtonListener());
greenButton.addActionListener(new GreenButtonListener());
blueButton.addActionListener(new BlueButtonListener());
// The command buttons will be arranged in 3 rows. Each row will
// appear in its own JPanel, and the 3 JPanels will be stacked
// vertically.
JPanel shapePanel = new JPanel(); // holds buttons for adding shapes
JLabel shapeLabel = new JLabel("Add shape:");
shapePanel.setLayout(new FlowLayout());
shapePanel.add(shapeLabel);
rectButton.setBackground(Color.yellow);
ellipseButton.setBackground(Color.yellow);
lineButton.setBackground(Color.yellow);
shapePanel.add(rectButton);
shapePanel.add(ellipseButton);
shapePanel.add(lineButton);
JPanel editPanel = new JPanel(); // holds buttons for editing operations
JLabel editLabel = new JLabel("Edit:");
editPanel.setLayout(new FlowLayout());
editPanel.add(editLabel);
moveButton.setBackground(Color.yellow);
deleteButton.setBackground(Color.yellow);
frontButton.setBackground(Color.yellow);
backButton.setBackground(Color.yellow);
exchangeButton.setBackground(Color.yellow);
editPanel.add(moveButton);
editPanel.add(deleteButton);
editPanel.add(frontButton);
editPanel.add(backButton);
editPanel.add(exchangeButton);
// The color panel is slightly different from the other two. In
// addition to a label and buttons for the color commands, this
// panel holds a ColorIndicator that gives the current default
// color.
JPanel colorPanel = new JPanel();
JLabel colorLabel = new JLabel("Colors:");
colorPanel.setLayout(new FlowLayout());
colorPanel.add(colorLabel);
colorBox = new ColorIndicator();
colorBox.show(initialColor);
redButton.setBackground(Color.yellow);
greenButton.setBackground(Color.yellow);
blueButton.setBackground(Color.yellow);
colorPanel.add(colorBox);
colorPanel.add(redButton);
colorPanel.add(greenButton);
colorPanel.add(blueButton);
// Use a grid layout to stack the button panels vertically. Also,
// give them a cyan background.
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(3, 1));
shapePanel.setBackground(Color.cyan);
editPanel.setBackground(Color.cyan);
colorPanel.setBackground(Color.cyan);
buttonPanel.add(shapePanel);
buttonPanel.add(editPanel);
buttonPanel.add(colorPanel);
// Now we have two panels: buttonPanel and canvasPanel. We want
// buttonPanel to appear above canvasPanel, and canvasPanel should
// grow with the applet.
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(buttonPanel, BorderLayout.NORTH);
cp.add(canvasPanel, BorderLayout.CENTER);
setSize(APPLET_WIDTH, APPLET_HEIGHT);
}
/**
* What to do when rectButton is pressed.
*/
private class RectButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
cmd = new ShapeCmd(); //set to correct Command subclass
//set shape to correct shape
Rect myRect = new Rect(dwg.getColor());
dwg.setShape(myRect);
repaint();
}
}
/**
* What to do when ellipseButton is pressed.
*/
private class EllipseButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
cmd = new ShapeCmd(); //set to correct Command subclass
//set shape to correct shape
Ellipse myEllipse = new Ellipse(dwg.getColor());
dwg.setShape(myEllipse);
repaint();
}
}
/**
* What to do when lineButton is pressed.
*/
private class LineButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
cmd = new ShapeCmd(); //set to correct Command subclass
//set shape to correct shape
Segment mySeg = new Segment(dwg.getColor());
dwg.setShape(mySeg);
repaint();
}
}
/**
* What to do when moveButton is pressed.
*/
private class MoveButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
cmd = new MoveCmd(); //set to correct Command subclass
repaint();
}
}
/**
* What to do when deleteButton is pressed.
*/
private class DeleteButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
cmd = new DeleteCmd(); //set to correct Command subclass
repaint();
}
}
/**
* What to do when frontButton is pressed.
*/
private class FrontButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
cmd = new FrontCmd(); //set to correct Command subclass
repaint();
}
}
/**
* What to do when backButton is pressed.
*/
private class BackButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
cmd = new BackCmd(); //set to correct Command subclass
repaint();
}
}
/**
* What to do when exchangeButton is pressed.
*/
private class ExchangeButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
cmd = new ExchangeCmd(); //set to correct Command subclass
repaint();
}
}
/**
* What to do when redButton is pressed.
*/
private class RedButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
dwg.setColor(Color.red); //change drawing color
colorBox.show(Color.red); //change indicator box color
repaint();
}
}
/**
* What to do when greenButton is pressed.
*/
private class GreenButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
dwg.setColor(Color.green); //change drawing color
colorBox.show(Color.green); //change indicator box color
repaint();
}
}
/**
* What to do when blueButton is pressed.
*/
private class BlueButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
dwg.setColor(Color.blue); //change drawing color
colorBox.show(Color.blue); //change indicator box color
repaint();
}
}
/**
* A ColorIndicator shows what the current color is.
*/
private class ColorIndicator extends JPanel {
private static final long serialVersionUID = 0;
private final int COLORBOX_WIDTH = 20, COLORBOX_HEIGHT = 20;
/**
* Constructor sets the size and border.
*/
public ColorIndicator() {
setBorder(BorderFactory.createEtchedBorder());
setPreferredSize(new Dimension(COLORBOX_WIDTH, COLORBOX_HEIGHT));
}
/**
* Show a new color.
* @param color the color to show
*/
public void show(Color color) {
setBackground(color);
}
}
/**
* CanvasPanel is the class upon which we actually draw. It listens
* for mouse events and calls the appropriate method of the current
* command.
*/
private class CanvasPanel extends JPanel implements MouseListener,
MouseMotionListener {
private static final long serialVersionUID = 0;
/**
* Constructor just needs to set up the CanvasPanel as a listener.
*/
public CanvasPanel() {
addMouseListener(this);
addMouseMotionListener(this);
}
/**
* Paint the whole drawing
* @page the Graphics object to draw on
*/
public void paintComponent(Graphics page) {
super.paintComponent(page); // execute the paint method of JPanel
dwg.draw(page); // have the drawing draw itself
}
/**
* When the mouse is clicked, call the executeClick method of the
* current command.
*/
public void mouseClicked(MouseEvent event) {
cmd.executeClick(event.getPoint(), dwg);
repaint();
}
/**
* When the mouse is pressed, call the executePress method of the
* current command.
*/
public void mousePressed(MouseEvent event) {
cmd.executePress(event.getPoint(), dwg);
repaint();
}
/**
* When the mouse is dragged, call the executeDrag method of the
* current command.
*/
public void mouseDragged(MouseEvent event) {
cmd.executeDrag(event.getPoint(), dwg);
repaint();
}
// We don't care about the other mouse events.
public void mouseReleased(MouseEvent event) { }
public void mouseEntered(MouseEvent event) { }
public void mouseExited(MouseEvent event) { }
public void mouseMoved(MouseEvent event) { }
}
}