-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPainterController.java
More file actions
346 lines (298 loc) · 10.2 KB
/
PainterController.java
File metadata and controls
346 lines (298 loc) · 10.2 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
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
public class PainterController {
// shapes icons and fill checkbox
@FXML private Line lineIcon;
@FXML private Circle circleIcon;
@FXML private Rectangle rectangleIcon;
@FXML private Polygon triangleIcon;
// fill toggle group
@FXML private ToggleGroup fillToggleGroup;
@FXML private RadioButton emptyRadioBtn;
@FXML private RadioButton fullRadioBtn;
//color picker
@FXML private ColorPicker colorPicker;
// size slider
@FXML private Slider sizeSlider;
@FXML private TextField sizeTextField;
// opacity slider
@FXML private Slider opacitySlider;
@FXML private TextField opacityTextField;
// drawing area pane
@FXML private Pane drawingAreaPane;
// -------------------- manage components --------------------
/**
* Current state of the painter
*/
private enum State{
PENCIL,
ERASER,
LINE,
CIRCLE,
RECTANGLE,
TRIANGLE;
}
private Color brashColor;
private Color fillColor;
private double size;
private State currState;
private double xStart, yStart;
private boolean whileDragging;
/**
* Initializes the PainterController.
*/
public void initialize(){
brashColor = Color.BLACK;
fillColor = Color.TRANSPARENT;
size = sizeSlider.getValue();
currState = State.LINE;
whileDragging = false;
initRadioBtns();
initSizeSlider();
initOpacitySlider();
}
/**
* Initializes the empty anf fill radio buttons.
*/
private void initRadioBtns(){
fullRadioBtn.setUserData(brashColor);
emptyRadioBtn.setUserData(Color.TRANSPARENT);
}
/**
* Initializes the size slider.
*/
private void initSizeSlider(){
sizeTextField.textProperty().bind(sizeSlider.valueProperty().asString("%.0f"));
sizeSlider.valueProperty().addListener(
new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> ov, Number oldVal, Number newVal) {
size = newVal.doubleValue();
}
}
);
}
/**
* Initializes the opacity slider.
*/
private void initOpacitySlider(){
opacityTextField.textProperty().bind(opacitySlider.valueProperty().asString("%.2f"));
opacitySlider.valueProperty().addListener(
new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> ov, Number oldVal, Number newVal) {
brashColor = changeColorOpacity(brashColor, newVal.doubleValue());
fillColor = changeColorOpacity(fillColor, newVal.doubleValue());
}
}
);
}
/**
* Changes the curr state of the painter according to the pressed button
*/
@FXML void pencilBtnPressed(ActionEvent event) { currState = State.PENCIL; }
@FXML void eraserBtnPressed(ActionEvent event) { currState = State.ERASER; }
@FXML void circleBtnPressed(ActionEvent event) { currState = State.CIRCLE; }
@FXML void lineBtnPressed(ActionEvent event) { currState = State.LINE; }
@FXML void rectangleBtnPressed(ActionEvent event) { currState = State.RECTANGLE; }
@FXML void triangleBtnPressed(ActionEvent event) { currState = State.TRIANGLE; }
/**
* Handles the color picked event.
*/
@FXML
void onColorPicked(ActionEvent event) {
brashColor = colorPicker.getValue();
fillColor = fillColor.equals(Color.TRANSPARENT) ? fillColor : brashColor;
setIconsStroke(lineIcon, circleIcon, rectangleIcon, triangleIcon);
setIconsFill(circleIcon, rectangleIcon, triangleIcon);
}
/**
* Sets the fill color of the given shapes to the fillColor
*/
private void setIconsFill(Shape... shapes){
for (Shape shape : shapes){
shape.setFill(fillColor);
}
}
/**
* Sets the stroke color of the given shapes to the fillColor
*/
private void setIconsStroke(Shape... shapes){
for (Shape shape : shapes){
shape.setStroke(brashColor);
}
}
/**
* Handles the fill radio button selected event
* by changing the fill color according to the pressed radio button
*/
@FXML
void fillRadioBtnSelected(ActionEvent event) {
fillColor = fillColor.equals(brashColor) ? Color.TRANSPARENT : brashColor;
setIconsFill(circleIcon, rectangleIcon, triangleIcon);
}
/**
* Handles the undo button press event,
* by removing the last shape that was painted
*/
@FXML
void undoBtnPressed(ActionEvent event) {
removeLast();
}
/**
* Removes the last shape that was painted
*/
private void removeLast(){
int count = drawingAreaPane.getChildren().size();
if (count > 0){
drawingAreaPane.getChildren().remove(count - 1);
}
}
/**
* Handles the clear button press event,
* by clearing all the drawing area
*/
@FXML
void clearBtnPressed(ActionEvent event) {
drawingAreaPane.getChildren().clear();
}
// -------------------- mouse events --------------------
/**
* Handles the mouse pressed event,
* by painting on the board (with pencil or eraser)
*/
@FXML
void onMousePressed(MouseEvent event) {
xStart = event.getX();
yStart = event.getY();
if (currState == State.PENCIL || currState == State.ERASER){
Color color = (currState == State.PENCIL) ? brashColor : Color.WHITE;
paintFreeStyle(xStart, yStart, color);
}
}
/**
* Handles the mouse dragged event,
* by painting on the board if it's pencil/eraser,
* or by showing preview shape.
*/
@FXML
void onMouseDragged(MouseEvent event) {
double x =event.getX();
double y = event.getY();
if (currState == State.PENCIL || currState == State.ERASER){
handlePencilOrEraser(x, y);
}
else {
handleShapePreview(x, y);
}
}
/**
* Handles pencil or eraser action during mouse drag.
*
* @param x The x-coordinate of the mouse.
* @param y The y-coordinate of the mouse.
*/
private void handlePencilOrEraser(double x, double y) {
Color color = (currState == State.PENCIL) ? brashColor : Color.WHITE;
paintFreeStyle(x, y, color);
}
/**
* Handles shape preview during mouse drag.
*
* @param x The x-coordinate of the mouse.
* @param y The y-coordinate of the mouse.
*/
private void handleShapePreview(double x, double y) {
adjustColorOpacityForDragging();
cleanDraggedShape();
paintShape(x, y);
restoreOriginalColorOpacity();
whileDragging = true; // Indicates that the shape will be removed
}
/**
* Adjusts color opacity for dragging shapes.
*/
private void adjustColorOpacityForDragging() {
brashColor = changeColorOpacity(brashColor, brashColor.getOpacity() / 2);
fillColor = (fillColor.equals(Color.TRANSPARENT)) ? fillColor : brashColor;
}
/**
* Restores the original color opacity after dragging shapes.
*/
private void restoreOriginalColorOpacity() {
brashColor = changeColorOpacity(brashColor, brashColor.getOpacity() * 2);
fillColor = (fillColor.equals(Color.TRANSPARENT)) ? fillColor : brashColor;
}
/**
* Paints a freehand style dot at the specified (x,y) coordinates with the given color
*/
private void paintFreeStyle(double x, double y, Color color){
Circle dot = new Circle(x, y, size, color);
drawingAreaPane.getChildren().add(dot);
}
/**
* Handles the mouse released event by painting the dragged shape
*/
@FXML
void onMouseReleased(MouseEvent event) {
if (currState != State.PENCIL && currState != State.ERASER){
cleanDraggedShape();
paintShape(event.getX(), event.getY());
whileDragging = false; //indicate that the shape won't be removed
}
}
/**
* Paints a shape based on the current state and start and end coordinates.
*/
private void paintShape(double xEnd, double yEnd){
Shape shape = null;
switch (currState){
case LINE:
shape = new Line(xStart, yStart, xEnd, yEnd);
break;
case CIRCLE:
double radius = Math.sqrt( Math.pow(xEnd - xStart, 2) + Math.pow(yEnd - yStart, 2))/2;
shape = new Circle((xStart+xEnd)/2, (yStart+yEnd)/2, radius);
break;
case RECTANGLE:
double width = Math.abs(xEnd-xStart);
double height = Math.abs(yEnd-yStart);
shape = new Rectangle(Math.min(xStart, xEnd), Math.min(yStart, yEnd), width, height);
break;
case TRIANGLE:
shape = new Polygon(xStart, yStart, (xStart+xEnd)/2, yEnd, xEnd, yStart);
break;
}
if (shape != null){
shape.setStrokeWidth(size);
shape.setStroke(brashColor);
shape.setFill(fillColor);
}
drawingAreaPane.getChildren().add(shape);
}
/**
* Removes the last dragged shape from the drawing area.
*/
private void cleanDraggedShape(){
if (whileDragging){ //need to remove the last dragged shape
removeLast();
}
}
/**
* Adjusts the opacity of a color.
*
* @param c The color to adjust.
* @param opacity The opacity value.
* @return The adjusted color.
*/
private Color changeColorOpacity(Color c, double opacity){
return new Color(c.getRed(), c.getGreen(), c.getBlue(), opacity);
}
}