用java完成一个动态爱心
时间: 2024-05-17 15:13:41 浏览: 16
以下是JavaFX实现的动态爱心的示例代码:
```java
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.CubicCurve;
import javafx.scene.shape.Line;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;
import javafx.util.Duration;
public class DynamicHeart extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Group root = new Group();
// 绘制心形的两个半圆
Circle leftArc = new Circle(50, 50, 25);
leftArc.setFill(Color.RED);
Circle rightArc = new Circle(75, 50, 25);
rightArc.setFill(Color.RED);
// 绘制心形的顶部部分
Line topLine = new Line(25, 25, 100, 25);
topLine.setStroke(Color.RED);
topLine.setStrokeWidth(5);
// 绘制心形的左侧曲线
CubicCurve leftCurve = new CubicCurve(
25, 25,
-50, -25,
-50, 75,
25, 125
);
leftCurve.setFill(null);
leftCurve.setStroke(Color.RED);
leftCurve.setStrokeWidth(5);
// 绘制心形的右侧曲线
CubicCurve rightCurve = new CubicCurve(
100, 25,
175, -25,
175, 75,
100, 125
);
rightCurve.setFill(null);
rightCurve.setStroke(Color.RED);
rightCurve.setStrokeWidth(5);
// 添加到根节点
root.getChildren().addAll(leftArc, rightArc, topLine, leftCurve, rightCurve);
// 创建动画
Timeline timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(true);
// 定义动画路径
Shape path = Shape.union(leftCurve, rightCurve);
double pathLength = path.getLayoutBounds().getWidth();
KeyValue xValue1 = new KeyValue(root.translateXProperty(), 0, Interpolator.LINEAR);
KeyValue xValue2 = new KeyValue(root.translateXProperty(), pathLength, Interpolator.LINEAR);
KeyFrame keyFrame1 = new KeyFrame(Duration.ZERO, xValue1);
KeyFrame keyFrame2 = new KeyFrame(Duration.seconds(4), xValue2);
timeline.getKeyFrames().addAll(keyFrame1, keyFrame2);
// 显示场景
Scene scene = new Scene(root, 200, 200);
stage.setScene(scene);
stage.setTitle("Dynamic Heart");
stage.show();
// 开始动画
timeline.play();
}
}
```
这段代码使用JavaFX绘制了一个红色的心形,然后通过动画将它沿着曲线运动。你可以根据需求自定义心形的大小、颜色和运动路径,让它更加动态和有趣。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![html](https://img-home.csdnimg.cn/images/20210720083451.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)