利用JavaFX实现一个弹球动画

来源:互联网 发布:适合mac的杀毒软件 编辑:程序博客网 时间:2024/06/14 19:32

程序使用Timeline来实现弹球动画,运行结果如图:
这里写图片描述
一个球在窗口中弹动
里写图片描述

下面是编写这个程序的关键步骤:
1)定义一个名为BallPane的Pane类的子类,用于显示一个弹动的球。
2)定义一个名为BounceBallcontrol的Application的子类,用来使用鼠标动作控制弹球。当鼠标按下的时候动画暂停,当鼠标释放的时候动画恢复执行。按下UP/DOWN方向键可以增加/减少动画的速度。

package com.company;import javafx.animation.Timeline;import javafx.animation.KeyFrame;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{    public final 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.setFill(Color.GREEN);        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);    }}

程序简单解读:①求的中心位于(x,y),下一次移动中改变成(x+dx,y+dy)。当球超出水平边界时,dx的符号发生改变(第46~49行);y也相同。②pause和play方法可以用于暂停和恢复动画。③increasedSpeed()和decreasedSpeed()方法可以用于增加和减少动画速度。④rateProperty()方法返回一个速率的绑定属性。

package com.company;import javafx.animation.Timeline;import javafx.application.Application;import javafx.geometry.Pos;import javafx.scene.Scene;import javafx.scene.input.KeyCode;import javafx.scene.layout.BorderPane;import javafx.stage.Stage;import javafx.util.Duration;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.animation.KeyFrame;import javafx.scene.control.Label;public class BounceBallControl extends Application {    @Override    public void start(Stage primaryStage) {        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("BounceBallControl");        primaryStage.setScene(scene);        primaryStage.show();        ballPane.requestFocus();    }}

BounceBallControl类是继承自Application的javaFX的主类,用于显示弹球的面板并具有控制功能。其中针对弹球面板实现了鼠标按下和鼠标释放的处理器,以暂停和恢复动画。当UP方向键被按下,弹球面板的increasedSpeed()方法就会被调用以增加求的移动,对于DOWN方向键也同理。
最后调用ballPane.requFocus()将输入焦点设置到ballPane上。