forked from Larryzpl123/Java-Flower-Animation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowerAnimation.java
More file actions
431 lines (213 loc) · 8.69 KB
/
Copy pathFlowerAnimation.java
File metadata and controls
431 lines (213 loc) · 8.69 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import java.awt.*; //java draw
import java.util.*; //java system
import java.util.concurrent.CompletableFuture; //time pause function
import java.util.concurrent.ExecutionException; //time pause
public class FlowerAnimation { //1 - main class function (1/3)
public static void main(String[] args) { //void main
Frame frame = new Frame("Flower Animation"); //fraim name
DrawingCanvas canvas = new DrawingCanvas(); //canvas
frame.setSize(800, 800); //size 800*800
frame.add(canvas); //add canvas to fram
frame.setVisible(true); //visible
frame.addWindowListener(new java.awt.event.WindowAdapter() { //window
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0); //exit window
}
});
// Animation thread
new Thread(() -> {
while (true) {
canvas.updatePetals(); //keep updating
canvas.repaint(); //repaint for animation effect
try { //wait function smoother here, old code turn to comments
Thread.sleep(50); //slepp
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}).start(); //start
}
}
class DrawingCanvas extends Canvas { //2 - drawing class canvas (2/3)
private java.util.List<Petal> petals = Collections.synchronizedList(new ArrayList<>());
@Override
public void paint(Graphics g) {
// Draw background
g.setColor(new Color(135, 206, 235)); // Sky blue
g.fillRect(0, 0, 800, 500);
// Set a transparent green color for the mountains
Color mountainColor = new Color(0, 255, 127, 150); // 青绿 with alpha for transparency
g.setColor(mountainColor);
// Create the mountain shape
int[] xPoints = {0, 100, 200, 350, 500, 550, 600, 800, 800, 0};
int[] yPoints = {100, 400, 150, 400, 450, 350, 200, 0, 500, 800};
g.fillPolygon(xPoints, yPoints, xPoints.length);
g.setColor(new Color(34, 139, 34)); // Forest green
g.fillRect(0, 500, 800, 400);
int sunX = 700; // X position for the sun
int sunY = 100; // Y position for the sun
int radius = 32; // Radius for the sun polygon
int spikes = 16; // Number of spikes
// Draw the spikes
g.setColor(Color.YELLOW);
for (int i = 0; i < spikes; i++) {
double angle = 2 * Math.PI / spikes * i;
int x1 = (int) (sunX + Math.cos(angle) * (radius + 20)); // Outer point
int y1 = (int) (sunY + Math.sin(angle) * (radius + 20));
int x2 = (int) (sunX + Math.cos(angle) * radius); // Inner point
int y2 = (int) (sunY + Math.sin(angle) * radius);
g.drawLine(x1, y1, x2, y2);
}
// Draw the center of the sun (32-sided polygon)
g.setColor(Color.RED);
int[] xxxPoints = new int[32];
int[] yyyPoints = new int[32];
for (int i = 0; i < 32; i++) {
double angle = 2 * Math.PI / 32 * i;
xxxPoints[i] = (int) (sunX + Math.cos(angle) * radius);
yyyPoints[i] = (int) (sunY + Math.sin(angle) * radius);
}
g.fillPolygon(xxxPoints, yyyPoints, xxxPoints.length);
// Draw static elements
drawTreeHouse(g);
drawFlowerTree(g);Flower
// Draw petals
g.setColor(new Color(255, 71, 76)); // Flower red
synchronized (petals) {
for (Petal p : petals) {
g.fillOval((int) p.x, (int) p.y, 8, 8);
}
}
}
private void drawTreeHouse(Graphics g) {
// Main structure
g.setColor(new Color(139, 69, 19)); // Brown
g.fillRect(300, 200, 200, 200);
g.setColor(new Color(1, 1, 1)); //black
g.drawRect(300, 200, 200, 200);
// Roof
int[] xPoints = {250, 550, 400};
int[] yPoints = {200, 200, 120};
g.setColor(new Color(178, 34, 34)); // Firebrick
g.fillPolygon(xPoints, yPoints, 3);
g.setColor(new Color(1, 1, 1)); //outline
g.drawPolygon(xPoints, yPoints, 3);
// Windows
g.setColor(new Color(135, 206, 250)); // Light sky blue
g.fillRect(320, 250, 50, 50);
g.fillRect(430, 250, 50, 50);
g.setColor(new Color(1, 1, 1)); // black
g.drawRect(320, 250, 50, 50);
g.drawRect(430, 250, 50, 50);
// door
g.setColor(new Color(1, 1, 1));
g.drawRect(374, 324, 51, 76);
g.setColor(new Color(130, 94, 92));
g.fillRect(375, 325, 50, 75);
// door handle
g.setColor(new Color(255, 255, 255));
g.drawOval(409, 374, 12, 12);
g.setColor(new Color(70, 44, 42));
g.fillOval(410, 375, 10, 10);
// windows lines
g.drawLine(345, 250, 345, 300);
g.drawLine(320, 275, 370, 275);
g.drawLine(430, 275, 480, 275);
g.drawLine(455, 250, 455, 300);
}
private void drawFlowerTree(Graphics g) {
// Trunk
g.setColor(new Color(101, 67, 33)); // Wood brown
g.fillRect(380, 400, 40, 300);
// Branches
g.drawLine(400, 450, 300, 500);
g.drawLine(400, 450, 500, 500);
g.drawLine(400, 500, 350, 550);
g.drawLine(400, 500, 450, 550);
// Blossoms
g.setColor(new Color(255, 71, 76)); // Flower red
drawBlossomCluster(g, 280, 490);
drawBlossomCluster(g, 480, 490);
drawBlossomCluster(g, 340, 540);
drawBlossomCluster(g, 440, 540);
int max1 = 550; //550
int min1 = 250; //250
int max2 = 550; //550
int min2 = 400; //420
int range1 = max1 - min1 + 1;
int range2 = max2 - min2 + 1;
// generate random numbers within 1 to 100
for (int i = 0; i < 200; i++) {
int rand = (int)(Math.random() * range1) + min1;
int rand2 = (int)(Math.random() * range2) + min2;
drawBlossomCluster(g, rand, rand2);
}
}
private void drawBlossomCluster(Graphics g, int x, int y) {
for (int i = 0; i < 5; i++) {
int offsetX = (int) (Math.random() * 20 - 10);
int offsetY = (int) (Math.random() * 20 - 10);
g.fillOval(x + offsetX, y + offsetY, 12, 12);
}
}
public void updatePetals() {
synchronized (petals) {
// Remove petals that fall out of screen
petals.removeIf(p -> p.y > 800);
// Update existing petals
for (Petal p : petals) {
//try {
//Thread.sleep(5); // Pause
//} catch (InterruptedException e) {
// Handle the exception if the sleep is interrupted
//e.printStackTrace();
//}
p.update();
}
// Add new petals
if (Math.random() < 0.2) {
int min3 = 222;
int max3 = 555;
int range3 = max3 - min3 + 1;
int rand3 = (int)(Math.random() * range3) + min3;
petals.add(new Petal(rand3, 400 + Math.random() * 200));
}
}
}
// Double buffering to prevent flickering
private Image offScreenBuffer;
private Graphics offScreenGraphics;
@Override
public void update(Graphics g) {
if (offScreenBuffer == null) {
offScreenBuffer = createImage(getWidth(), getHeight());
offScreenGraphics = offScreenBuffer.getGraphics();
}
// Clear offscreen buffer
offScreenGraphics.setColor(getBackground());
offScreenGraphics.fillRect(0, 0, getWidth(), getHeight());
// Draw to offscreen buffer
paint(offScreenGraphics);
// Draw offscreen buffer to screen
g.drawImage(offScreenBuffer, 0, 0, this);
}
}
//3 - Class Petal (3/3)
class Petal {
double x, y;
double angle;
double speedY;
double speedX;
public Petal(double x, double y) {
this.x = x;
this.y = y;
this.angle = Math.random() * 2 * Math.PI;
this.speedY = 2 + Math.random() * 3;
this.speedX = 1.5;
}
public void update() {
y += speedY;
x += Math.cos(angle) * speedX;
angle += 0.1;
}
}