Java做动图
时间: 2024-02-11 18:05:41 浏览: 113
你可以使用Java的图形库JavaFX或者Swing来做动图。其中,JavaFX是Java 8之后才开始出现的图形库,它支持更多的动画特效,并且在性能上也比Swing更好。下面是一个使用JavaFX制作动图的简单示例代码:
```java
import javafx.animation.Animation;
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.stage.Stage;
import javafx.util.Duration;
public class AnimatedCircle extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// 创建一个圆形
Circle circle = new Circle(50, Color.BLUE);
circle.setTranslateX(100);
circle.setTranslateY(100);
// 创建一个时间轴
Timeline timeline = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(circle.translateXProperty(), 100), new KeyValue(circle.translateYProperty(), 100)),
new KeyFrame(Duration.seconds(2), new KeyValue(circle.translateXProperty(), 300), new KeyValue(circle.translateYProperty(), 300)));
timeline.setCycleCount(Animation.INDEFINITE); // 循环播放
// 创建一个场景
Group root = new Group(circle);
Scene scene = new Scene(root, 400, 400, Color.WHITE);
// 将场景添加到舞台
primaryStage.setScene(scene);
primaryStage.setTitle("Animated Circle");
primaryStage.show();
// 播放动画
timeline.play();
}
public static void main(String[] args) {
launch(args);
}
}
```
这段代码创建了一个蓝色的圆形,并且将其沿着一个斜线运动。你可以通过修改时间轴中的关键帧来改变圆形的运动轨迹和速度。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)