用Java代码生成粒子爱心效果
时间: 2023-06-03 10:05:53 浏览: 184
很高兴为您解答问题。以下是一个用Java代码生成粒子爱心效果的示例:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ParticleHeart extends JPanel {
private final int WIDTH = 600;
private final int HEIGHT = 600;
private final int NUM_PARTICLES = 2000;
private final int PARTICLE_SIZE = 3;
private ArrayList<Particle> particles;
private Shape heart;
private Random random;
public ParticleHeart() {
particles = new ArrayList<Particle>();
random = new Random();
// Create heart shape
heart = new Ellipse2D.Double(200, 120, 200, 200);
Shape top = new Line2D.Double(300, 120, 400, 20);
Shape left = new Line2D.Double(200, 220, 300, 120);
Shape right = new Line2D.Double(400, 220, 300, 120);
heart = new java.awt.geom.Path2D.Double(heart);
((java.awt.geom.Path2D.Double) heart).append(top, false);
((java.awt.geom.Path2D.Double) heart).append(left, false);
((java.awt.geom.Path2D.Double) heart).append(right, false);
((java.awt.geom.Path2D.Double) heart).closePath();
// Create particles and add to list
for (int i = 0; i < NUM_PARTICLES; i++) {
double angle = Math.random() * 2 * Math.PI;
double x = (heart.getBounds().getCenterX() + Math.cos(angle) * heart.getBounds().getWidth() / 2) + PARTICLE_SIZE / 2;
double y = (heart.getBounds().getCenterY() + Math.sin(angle) * heart.getBounds().getHeight() / 2) + PARTICLE_SIZE / 2;
float vx = (float) (Math.random() * 10 - 5);
float vy = (float) (Math.random() * 10 - 5);
Color color = Color.getHSBColor(random.nextFloat(), 1.0f, 1.0f);
particles.add(new Particle(x, y, vx, vy, PARTICLE_SIZE, color));
}
// Start animation timer
new javax.swing.Timer(10, e -> {
update();
repaint();
}).start();
}
private void update() {
for (Particle particle : particles) {
particle.update();
if (!heart.contains(particle.getX(), particle.getY())) {
double angle = Math.random() * 2 * Math.PI;
particle.setX(heart.getBounds().getCenterX() + Math.cos(angle) * heart.getBounds().getWidth() / 2 + PARTICLE_SIZE / 2);
particle.setY(heart.getBounds().getCenterY() + Math.sin(angle) * heart.getBounds().getHeight() / 2 + PARTICLE_SIZE / 2);
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
// Clear the screen
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, WIDTH, HEIGHT);
// Draw particles
for (Particle particle : particles) {
g2d.setColor(particle.getColor());
g2d.fill(new Ellipse2D.Double(particle.getX(), particle.getY(), PARTICLE_SIZE, PARTICLE_SIZE));
}
g2d.dispose();
}
public static void main(String[] args) {
JFrame frame = new JFrame("Particle Heart");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setContentPane(new ParticleHeart());
frame.setVisible(true);
}
private class Particle {
private double x;
private double y;
private float vx;
private float vy;
private int size;
private Color color;
public Particle(double x, double y, float vx, float vy, int size, Color color) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.size = size;
this.color = color;
}
public void update() {
x += vx;
y += vy;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public float getVx() {
return vx;
}
public void setVx(float vx) {
this.vx = vx;
}
public float getVy() {
return vy;
}
public void setVy(float vy) {
this.vy = vy;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
}
```
该代码使用Java Swing库创建了一个动画,显示了一个粒子爱心效果。在代码中,我们创建了一个具有圆心和三条线段的“心”形状,然后在心形状中放置了一系列颜色随机、随机移动的小颗粒。每小颗粒达到边缘时,我们会将其移回到心形状中,以便呈现出一种流动的效果。
希望这个示例对您有所帮助!