(1)定义一个名为BallPane的类,用于显示一个弹动的球;

(2)定义一个名为BounceBallControl的类,用来使用鼠标动作控制弹球,当鼠标按下的时候动画暂停,当鼠标释放的时候动画恢复执行,按下Up/Down方向键的时候可以增加/减少动画的速度。

BallPane类:

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.util.Duration; public class BallPane extends Pane{
private double radius = 20;
private double x = radius, y = radius;
private double dx = 1, dy = 1;
private Circle circle = new Circle(x, y, radius);
private Timeline animation; public BallPane() {
circle.setStroke(Color.BLACK);
circle.setFill(Color.ORANGE);
getChildren().add(circle); animation = new Timeline(
new KeyFrame(Duration.millis(50), e -> moveBall()));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
} public void play() {
animation.play();
} public void pause() {
animation.pause();
} public void increaseSpeed() {
animation.setRate(animation.getRate() + 0.1);
} public void decreaseSpeed() {
animation.setRate(animation.getRate() > 0 ? animation.getRate() - 0.1 : 0);
} public DoubleProperty rateProperty() {
return animation.rateProperty();
} protected void moveBall() {
if(x < radius || x > getWidth() - radius) {
dx *= -1;
}
if(y < radius || y > getHeight() - radius) {
dy *= -1;
} x += dx;
y += dy;
circle.setCenterX(x);
circle.setCenterY(y);
}
}

BounceBallControl类:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.stage.Stage; public class BounceBallControl extends Application{ @Override
public void start(Stage primaryStage) throws Exception {
BallPane ballPane = new BallPane(); ballPane.setOnMousePressed(e -> ballPane.pause());
ballPane.setOnMouseReleased(e -> ballPane.play()); //控制速度
ballPane.setOnKeyPressed(e -> {
if(e.getCode() == KeyCode.UP) {
ballPane.increaseSpeed();
}
else if(e.getCode() == KeyCode.DOWN) {
ballPane.decreaseSpeed();
}
}); Scene scene = new Scene(ballPane, 250, 150);
primaryStage.setTitle("Bounce Ball Control");
primaryStage.setScene(scene);
primaryStage.show(); //将输入焦点设置到ballPane上
ballPane.requestFocus();
} public static void main(String[] args) {
Application.launch(args);
} }

最新文章

  1. python基础之迭代器、装饰器、软件开发目录结构规范
  2. tomcat服务重启linux
  3. IOS RSA 加密方式
  4. 关于float的感悟
  5. C# DataGridView控件绑定数据后清空数据
  6. iTiTa再次回归,这一年我们都在干什么?
  7. HDU5093——Battle ships(最大二分匹配)(2014上海邀请赛重现)
  8. bzoj 2959 长跑(LCT+BCC+并查集)
  9. 海量Office文档搜索
  10. ZOJ1074 (最大和子矩阵 DP)
  11. hdu 1075 What Are You Talking About(字典树)
  12. JDBC复习
  13. Java课设-购物车系统
  14. beta冲刺3
  15. HTTP状态码总结
  16. C++之默认参数
  17. C++预编译头文件 – stdafx.h
  18. Python-Pool类
  19. Confluence 6 通过 SSL 或 HTTPS 运行 - 创建或请求一个 SSL 证书
  20. Android:getContext().getSystemService()

热门文章

  1. leetcood学习笔记-7
  2. common配置文件
  3. 批量更新数据(BatchUpdate)
  4. 阿里云HBase全新发布X-Pack NoSQL数据库再上新台阶
  5. php开发面试题---创建型设计模式1(创建型设计模式有哪几种)
  6. JAVA中 成员变量和和实例变量区别
  7. MySql命令行无法显示中文
  8. JAVA单元测试的用法和要点(入门篇)
  9. spring boot下WebSocket消息推送
  10. opencv——常见的操作