-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSPaint.java
More file actions
298 lines (256 loc) · 9.43 KB
/
CSPaint.java
File metadata and controls
298 lines (256 loc) · 9.43 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
// Imports for javafx
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.geometry.Pos;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.geometry.Insets;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Slider;
import javafx.scene.control.Button;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.ArcType;
/**
*@author Natalie Wilkinson
*@version 1.0
*/
public class CSPaint extends Application {
/**
*
* @param args arguments
*/
public static void main(String[] args) {
launch(args);
}
private Color currentColor = Color.BLACK; // keeps track of the current color the user chooses
private double prevX, prevY; // used when drawing to create stroke to the new X and Y values
private boolean dragging; // bool that indicated whether the mouse is currently being dragged
private Canvas canvas; // the canvas where all drawing can be done
private GraphicsContext gc; // used to issue draw calls to the canvas
private VBox left; // creates a VBox
private Button clear;
// In the VBox there are a set of radio buttons and a TextField for choosing color
// setof radio buttons for drawing, erasing, and inserting shapes:
private RadioButton drawBt;
private Slider drawSize;
private Label drawSizeLabel;
private double currentSize = 4;
private RadioButton eraseBt;
/* private Slider eraseSize;
private Label eraseSizeLabel;
private double currentEraseSize = 20; */
private RadioButton circleBt;
// a new textfield for the user to enter a color:
private TextField colorTextBox;
private Alert colorAlert; // new Alert object used to alert user if they entered an invalid color
private BorderPane colorTextLabel;
private RadioButton squareBt;
private Rectangle square;
private RadioButton arcBt;
private HBox bottom; // creates an HBox
// In the HBox there is the graphical coordinates of the mouse and the number of shapes:
// creates a label to keep track of the number of shapes on the GUI
private int shapeCount = 0; // keeps track of the number of shapes added
private Label shapeCountLabel = new Label(Integer.toString(shapeCount));
private Label xyLabel = new Label("(0, 0)");
/**
* @param stage the stage
*/
public void start(Stage stage) {
canvas = new Canvas(650, 450);
gc = canvas.getGraphicsContext2D();
int width = (int) canvas.getWidth();
int height = (int) canvas.getHeight();
gc.setFill(Color.WHITE);
gc.fillRect(0, 0, width, height);
gc.setStroke(Color.WHITE);
gc.setLineWidth(2);
canvas.setOnMousePressed(e -> mousePressed(e));
canvas.setOnMouseDragged(e -> mouseDragged(e));
canvas.setOnMouseReleased(e -> mouseReleased(e));
canvas.setOnMouseMoved(e -> mouseMoved(e));
left = new VBox(20);
left.setPadding(new Insets(5, 5, 5, 5));
clear = new Button("Clear");
clear.setOnMousePressed(e -> clearCanvas(e));
drawBt = new RadioButton("Draw");
// set up penSize slider
drawSize = new Slider(0.1, 30, 4);
drawSizeLabel = new Label("Slide to change pen size:\n" + "(radius = " + (int) currentSize / 2 + " pixels)");
drawSize.setOnMouseReleased(e -> changeDrawSize(e));
drawSize.setOnMouseReleased(e -> changeDrawSize(e));
drawBt.setSelected(true);
eraseBt = new RadioButton("Erase");
circleBt = new RadioButton("Circle");
squareBt = new RadioButton("Square");
arcBt = new RadioButton("Arc");
colorTextLabel = new BorderPane();
colorTextLabel.setLeft(new Label("Color:\n(press Enter when done)"));
colorTextBox = new TextField();
left.setStyle("-fx-background-color: pink");
colorTextBox.setAlignment(Pos.BOTTOM_RIGHT);
colorTextLabel.setCenter(colorTextBox);
left.getChildren().addAll(clear, drawBt, drawSizeLabel, drawSize, eraseBt,
circleBt, squareBt, arcBt, colorTextLabel, colorTextBox);
ToggleGroup group = new ToggleGroup();
drawBt.setToggleGroup(group);
eraseBt.setToggleGroup(group);
circleBt.setToggleGroup(group);
squareBt.setToggleGroup(group);
arcBt.setToggleGroup(group);
colorTextBox.setOnKeyPressed(e -> keyPressed(e));
BorderPane pane = new BorderPane();
pane.setLeft(left);
Pane mainPane = new Pane(canvas);
bottom = new HBox(10);
bottom.setPadding(new Insets(5, 5, 5, 5));
bottom.getChildren().add(xyLabel);
Text text = new Text(50, 50, "Number of Shapes: ");
bottom.getChildren().add(text);
bottom.getChildren().add(shapeCountLabel);
pane.setBottom(bottom);
bottom.setStyle("-fx-background-color: pink");
Pane root = new Pane(canvas);
pane.setCenter(root);
Scene scene = new Scene(pane);
stage.setScene(scene);
stage.setResizable(false);
stage.setTitle("CSPaint");
stage.show();
}
/**
* Updates the number of shapes counted
*/
public void updateShapeCount() {
shapeCount++;
shapeCountLabel.setText(Integer.toString(shapeCount));
}
/**
* changes pen size
* @param event changing draw slider size
*/
public void changeDrawSize(MouseEvent event) {
currentSize = drawSize.getValue();
drawSizeLabel.setText("Slide to change pen size:\n" + "(radius = " + (int) currentSize / 2 + " pixels)");
gc.setLineWidth(currentSize);
//System.out.println(currentSize);
}
/**
* clears canvas
* @param event pressing "clear" button
*/
public void clearCanvas(MouseEvent event) {
gc.setFill(Color.WHITE);
shapeCount = 0;
shapeCountLabel.setText(Integer.toString(shapeCount));
gc.fillRect(0, 0, 650, 450);
//System.out.println(currentSize);
}
/**
*
* @param event event of key getting pressed
*/
public void keyPressed(KeyEvent event) {
if (event.getCode() == KeyCode.ENTER) {
changeColor(colorTextBox.getText());
}
}
private void changeColor(String textFieldColor) {
if (textFieldColor != null) {
try {
currentColor = Color.valueOf(textFieldColor);
} catch (IllegalArgumentException e) {
colorAlert = new Alert(AlertType.WARNING);
colorAlert.setTitle("Invalid Color Warning");
colorAlert.setHeaderText("You've chosen an invalid color!");
colorAlert.setContentText("Type in another color.");
colorAlert.showAndWait();
}
} else {
currentColor = Color.BLACK;
}
}
/**
* @param event mouse getting moved
*/
public void mouseMoved(MouseEvent event) {
xyLabel.setText("(" + event.getX() + ", " + event.getY() + ")");
}
/**
* @param event mouse getting pressed
*/
public void mousePressed(MouseEvent event) {
if (drawBt.isSelected()) {
if (dragging) {
return;
}
int x = (int) event.getX();
int y = (int) event.getY();
prevX = x;
prevY = y;
dragging = true;
gc.setLineWidth(currentSize);
gc.setStroke(currentColor);
gc.strokeLine(x, y, x, y);
} else if (eraseBt.isSelected()) {
int x = (int) event.getX();
int y = (int) event.getY();
prevX = x;
prevY = y;
dragging = true;
gc.setLineWidth(20);
gc.setStroke(Color.WHITE);
gc.strokeLine(x, y, x, y);
} else if (circleBt.isSelected()) {
gc.setFill(currentColor);
gc.fillOval((int) event.getX(), (int) event.getY(), 30, 30);
updateShapeCount();
} else if (squareBt.isSelected()) {
gc.setFill(currentColor);
gc.fillRect(event.getX(), event.getY(), 30.0, 30.0);
updateShapeCount();
} else if (arcBt.isSelected()) {
gc.setFill(currentColor);
gc.fillArc(event.getX(), event.getY(), 30, 30, 45, 240, ArcType.ROUND);
updateShapeCount();
}
}
/**
* @param event mouse getting released
*/
public void mouseReleased(MouseEvent event) {
dragging = false;
}
/**
*
* @param event mouse getting dragged
*/
public void mouseDragged(MouseEvent event) {
double x = event.getX();
double y = event.getY();
if (x >= 0 && x <= 650 && y >= 0 && y <= 450) {
xyLabel.setText("(" + event.getX() + ", " + event.getY() + ")");
}
if (!dragging) {
return;
}
gc.strokeLine(prevX, prevY, x, y);
prevX = x;
prevY = y;
}
}