帮我JAVA语言代码实现烟花效果
时间: 2023-06-02 21:05:01 浏览: 134
好的,我可以帮您实现烟花效果的JAVA代码。以下是一份简单的JAVA烟花代码示例:
```
import java.applet.*;
import java.awt.*;
import java.util.Random;
public class Firework extends Applet implements Runnable {
Thread animatorThread;
int numberOfParticles = 150;
int particles[];
int accels[] = new int[numberOfParticles];
int velocities[] = new int[numberOfParticles];
int colors[] = new int[numberOfParticles];
Random random;
int height, width;
public void init() {
height = getSize().height;
width = getSize().width;
setBackground(Color.black);
particles = new int[numberOfParticles];
random = new Random();
}
synchronized public void start() {
animatorThread = new Thread(this);
animatorThread.start();
}
synchronized public void stop() {
animatorThread = null;
}
public void run() {
for (;;) {
// Initialize some values
for (int i = 0; i < numberOfParticles; i++) {
particles[i] = height * 10;
accels[i] = random.nextInt(30) + 1;
velocities[i] = random.nextInt(10) + 2;
colors[i] = random.nextInt(255) + 1;
}
// Perform the explosion
for (int j = 0; j < 75; j++) { // Time loop
for (int i = 0; i < numberOfParticles; i++) { // Particle loop
particles[i] -= velocities[i] + accels[i] / 2;
accels[i] -= 1;
if (particles[i] < 0) {
particles[i] = height * 10;
accels[i] = random.nextInt(30) + 1;
velocities[i] = random.nextInt(10) + 2;
colors[i] = random.nextInt(255) + 1;
}
}
// Wait before refreshing
try {
Thread.sleep(35);
} catch (InterruptedException e) {
break;
}
repaint();
}
}
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillRect(0, 0, width, height);
for (int i = 0; i < numberOfParticles; i++) {
g.setColor(new Color(colors[i], colors[i], colors[i]));
g.fillOval(width / 2, height / 2 - particles[i] / 10, particles[i] / 25, particles[i] / 25);
}
}
}
```
您可以将此代码复制并粘贴到JAVA编译器中进行编译运行,或者将其保存为一个.java文件并在命令行中编译运行。这份代码将在屏幕中心创建一些烟花效果。