胖大海瘦西湖 2016-02-23 07:59 采纳率: 0%
浏览 896
已结题

javafx让小球从上次静止的位置开始运行?

有一个BallPane类,

 package javafx_ex;
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{
    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()+1);
    }

    public void decreaseSpeed(){
        animation.setRate(animation.getRate()>0 ? animation.getRate()-1 : 0);
    }

    public DoubleProperty rateProperty(){
        if(animation.getRate()==0.0){
            circle=new Circle(circle.getCenterX(),circle.getCenterY(),radius);
            circle.setFill(Color.GREEN);
        }
        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);
    }
}

主函数,

 package javafx_ex;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;

public class BounceBallSlider extends Application{
    @Override
    public void start(Stage primaryStage){
        BallPane ballPane=new BallPane();

        Slider slSpeed=new Slider();
        slSpeed.setMax(20);
        slSpeed.setValue(10);
        ballPane.rateProperty().bind(slSpeed.valueProperty());

        BorderPane pane=new BorderPane();
        pane.setCenter(ballPane);
        pane.setBottom(slSpeed);

        ballPane.setOnMousePressed(e->ballPane.pause());
        ballPane.setOnMouseReleased(e->ballPane.play());
        slSpeed.setOnKeyPressed(e->{
            if(e.getCode()==KeyCode.UP){
                slSpeed.setValue(slSpeed.getValue()+1);
            }
            else if(e.getCode()==KeyCode.DOWN){
                slSpeed.setValue(slSpeed.getValue()-1);
            }
        });

        Scene scene=new Scene(pane,250,150);
        primaryStage.setTitle("BounceBallControl");
        primaryStage.setScene(scene);
        primaryStage.show();

        slSpeed.requestFocus();
    }
    public static void main(String[] args){
        Application.launch(args);
    }
}

出现的问题是:小球速度降为0后,再增加速度,小球不是从上次静止的位置开始运动,如果让它从上次静止的位置开始运动呢?
我查看了Animation.class的源代码,里面有这样一句话,

 An {@code Animation}'s play head can be randomly positioned, whether it is
 * running or not. If the {@code Animation} is running, the play head jumps to
 * the specified position immediately and continues playing from new position.
 * If the {@code Animation} is not running, the next {@link #play()} will start
 * the {@code Animation} from the specified position.

这句话是什么意思呢?

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?