java代码 动态爱心 叶丹字
时间: 2023-10-02 17:08:41 浏览: 67
以下是使用Java Swing实现的动态爱心和“叶丹”字的代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.Random;
public class DynamicHeart extends JFrame {
private static final long serialVersionUID = 1L;
private static final int FRAME_WIDTH = 500;
private static final int FRAME_HEIGHT = 500;
private static final int HEART_WIDTH = 100;
private static final int HEART_HEIGHT = 100;
private static final int LEAF_SIZE = 20;
private static final int LEAF_COUNT = 10;
private static final int DELAY = 20;
private static final long ANIMATION_TIME = 10000;
private Shape heartShape;
private Shape[] leafShapes;
private Random random;
private long startTime;
public DynamicHeart() {
setTitle("Dynamic Heart");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
heartShape = createHeartShape();
leafShapes = createLeafShapes();
random = new Random();
startTime = System.currentTimeMillis();
setVisible(true);
new Timer(DELAY, e -> repaint()).start();
}
private Shape createHeartShape() {
Path2D.Double heart = new Path2D.Double();
heart.moveTo(0, 0.5 * HEART_HEIGHT);
heart.curveTo(0, 0.2 * HEART_HEIGHT, 0.5 * HEART_WIDTH, 0, HEART_WIDTH, 0.5 * HEART_HEIGHT);
heart.curveTo(HEART_WIDTH, 0.7 * HEART_HEIGHT, 0.5 * HEART_WIDTH, HEART_HEIGHT, 0, 0.5 * HEART_HEIGHT);
heart.closePath();
return heart;
}
private Shape[] createLeafShapes() {
Shape[] leaves = new Shape[LEAF_COUNT];
for (int i = 0; i < LEAF_COUNT; i++) {
Path2D.Double leaf = new Path2D.Double();
double x = random.nextDouble() * FRAME_WIDTH;
double y = random.nextDouble() * FRAME_HEIGHT;
leaf.moveTo(x, y);
leaf.lineTo(x + LEAF_SIZE, y);
leaf.lineTo(x + LEAF_SIZE / 2, y - LEAF_SIZE);
leaf.closePath();
leaves[i] = leaf;
}
return leaves;
}
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
long elapsedTime = System.currentTimeMillis() - startTime;
double progress = (double) elapsedTime / ANIMATION_TIME;
double angle = progress * Math.PI * 2;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.RED);
double x = FRAME_WIDTH / 2 - HEART_WIDTH / 2 + Math.sin(angle) * HEART_WIDTH / 2;
double y = FRAME_HEIGHT / 2 - HEART_HEIGHT / 2 + Math.cos(angle) * HEART_HEIGHT / 2;
AffineTransform transform = AffineTransform.getTranslateInstance(x, y);
transform.rotate(angle, HEART_WIDTH / 2, HEART_HEIGHT / 2);
g2.fill(transform.createTransformedShape(heartShape));
g2.setColor(Color.GREEN);
for (Shape leaf : leafShapes) {
x = leaf.getBounds2D().getX() + Math.sin(angle) * LEAF_SIZE;
y = leaf.getBounds2D().getY() + Math.cos(angle) * LEAF_SIZE;
transform = AffineTransform.getTranslateInstance(x, y);
transform.rotate(angle, leaf.getBounds2D().getX(), leaf.getBounds2D().getY());
g2.fill(transform.createTransformedShape(leaf));
}
g2.setColor(Color.BLACK);
g2.setFont(new Font("Arial", Font.BOLD, 30));
g2.drawString("叶丹", FRAME_WIDTH / 2 - 50, FRAME_HEIGHT / 2 + HEART_HEIGHT / 2 + 50);
}
public static void main(String[] args) {
new DynamicHeart();
}
}
```
该程序会在窗口中绘制一个动态的爱心和叶丹字,并且每隔一定时间会更新位置和角度,实现动态效果。具体实现可以参考代码注释。
阅读全文