能写一个Java的烟花代码
时间: 2023-07-16 11:17:36 浏览: 122
当然可以!以下是一个简单的 Java 烟花代码示例:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Fireworks extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
private static final int MAX_PARTICLES = 150;
private static final int MAX_EXPLOSIONS = 15;
private static final int MAX_DETONATIONS = 50;
private ArrayList<Particle> particles = new ArrayList<Particle>();
private ArrayList<Explosion> explosions = new ArrayList<Explosion>();
private ArrayList<Detonation> detonations = new ArrayList<Detonation>();
private Random rand = new Random();
public Fireworks() {
setBackground(Color.BLACK);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Particle p : particles) {
g.setColor(p.getColor());
g.fillOval((int) p.getX(), (int) p.getY(), p.getSize(), p.getSize());
}
for (Explosion e : explosions) {
g.setColor(e.getColor());
g.fillOval((int) e.getX(), (int) e.getY(), e.getSize(), e.getSize());
}
}
public void start() {
Thread t = new Thread(this);
t.start();
}
private void createParticle(double x, double y, double dx, double dy) {
if (particles.size() < MAX_PARTICLES) {
Color color = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
particles.add(new Particle(x, y, dx, dy, color));
}
}
private void createExplosion(double x, double y) {
if (explosions.size() < MAX_EXPLOSIONS) {
Color color = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
explosions.add(new Explosion(x, y, color));
for (int i = 0; i < MAX_DETONATIONS; i++) {
double angle = rand.nextDouble() * Math.PI * 2;
double speed = rand.nextDouble() * 5 + 5;
detonations.add(new Detonation(x, y, Math.cos(angle) * speed, Math.sin(angle) * speed, color));
}
}
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(30);
} catch (InterruptedException e) {}
for (Particle p : particles) {
p.update();
}
for (Explosion e : explosions) {
e.update();
}
for (Detonation d : detonations) {
d.update();
}
particles.removeIf(p -> p.isDead());
explosions.removeIf(e -> e.isDead());
detonations.removeIf(d -> d.isDead());
if (rand.nextDouble() < 0.05) {
createParticle(rand.nextInt(WIDTH), HEIGHT, 0, -rand.nextDouble() * 3 - 2);
}
for (Particle p : particles) {
if (p.getY() < HEIGHT / 4 * 3) {
createExplosion(p.getX(), p.getY());
break;
}
}
repaint();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Fireworks");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null);
Fireworks fireworks = new Fireworks();
frame.add(fireworks);
frame.setVisible(true);
fireworks.start();
}
private class Particle {
private double x;
private double y;
private double dx;
private double dy;
private Color color;
private int size;
public Particle(double x, double y, double dx, double dy, Color color) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this.color = color;
size = rand.nextInt(5) + 3;
}
public void update() {
x += dx;
y += dy;
dy += 0.1;
}
public boolean isDead() {
return y > HEIGHT;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public Color getColor() {
return color;
}
public int getSize() {
return size;
}
}
private class Explosion {
private double x;
private double y;
private Color color;
private int size;
private int age;
public Explosion(double x, double y, Color color) {
this.x = x;
this.y = y;
this.color = color;
size = rand.nextInt(20) + 30;
age = 0;
}
public void update() {
age++;
}
public boolean isDead() {
return age > size / 2;
}
public double getX() {
return x - size / 2;
}
public double getY() {
return y - size / 2;
}
public Color getColor() {
return color;
}
public int getSize() {
return size;
}
}
private class Detonation {
private double x;
private double y;
private double dx;
private double dy;
private Color color;
private int size;
private int age;
public Detonation(double x, double y, double dx, double dy, Color color) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this.color = color;
size = rand.nextInt(5) + 3;
age = 0;
}
public void update() {
x += dx;
y += dy;
age++;
}
public boolean isDead() {
return age > 30;
}
public double getX() {
return x - size / 2;
}
public double getY() {
return y - size / 2;
}
public Color getColor() {
return color;
}
public int getSize() {
return size;
}
}
}
```
这段代码实现了一个简单的烟花效果,可以在 Java 程序中运行。您可以将其复制粘贴到您的 Java IDE 中,并尝试运行它。