能否提供一份Java实现的烟花秀源代码示例?
时间: 2024-10-28 10:13:08 浏览: 28
当然可以,以下是一个简单的Java Swing程序,它模拟了烟花秀的效果。这个例子使用了Java的图形用户界面(GUI)API来绘制不同形状和颜色的"烟花"。请注意,这只是一个基础示例,并非完整的烟花秀游戏,但它能展示一些基本的概念:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Path2D;
import java.util.Random;
public class Fireworks extends JFrame {
private static final int CANVAS_WIDTH = 800;
private static final int CANVAS_HEIGHT = 600;
private Random random;
public Fireworks() {
setTitle("简单烟花秀");
setSize(CANVAS_WIDTH, CANVAS_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
random = new Random();
createCanvas();
}
private void createCanvas() {
JPanel canvasPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// 创建随机的烟花效果
for (int i = 0; i < 50; i++) {
double x = random.nextDouble() * CANVAS_WIDTH;
double y = random.nextDouble() * CANVAS_HEIGHT;
double size = random.nextDouble() * 20 + 10;
Color color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
Path2D.Float path = new Path2D.Float(Path2D.WIND_EVEN_ODD);
path.moveTo(x, y);
path.arcTo(x + size, y, size, size, -45, 90);
g2d.setPaint(color);
g2d.fill(path);
}
}
};
add(canvasPanel);
setVisible(true);
}
public static void main(String[] args) {
new Fireworks();
}
}
阅读全文