用Java语言写一个浪漫烟花
时间: 2023-06-24 15:03:14 浏览: 47
以下是一个示例代码,可以用Java语言编写一个简单的浪漫烟花效果:
```java
import java.awt.*;
import java.util.Random;
import javax.swing.*;
public class RomanticFirework extends JFrame {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
private static final int NUM_FIREWORKS = 50;
private static final int MAX_PARTICLES = 1000;
private Firework[] fireworks = new Firework[NUM_FIREWORKS];
private Particle[][] particles = new Particle[NUM_FIREWORKS][MAX_PARTICLES];
public RomanticFirework() {
super("Romantic Firework");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 初始化烟花和粒子
for (int i = 0; i < NUM_FIREWORKS; i++) {
fireworks[i] = new Firework();
particles[i] = new Particle[MAX_PARTICLES];
}
setVisible(true);
// 开始绘制
while (true) {
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
// 绘制烟花和粒子
for (int i = 0; i < NUM_FIREWORKS; i++) {
if (fireworks[i].isExploded()) {
for (int j = 0; j < MAX_PARTICLES; j++) {
if (particles[i][j] != null) {
particles[i][j].draw(g);
}
}
} else {
fireworks[i].draw(g);
}
}
// 更新烟花和粒子状态
for (int i = 0; i < NUM_FIREWORKS; i++) {
if (fireworks[i].isExploded()) {
for (int j = 0; j < MAX_PARTICLES; j++) {
if (particles[i][j] != null) {
particles[i][j].update();
}
}
} else {
fireworks[i].update();
}
}
}
public static void main(String[] args) {
new RomanticFirework();
}
private class Firework {
private static final int SPEED = 5;
private static final int MAX_EXPLOSION_RADIUS = 50;
private static final int MIN_EXPLOSION_RADIUS = 20;
private static final int MAX_PARTICLES_PER_EXPLOSION = 200;
private static final int MIN_PARTICLES_PER_EXPLOSION = 50;
private boolean exploded = false;
private int x;
private int y;
private int targetX;
private int targetY;
private Color color;
private int explosionRadius;
private Particle[] particles;
private int numParticles;
public Firework() {
x = WIDTH / 2;
y = HEIGHT;
targetX = (int) (Math.random() * WIDTH);
targetY = (int) (Math.random() * HEIGHT / 2);
color = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
}
public void update() {
if (!exploded) {
// 向目标位置移动
int dx = targetX - x;
int dy = targetY - y;
double distance = Math.sqrt(dx * dx + dy * dy);
if (distance <= SPEED) {
exploded = true;
explosionRadius = (int) (MIN_EXPLOSION_RADIUS + Math.random() * (MAX_EXPLOSION_RADIUS - MIN_EXPLOSION_RADIUS));
numParticles = (int) (MIN_PARTICLES_PER_EXPLOSION + Math.random() * (MAX_PARTICLES_PER_EXPLOSION - MIN_PARTICLES_PER_EXPLOSION));
particles = new Particle[numParticles];
for (int i = 0; i < numParticles; i++) {
particles[i] = new Particle(x, y, explosionRadius, color);
}
} else {
x += dx / distance * SPEED;
y += dy / distance * SPEED;
}
}
}
public void draw(Graphics g) {
if (!exploded) {
g.setColor(color);
g.fillOval(x - 5, y - 5, 10, 10);
}
}
public boolean isExploded() {
return exploded;
}
}
private class Particle {
private static final double GRAVITY = 0.1;
private static final double AIR_RESISTANCE = 0.01;
private int x;
private int y;
private double vx;
private double vy;
private int radius;
private Color color;
public Particle(int x, int y, int radius, Color color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
Random random = new Random();
double angle = random.nextDouble() * Math.PI * 2;
double speed = random.nextDouble() * radius / 10;
vx = Math.sin(angle) * speed;
vy = Math.cos(angle) * speed;
}
public void update() {
// 受重力影响
vy += GRAVITY;
// 受空气阻力影响
vx *= 1 - AIR_RESISTANCE;
vy *= 1 - AIR_RESISTANCE;
x += vx;
y += vy;
}
public void draw(Graphics g) {
g.setColor(color);
g.fillOval((int) (x - radius / 2), (int) (y - radius / 2), radius, radius);
}
}
}
```
这个浪漫烟花效果会在窗口中随机产生50个烟花,每个烟花会向随机位置移动,并在到达目标位置后爆炸,产生50到200个粒子。粒子会受重力和空气阻力的影响,向下运动并减速,最终消失。