javafx KeyFrame
2024-08-31 16:28:06
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
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 Main extends Application {
static int dx = ;
static int dy = ; public static void main(String[] args) {
Application.launch(args);
} @Override
public void start(final Stage primaryStage) {
primaryStage.setTitle("Animation");
Group root = new Group();
Scene scene = new Scene(root, , , Color.WHITE); primaryStage.setScene(scene);
addBouncyBall(scene);
primaryStage.show();
}
private void addBouncyBall(final Scene scene) {
final Circle ball = new Circle(, , ); final Group root = (Group) scene.getRoot();
root.getChildren().add(ball); Timeline tl = new Timeline();
tl.setCycleCount(Animation.INDEFINITE);
KeyFrame moveBall = new KeyFrame(Duration.seconds(.),
new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { double xMin = ball.getBoundsInParent().getMinX();
double yMin = ball.getBoundsInParent().getMinY();
double xMax = ball.getBoundsInParent().getMaxX();
double yMax = ball.getBoundsInParent().getMaxY(); if (xMin < || xMax > scene.getWidth()) {
dx = dx * -;
}
if (yMin < || yMax > scene.getHeight()) {
dy = dy * -;
} ball.setTranslateX(ball.getTranslateX() + dx);
ball.setTranslateY(ball.getTranslateY() + dy); }
}); tl.getKeyFrames().add(moveBall);
tl.play();
}
}
最新文章
- 1Z0-053 争议题目解析46
- 用dos命令备份和恢复sql server 数据库
- [UCSD白板题] Compute the Edit Distance Between Two Strings
- struts2的两个核心配置文件
- SQLserver的存储过程
- 【转】Git详解之一:Git起步
- java技术栈:一、java编程语言概述
- hg 证书验证失败
- 认识<;img>;标签,为网页插入图片
- poj Pie
- 《python基础教程》笔记之 基础知识
- 【原创整理,基于JavaScript的创建对象方式的集锦】
- IOS 使用动态库(dylib)和动态加载framework
- 解读Java内部类
- rsync源目录写法的一点小细节
- Shell脚本 | 性能测试之CPU占有率
- UVA1599-Ideal Path(BFS进阶)
- [IR] Arithmetic Coding
- Mysql分页之limit用法与limit优化
- 《Spring2之站立会议7》
热门文章
- vmware workstation虚拟机克隆后不能上网(桥接模式下)
- spinlock参考资料
- 错排公式 全排列函数 next_permitation(a,a+n)
- Centos安装masscan
- NodeJS学习笔记 (15)二进制数据-buffer(ok)
- HDU-1789 Doing Homework again 贪心问题 有时间限制的最小化惩罚问题
- 单调队列&;单调栈归纳
- iOS 全局修改UINavigation 后退按钮
- windows server,无桌面服务器 , 批处理更改时区
- 分享:FIFO 同步、异步以及Verilog代码实现