-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA8.java
More file actions
360 lines (301 loc) · 12.6 KB
/
A8.java
File metadata and controls
360 lines (301 loc) · 12.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
// import javafx.scene.text.Font;
import javafx.scene.control.*;
import javafx.scene.paint.Color;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.util.ArrayList;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.MouseButton;
// fix slection bugs
/**
* ???
*
* @author YOUR NAME
*/
public class A8 extends Application {
// TODO: Instance Variables for View Components and Model
private final static int MAXROWS = 600;
private final static int MAXCOLS = 1000;
private Shape shape;
private Label message;
private ArrayList<Shape> shapes;
private GraphicsContext gc;
private Label widthLabel; // New instance variable for width label
private TextField widthTextField; // New instance variable for width
private Label heightLabel; // New instance variable for height label
private TextField heightTextField; // New instance variable for height
private Button ellipseButton;
private Button circleButton;
private Button rectangleButton;
private Button squareButton;
private Button clearButton;
private Button undoButton;
public String selectedShape = "Ellipse";
private Label outlineColorLabel;
private ColorPicker outlineColorPicker;
private Label fillColorLabel;
private ColorPicker fillColorPicker;
private Label outlineWidthLabel;
private TextField outlineWidthField;
private Button deleteButton;
private Button toFrontButton;
private Button toBackButton;
private Button resetColorButton;
private Shape rightSelectedShape = null;
private Label instructionLabel;
// TODO: Private Event Handlers and Helper Methods
public void pressHandler(MouseEvent me) {
try {
if (me.getButton() == MouseButton.PRIMARY) {
if (me.getY() < MAXROWS - 200) {
double width = Double.parseDouble(widthTextField.getText());
double height = Double.parseDouble(heightTextField.getText());
Color outlineColor = outlineColorPicker.getValue();
Color fillColor = fillColorPicker.getValue();
double outlineWidth = Double.parseDouble(outlineWidthField.getText());
switch (selectedShape) {
case "Ellipse":
shape = new Ellipse(width, height, outlineColor, fillColor, outlineWidth, me.getX(), me.getY());
break;
case "Circle":
shape = new Circle(width, outlineColor, fillColor, outlineWidth, me.getX(), me.getY());
break;
case "Rectangle":
shape = new Rectangle(width, height, outlineColor, fillColor, outlineWidth, me.getX(), me.getY());
break;
case "Square":
shape = new Square(width, outlineColor, fillColor, outlineWidth, me.getX(), me.getY());
break;
default:
return;
}
shapes.add(shape);
updateFrame();
}
} else if (me.getButton() == MouseButton.SECONDARY) {
if (me.getY() < MAXROWS - 200) {
for (Shape s : shapes) {
if (s.contains(me.getX(), me.getY())) {
if (rightSelectedShape != null)
rightSelectedShape.rightSelectionToggleOff();
rightSelectedShape = s;
rightSelectedShape.rightSelectionToggleOn();
updateFrame();
break;
}
}
}
}
} catch (NumberFormatException ex) {
// Handle the exception by displaying an error message
new Alert(Alert.AlertType.WARNING, "Invalid Line Width").showAndWait();
}
}
private void ellipseButtonHandler(ActionEvent event){
selectedShape = "Ellipse";
}
private void circleButtonHandler(ActionEvent event) {
selectedShape = "Circle";
}
private void rectangleButtonHandler(ActionEvent event) {
selectedShape = "Rectangle";
}
private void squareButtonHandler(ActionEvent event) {
selectedShape = "Square";
}
private void clearButtonHandler(ActionEvent event) {
shapes.clear();
updateFrame();
}
private void undoButtonHandler(ActionEvent event) {
if (!shapes.isEmpty()) {
shapes.remove(shapes.size() - 1);
updateFrame();
}
}
private void deleteButtonHandler(ActionEvent event) {
if (rightSelectedShape!= null) {
shapes.remove(rightSelectedShape);
rightSelectedShape.rightSelectionToggleOff();
rightSelectedShape = null;
updateFrame();
}
}
private void toFrontButtonHandler(ActionEvent event) {
if (rightSelectedShape != null) {
shapes.remove(rightSelectedShape);
shapes.add(rightSelectedShape);
rightSelectedShape.rightSelectionToggleOff();
rightSelectedShape = null;
updateFrame();
}
}
private void toBackButtonHandler(ActionEvent event) {
if (rightSelectedShape != null) {
shapes.remove(rightSelectedShape);
shapes.add(0,rightSelectedShape);
rightSelectedShape.rightSelectionToggleOff();
rightSelectedShape = null;
updateFrame();
}
}
private void resetColorHandler(ActionEvent event){
if (rightSelectedShape != null) {
Color outlineColor = outlineColorPicker.getValue();
Color fillColor = fillColorPicker.getValue();
rightSelectedShape.setFillColor(fillColor);
rightSelectedShape.setOutlineColor(outlineColor);
rightSelectedShape.rightSelectionToggleOff();
rightSelectedShape = null;
updateFrame();
}
}
public void updateFrame() {
if (rightSelectedShape != null) {
deleteButton.setDisable(false);
toFrontButton.setDisable(false);
toBackButton.setDisable(false);
resetColorButton.setDisable(false);
} else {
deleteButton.setDisable(true);
toFrontButton.setDisable(true);
toBackButton.setDisable(true);
resetColorButton.setDisable(true);
}
// add white layer
gc.setFill(Color.WHITE);
gc.fillRect(0,0,MAXCOLS, MAXROWS-200);
// add shapes layer
for( Shape s : shapes)
s.draw(gc);
// add grey layer
gc.setFill(Color.LIGHTGRAY);
gc.fillRect(0,MAXROWS - 200, MAXCOLS,200);
message.setText("Number of shapes: "+ shapes.size()) ;
}
/**
* This is where you create your components and the model and add event
* handlers.
*
* @param stage The main stage
* @throws Exception
*/
@Override
public void start(Stage stage) throws Exception {
Pane root = new Pane();
Scene scene = new Scene(root, MAXCOLS, MAXROWS); // set the size here
stage.setTitle("A8-Paint"); // set the window title here
stage.setScene(scene);
// 1. Create the model
shapes = new ArrayList<Shape>();
// 2. Create the GUI components
Canvas c = new Canvas(MAXCOLS, MAXROWS);
message = new Label("Number of shapes: 0");
ellipseButton = new Button("Ellipse");
circleButton = new Button("Circle");
rectangleButton = new Button("Rectangle");
squareButton = new Button("Square");
clearButton = new Button("Clear");
undoButton = new Button("Undo");
outlineColorLabel = new Label("Outline Color:");
outlineColorPicker = new ColorPicker(Color.BLACK); // Initial value for black color
fillColorLabel = new Label("Fill Color:");
fillColorPicker = new ColorPicker(Color.WHITE); // Initial value for white color
outlineWidthLabel = new Label("Outline Width:");
outlineWidthField = new TextField("2"); // Initial value for outline width
widthLabel = new Label("Width(Ellipse/Rectangle)\n Radius(circle)\n Square(side):");
widthTextField = new TextField("60.0"); // Initial value for width
heightLabel = new Label("Height");
heightTextField = new TextField("40.0"); // Initial value for height
deleteButton = new Button("Delete");
toFrontButton = new Button("To Front");
toBackButton = new Button("To Back");
resetColorButton = new Button("Reset Color");
instructionLabel = new Label("INSTRUCTIONS\n"+"Left-click to draw shapes. Right-click to select.\n"
+ "Use Delete to remove selected shape.\n"
+ "Use To Front and To Back to change layer order.");
deleteButton.setDisable(true);
toFrontButton.setDisable(true);
toBackButton.setDisable(true);
resetColorButton.setDisable(true);
// 3. Add components to the root
root.getChildren().addAll(
c,
message,
ellipseButton,
circleButton,
rectangleButton,
squareButton,
clearButton,
undoButton,
widthLabel,
widthTextField,
heightLabel,
heightTextField,
outlineColorLabel,
fillColorLabel,
outlineColorPicker,
fillColorPicker,
outlineWidthLabel,
outlineWidthField,
deleteButton,
toFrontButton,
toBackButton,
resetColorButton,
instructionLabel
);
ellipseButton.setOnAction(this::ellipseButtonHandler);
circleButton.setOnAction(this::circleButtonHandler);
rectangleButton.setOnAction(this::rectangleButtonHandler);
squareButton.setOnAction(this::squareButtonHandler);
clearButton.setOnAction(this::clearButtonHandler);
undoButton.setOnAction(this::undoButtonHandler);
deleteButton.setOnAction(this::deleteButtonHandler);
toFrontButton.setOnAction(this::toFrontButtonHandler);
toBackButton.setOnAction(this::toBackButtonHandler);
resetColorButton.setOnAction(this::resetColorHandler);
// 4. Configure the components (colors, fonts, size, location)
gc = c.getGraphicsContext2D();
message.relocate(20, 20); // debug line --- take out soon
ellipseButton.relocate(20, MAXROWS - 180);
circleButton.relocate(20, MAXROWS - 150);
rectangleButton.relocate(20, MAXROWS - 120);
squareButton.relocate(20, MAXROWS - 90);
clearButton.relocate(20, MAXROWS - 40);
undoButton.relocate(80, MAXROWS - 40);
widthLabel.relocate(350, MAXROWS - 180); // Adjust the y-coordinate as needed
widthTextField.relocate(350, MAXROWS - 120); // Adjust the y-coordinate as needed
heightLabel.relocate(350, MAXROWS - 90); // Adjust the y-coordinate as needed
heightTextField.relocate(350, MAXROWS - 60); // Adjust the y-coordinate as needed
outlineColorLabel.relocate(150, MAXROWS - 180);
outlineColorPicker.relocate(150, MAXROWS - 160);
outlineWidthLabel.relocate(150, MAXROWS - 120); // Adjust the y-coordinate as needed
outlineWidthField.relocate(150, MAXROWS - 100); // Adjust the y-coordinate as needed
fillColorLabel.relocate(150, MAXROWS - 60);
fillColorPicker.relocate(150, MAXROWS - 40);
deleteButton.relocate(550, MAXROWS - 180);
toFrontButton.relocate(550, MAXROWS - 140);
toBackButton.relocate(550, MAXROWS - 100);
resetColorButton.relocate(550, MAXROWS - 60);
instructionLabel.relocate(MAXCOLS -350, MAXROWS - 180);
updateFrame();
message.relocate(20,20); //debug line --- take out soon
// 5. Add Event Handlers and do final setup
c.addEventHandler(MouseEvent.MOUSE_PRESSED,this::pressHandler);
// 6. Show the stage
stage.show();
}
/**
* Make no changes here.
*
* @param args unused
*/
public static void main(String[] args) {
launch(args);
}
}