爱心粒子代码java
时间: 2023-11-27 18:48:25 浏览: 98
根据提供的引用内容,以下是一个基于Java的爱心粒子代码示例:
```java
import processing.core.PApplet;
import processing.core.PVector;
class Particle {
PVector pos;
float zDepth;
Particle(float x, float y, float z) {
pos = new PVector(x, y, z);
zDepth = z;
}
}
public class HeartParticles extends PApplet {
Particle[] particles;
float pulsateMult;
public void settings() {
size(400, 400);
}
public void setup() {
particles = new Particle[1000];
for (int i = 0; i < particles.length; i++) {
float x = random(-width / 2, width / 2);
float y = random(-height / 2, height / 2);
float z = random(width);
particles[i] = new Particle(x, y, z);
}
}
public void draw() {
background(255);
translate(width / 2, height / 2);
for (Particle particle : particles) {
stroke(255, 0, 0);
strokeWeight(2);
point(particle.pos.x, particle.pos.y);
PVector dir = new PVector(-width / 2, -height / 2, 0);
dir.add(particle.pos);
dir.normalize();
dir.mult(pulsateMult * particle.zDepth * 0.05 + cos(frameCount * 0.05) * 1.5);
particle.pos.add(dir);
}
pulsateMult = sin(frameCount * 0.05) * 1.5;
}
public static void main(String[] args) {
PApplet.main("HeartParticles");
}
}
```
阅读全文