爱心烟花代码JAVA
时间: 2024-05-13 07:12:30 浏览: 13
爱心烟花是一种特殊的烟花效果,它能够以爱心的形状在屏幕上绽放出美丽的花火。在JAVA中实现爱心烟花需要使用到图形界面库,比如Swing或JavaFX。
实现爱心烟花的大致思路是:在屏幕上绘制出一个爱心形状的图案,然后在该图案的每个点上依次绘制出烟花效果。具体实现过程可以参考以下步骤:
1. 定义一个类来表示爱心形状,可以通过绘制多个圆弧和线段来实现。
2. 使用Java Swing或JavaFX中的Graphics2D类来绘制爱心形状。
3. 在爱心形状的每个点上依次绘制出烟花效果,可以使用Java中提供的随机数函数来生成不同的烟花效果。
4. 使用Java Timer类来实现动画效果,每隔一定时间重绘一次屏幕,直到所有的烟花效果绘制完毕。
以下是一个简单的实现示例:
```
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.GeneralPath;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class HeartFireworks extends JPanel {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 600;
private static final int HEIGHT = 600;
private static final int POINTS = 1000;
private static final int MAX_RADIUS = 5;
private static final int MAX_SPEED = 4;
private Random random = new Random();
private GeneralPath path = new GeneralPath();
private ArrayList<Point> points = new ArrayList<Point>();
private ArrayList<Firework> fireworks = new ArrayList<Firework>();
public HeartFireworks() {
// 定义爱心形状
path.moveTo(0, 0);
path.curveTo(50, -90, 150, -70, 150, -20);
path.curveTo(150, -70, 250, -90, 300, 0);
path.curveTo(250, -90, 150, -110, 150, -170);
path.curveTo(150, -110, 50, -90, 0, -170);
path.closePath();
// 随机生成1000个点
for (int i = 0; i < POINTS; i++) {
double theta = Math.toRadians(random.nextDouble() * 360);
double radius = random.nextDouble() * MAX_RADIUS;
int x = (int) (radius * Math.cos(theta) + WIDTH / 2);
int y = (int) (radius * Math.sin(theta) + HEIGHT / 2);
points.add(new Point(x, y));
}
// 定时器,每隔10毫秒刷新屏幕
new javax.swing.Timer(10, e -> {
repaint();
}).start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 绘制爱心形状
AffineTransform at = new AffineTransform();
at.translate(WIDTH / 2, HEIGHT / 2);
at.scale(100, 100);
g2d.setColor(Color.RED);
g2d.draw(path.createTransformedShape(at));
// 随机绘制烟花效果
for (Point p : points) {
if (random.nextInt(20) == 0) {
fireworks.add(new Firework(p));
}
}
// 绘制所有烟花效果
for (int i = fireworks.size() - 1; i >= 0; i--) {
Firework fw = fireworks.get(i);
fw.update();
if (!fw.isAlive()) {
fireworks.remove(i);
continue;
}
g2d.setColor(fw.getColor());
g2d.fillOval((int) fw.getX(), (int) fw.getY(), fw.getSize(), fw.getSize());
}
g2d.dispose();
}
private class Firework {
private double x;
private double y;
private double vx;
private double vy;
private Color color;
private int size;
private boolean alive;
public Firework(Point p) {
x = p.x;
y = p.y;
vx = (random.nextDouble() - 0.5) * MAX_SPEED;
vy = (random.nextDouble() - 0.5) * MAX_SPEED;
color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
size = random.nextInt(5) + 5;
alive = true;
}
public void update() {
if (!alive) {
return;
}
x += vx;
y += vy;
vy += 0.1; // 加速度
if (y > HEIGHT + MAX_RADIUS) {
alive = false;
return;
}
if (path.contains(x - WIDTH / 2, y - HEIGHT / 2)) { // 如果落在爱心形状内
for (int i = 0; i < size; i++) {
double theta = Math.toRadians(random.nextDouble() * 360);
double speed = random.nextDouble() * MAX_SPEED * 2 + MAX_SPEED * 2;
fireworks.add(new Firework(x, y, Math.cos(theta) * speed, Math.sin(theta) * speed,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), 1));
}
alive = false;
return;
}
size--;
if (size <= 0) {
alive = false;
return;
}
}
public Firework(double x, double y, double vx, double vy, Color color, int size) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.color = color;
this.size = size;
alive = true;
}
public double getX() {
return x - size / 2.0;
}
public double getY() {
return y - size / 2.0;
}
public Color getColor() {
return color;
}
public int getSize() {
return size;
}
public boolean isAlive() {
return alive;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Heart Fireworks");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null);
frame.setContentPane(new HeartFireworks());
frame.setVisible(true);
});
}
}
```