java烟花代码
时间: 2023-07-02 13:21:05 浏览: 282
以下是一个简单的 Java 烟花代码示例:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Fireworks extends JPanel {
private static final int FRAME_WIDTH = 600;
private static final int FRAME_HEIGHT = 400;
private static final int MAX_PARTICLES = 150;
private static final int MAX_TRIES = 200;
private static final int PARTICLE_SIZE = 5;
private List<Particle> particles;
private Random rand;
public Fireworks() {
particles = new ArrayList<Particle>();
rand = new Random();
setBackground(Color.BLACK);
}
public void animate() {
while (true) {
createParticles();
repaint();
try {
Thread.sleep(30);
} catch (InterruptedException ex) {}
}
}
private void createParticles() {
int x = rand.nextInt(FRAME_WIDTH);
int y = rand.nextInt(FRAME_HEIGHT);
int tries = 0;
while (isNearOtherParticles(x, y) && tries < MAX_TRIES) {
x = rand.nextInt(FRAME_WIDTH);
y = rand.nextInt(FRAME_HEIGHT);
tries++;
}
if (tries < MAX_TRIES) {
int numParticles = rand.nextInt(MAX_PARTICLES) + 1;
Color color = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
for (int i = 0; i < numParticles; i++) {
particles.add(new Particle(new Point(x, y), color));
}
}
}
private boolean isNearOtherParticles(int x, int y) {
for (Particle p : particles) {
if (p.getPosition().distance(x, y) < PARTICLE_SIZE * 2) {
return true;
}
}
return false;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Particle p : particles) {
g.setColor(p.getColor());
g.fillOval(p.getPosition().x, p.getPosition().y, PARTICLE_SIZE, PARTICLE_SIZE);
p.update();
}
removeDeadParticles();
}
private void removeDeadParticles() {
List<Particle> deadParticles = new ArrayList<Particle>();
for (Particle p : particles) {
if (p.isDead()) {
deadParticles.add(p);
}
}
particles.removeAll(deadParticles);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Fireworks");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
Fireworks fireworks = new Fireworks();
frame.add(fireworks);
frame.setVisible(true);
fireworks.animate();
}
}
```
这个程序使用 Java Swing 绘制烟花效果。它会在一个窗口中随机生成火花,并在屏幕上显示它们的粒子效果。每个火花由多个粒子组成,这些粒子会在屏幕上随机移动,直到它们消失。
相关推荐
![](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)
![](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)