Java语言写一个烟花代码
时间: 2024-06-11 17:05:13 浏览: 9
由于烟花代码比较长,我只能提供一个简单的烟花代码示例,希望能够帮到您:
```java
import java.awt.*;
import java.util.Random;
import javax.swing.*;
public class Firework extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
private static final int NUM_SPARKS = 200;
private static final int MAX_SIZE = 5;
private static final int MAX_SPEED = 10;
private static final int DELAY = 30;
private static final int WIDTH = 600;
private static final int HEIGHT = 400;
private static final Color[] colors = { Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, Color.MAGENTA };
private Spark[] sparks;
private Random random;
public Firework() {
setBackground(Color.BLACK);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setDoubleBuffered(true);
random = new Random();
sparks = new Spark[NUM_SPARKS];
for (int i = 0; i < NUM_SPARKS; i++) {
sparks[i] = new Spark();
}
}
public void run() {
while (true) {
for (int i = 0; i < NUM_SPARKS; i++) {
if (sparks[i].isVisible()) {
sparks[i].move();
} else if (random.nextInt(100) == 0) {
sparks[i].reset();
}
}
repaint();
try {
Thread.sleep(DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < NUM_SPARKS; i++) {
if (sparks[i].isVisible()) {
sparks[i].draw(g);
}
}
}
private class Spark {
private int x;
private int y;
private int size;
private int dx;
private int dy;
private int speed;
private boolean visible;
private Color color;
public Spark() {
reset();
}
public void reset() {
x = random.nextInt(WIDTH);
y = HEIGHT;
size = random.nextInt(MAX_SIZE) + 1;
dx = random.nextInt(MAX_SPEED * 2) - MAX_SPEED;
dy = -(random.nextInt(MAX_SPEED) + 1);
speed = random.nextInt(10) + 1;
visible = true;
color = colors[random.nextInt(colors.length)];
}
public void move() {
x += dx * speed;
y += dy * speed;
if (y < 0) {
visible = false;
}
}
public void draw(Graphics g) {
g.setColor(color);
g.fillOval(x, y, size, size);
}
public boolean isVisible() {
return visible;
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Firework");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Firework firework = new Firework();
frame.add(firework);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Thread thread = new Thread(firework);
thread.start();
}
}
```
在该代码中,我们定义了一个名为 `Firework` 的类,它继承自 `JPanel` 类,并实现了 `Runnable` 接口。在 `Firework` 类中,我们定义了一些常量和变量,例如烟花火花数量、最大大小、最大速度、延迟、宽度和高度等。我们还定义了一个名为 `Spark` 的内部类,它表示烟花火花,并包含一些属性和方法,例如位置、大小、速度、颜色、可见性等。
在 `Firework` 类的构造函数中,我们设置了背景颜色、首选大小和双缓冲,并创建了一组 `Spark` 对象。在 `run` 方法中,我们循环处理每个 `Spark` 对象,如果它可见,则移动它;否则,有一定概率重新设置它的属性。在 `paintComponent` 方法中,我们循环绘制每个可见的 `Spark` 对象。
最后,在 `main` 方法中,我们创建了一个 `Firework` 对象,并将其添加到 `JFrame` 中。然后,我们启动了一个新的线程来运行 `Firework` 对象的 `run` 方法。当该线程运行时,它将循环处理每个 `Spark` 对象,并不断更新屏幕。这样,我们就实现了一个简单的烟花效果。
相关推荐
![](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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)