使用Java绘制心形动画的代码示例
作者:ZacharyYaaaang
Java动态爱心代码是一种简单而精美的动态效果,这篇文章主要介绍了使用Java绘制心形动画的代码示例,文中通过代码介绍的非常详细,需要的朋友可以参考下
代码说明
动画原理:
使用
Timer定期更新心形的缩放比例(scale变量)缩放比例在0.8到1.2之间循环变化,产生心跳效果
每次缩放变化后调用
repaint()重绘组件
心形绘制:
使用
Path2D类和贝塞尔曲线绘制心形createHeart()方法创建心形路径通过改变
size参数实现缩放效果
视觉效果:
黑色背景衬托红色心形
添加了半透明的红色发光效果增强视觉效果
启用了抗锯齿使图形更平滑
运行方法
将代码保存为
HeartAnimation.java编译运行:
javac HeartAnimation.java && java HeartAnimation
你将看到一个400x400的窗口,其中有一个跳动的红色心形
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Path2D;
import java.util.ArrayList;
import java.util.Random;
public class Main extends JPanel {
private float scale = 1.0f;
private float delta = 0.01f;
private final Color heartColor = new Color(255, 50, 50);
private ArrayList<Particle> particles = new ArrayList<>();
private Random random = new Random();
public Main() {
// 设置定时器来更新动画
Timer timer = new Timer(30, e -> {
scale += delta;
// 改变缩放方向时产生粒子
if (scale > 1.2f) {
delta = -0.01f;
createParticles();
} else if (scale < 0.8f) {
delta = 0.01f;
}
// 更新粒子
updateParticles();
repaint();
});
timer.start();
}
// 创建粒子
private void createParticles() {
int width = getWidth();
int height = getHeight();
int centerX = width / 2;
int centerY = height / 2;
int size = (int) (100 * scale);
// 创建20-30个新粒子
int particleCount = 20 + random.nextInt(10);
for (int i = 0; i < particleCount; i++) {
// 粒子从心形边缘发射
double angle = random.nextDouble() * 2 * Math.PI;
double distance = size * (0.5 + random.nextDouble() * 0.3);
int x = centerX + (int)(distance * Math.cos(angle));
int y = centerY + (int)(distance * Math.sin(angle) * 0.8);
// 随机速度和颜色
float speedX = (random.nextFloat() - 0.5f) * 3f;
float speedY = (random.nextFloat() - 0.8f) * 3f;
Color color = new Color(
255,
50 + random.nextInt(100),
50 + random.nextInt(100),
150 + random.nextInt(100)
);
particles.add(new Particle(x, y, speedX, speedY, color));
}
}
// 更新粒子位置和生命周期
private void updateParticles() {
for (int i = particles.size() - 1; i >= 0; i--) {
Particle p = particles.get(i);
p.update();
// 移除生命周期结束的粒子
if (p.life <= 0) {
particles.remove(i);
}
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// 启用抗锯齿
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int width = getWidth();
int height = getHeight();
// 设置渐变背景
GradientPaint gradient = new GradientPaint(
0, 0, new Color(10, 10, 30),
width, height, new Color(30, 10, 50)
);
g2d.setPaint(gradient);
g2d.fillRect(0, 0, width, height);
// 先绘制粒子(在心形后面)
for (Particle p : particles) {
p.draw(g2d);
}
// 计算心形位置和大小
int centerX = width / 2;
int centerY = height / 2;
int size = (int) (100 * scale);
// 创建心形路径
Path2D heart = createHeart(centerX, centerY, size);
// 绘制心形
g2d.setColor(heartColor);
g2d.fill(heart);
// 添加发光效果
g2d.setColor(new Color(255, 100, 100, 100));
for (int i = 1; i <= 5; i++) {
Path2D glowHeart = createHeart(centerX, centerY, size + i * 3);
g2d.fill(glowHeart);
}
}
private Path2D createHeart(int centerX, int centerY, int size) {
Path2D path = new Path2D.Double();
// 心形贝塞尔曲线
path.moveTo(centerX, centerY - size / 4);
// 左半边心形
path.curveTo(
centerX - size / 2, centerY - size,
centerX - size, centerY,
centerX, centerY + size / 2
);
// 右半边心形
path.curveTo(
centerX + size, centerY,
centerX + size / 2, centerY - size,
centerX, centerY - size / 4
);
return path;
}
// 粒子类
private class Particle {
float x, y;
float speedX, speedY;
Color color;
int life;
float size;
Particle(float x, float y, float speedX, float speedY, Color color) {
this.x = x;
this.y = y;
this.speedX = speedX;
this.speedY = speedY;
this.color = color;
this.life = 30 + random.nextInt(70); // 生命周期30-100帧
this.size = 2 + random.nextFloat() * 3; // 大小2-5像素
}
void update() {
x += speedX;
y += speedY;
speedY += 0.05f; // 重力效果
life--;
size *= 0.98f; // 逐渐缩小
}
void draw(Graphics2D g2d) {
float alpha = (float)life / 100f; // 根据生命周期计算透明度
if (alpha < 0) alpha = 0;
if (alpha > 1) alpha = 1;
g2d.setColor(new Color(
color.getRed(),
color.getGreen(),
color.getBlue(),
(int)(color.getAlpha() * alpha)
));
g2d.fillOval((int)x, (int)y, (int)size, (int)size);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("带粒子效果的心形动画");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
Main animation = new Main();
frame.add(animation);
frame.setVisible(true);
}
}效果如图

总结
到此这篇关于使用Java绘制心形动画的文章就介绍到这了,更多相关Java绘制心形动画内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
