-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawingPanel.java
More file actions
234 lines (198 loc) · 6.84 KB
/
DrawingPanel.java
File metadata and controls
234 lines (198 loc) · 6.84 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
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.Timer;
class DrawingPanel extends JPanel {
private ArrayList<Point> points = new ArrayList<>();
private ArrayList<Point> currentCurve = new ArrayList<>();
private final int MIN_DISTANCE = 10;
private String statusMessage = "";
private boolean canAddPoints = true;
private boolean isAnimating = false;
private int animationStep = 0;
private final Timer animationTimer;
public DrawingPanel() {
setBackground(Color.BLACK);
setFocusable(true);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1 && canAddPoints){
Point newPoint = new Point(e.getX(), e.getY());
if (!isTooClose(newPoint)) {
points.add(newPoint);
repaint();
}
}
}
});
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e){
switch (e.getKeyCode()) {
case KeyEvent.VK_ENTER:
handleEnterPressed();
break;
case KeyEvent.VK_ESCAPE:
System.exit(0);
case KeyEvent.VK_C:
resetCanvas();
break;
default:
break;
}
}
});
animationTimer = new Timer(1000, (ActionEvent e) -> {
performChaikinStep();
});
}
private void handleEnterPressed() {
if (points.size() <= 0) {
statusMessage = "Please add some control points first!";
repaint();
} else if (points.size() == 1) {
statusMessage = "Need at least 2 points for a curve";
repaint();
} else if (points.size() == 2) {
drawStraightLine();
} else {
startChaikinAnimation();
}
}
private void startChaikinAnimation() {
if (!isAnimating) {
isAnimating = true;
canAddPoints = false;
animationStep = 0;
currentCurve = new ArrayList<>(points);
statusMessage = "";
repaint();
animationTimer.start();
}
}
private void performChaikinStep() {
if (animationStep >= 7) {
animationStep = 0;
currentCurve = new ArrayList<>(points);
statusMessage = "";
} else {
currentCurve = applyChaikinStep(currentCurve);
animationStep++;
statusMessage = "Chaikin's Algorithm - Step " + animationStep;
}
repaint();
}
private ArrayList<Point> applyChaikinStep(ArrayList<Point> inputPoints) {
if (inputPoints.size() < 2) return inputPoints;
ArrayList<Point> newPoints = new ArrayList<>();
newPoints.add(new Point(inputPoints.get(0).x, inputPoints.get(0).y));
for (int i = 0; i < inputPoints.size() - 1; i++) {
Point p1 = inputPoints.get(i);
Point p2 = inputPoints.get(i + 1);
double deltaX = p2.x - p1.x;
double deltaY = p2.y - p1.y;
double qx = p1.x + 0.25 * deltaX;
double qy = p1.y + 0.25 * deltaY;
double rx = p1.x + 0.75 * deltaX;
double ry = p1.y + 0.75 * deltaY;
newPoints.add(new Point((int)Math.round(qx), (int)Math.round(qy)));
newPoints.add(new Point((int)Math.round(rx), (int)Math.round(ry)));
}
newPoints.add(new Point(inputPoints.get(inputPoints.size()-1).x, inputPoints.get(inputPoints.size()-1).y));
return newPoints;
}
private void resetCanvas() {
points.clear();
currentCurve.clear();
isAnimating = false;
canAddPoints = true;
animationStep = 0;
statusMessage = "";
if (animationTimer.isRunning()) {
animationTimer.stop();
}
repaint();
}
private void drawStraightLine() {
statusMessage = "";
canAddPoints = false;
repaint();
}
private boolean isTooClose(Point newPoint) {
for (Point p : points){
double distance = Math.sqrt(
Math.pow(newPoint.x - p.x, 2) +
Math.pow(newPoint.y - p.y, 2)
);
if (distance < MIN_DISTANCE){
return true;
}
}
return false;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setColor(Color.WHITE);
for (Point p : points) {
g2d.drawOval(p.x-3, p.y-3, 6, 6);
}
if (points.size() == 2 && !isAnimating && !canAddPoints) {
g2d.setColor(Color.WHITE);
Point p1 = points.get(0);
Point p2 = points.get(1);
g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
}
if (isAnimating && currentCurve.size() > 1) {
g2d.setColor(Color.WHITE);
for (int i = 0; i < currentCurve.size() - 1; i++) {
Point p1 = currentCurve.get(i);
Point p2 = currentCurve.get(i + 1);
g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
}
}
drawInstructions(g2d);
}
private void drawInstructions(Graphics g) {
g.setFont(new Font("Arial", Font.BOLD, 14));
g.setColor(Color.CYAN);
String[] instructions = {
"Left Click: Add control point",
"Enter: Start animation",
"C: Clear all points",
"Escape: Exit program",
};
int x = 15;
int y = 25;
int lineHeight = 18;
for (String s : instructions) {
g.drawString(s, x, y);
y += lineHeight;
}
if (!statusMessage.isEmpty()) {
g.setColor(Color.RED);
g.setFont(new Font("Arial", Font.BOLD, 16));
g.drawString(statusMessage, 15, getHeight() - 50);
}
}
class Point {
int x,y;
Point(int p1, int p2) {
x = p1;
y = p2;
}
}
}