java动态爱心烟花代码
时间: 2023-10-11 18:06:30 浏览: 193
你可以使用Java Swing来实现一个动态的爱心烟花效果。以下是一个简单的Java代码实现:
```java
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class Firework extends JFrame {
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
private static final int FIREWORK_NUM = 10;
private static final int FIREWORK_RADIUS = 5;
private Firework() {
setTitle("Dynamic Love Firework");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
private void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
Random random = new Random();
for (int i = 0; i < FIREWORK_NUM; i++) {
int x = random.nextInt(WIDTH);
int y = random.nextInt(HEIGHT);
drawFirework(g, x, y);
}
}
private void drawFirework(Graphics g, int x, int y) {
Random random = new Random();
Color color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
g.setColor(color);
for (int angle = 0; angle < 360; angle += 15) {
double radians = Math.toRadians(angle);
int endX = (int) (x + FIREWORK_RADIUS * Math.cos(radians));
int endY = (int) (y + FIREWORK_RADIUS * Math.sin(radians));
g.drawLine(x, y, endX, endY);
}
}
public static void main(String[] args) {
Firework firework = new Firework();
firework.run();
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)