JAVA FX-时间线动画(在动画过程中找到某些x,y点) [英] JAVA FX - TimeLine Animation (Find certain x,y point during animation)

查看:55
本文介绍了JAVA FX-时间线动画(在动画过程中找到某些x,y点)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此程序中,我尝试在for循环中x == 600时使rect变为红色.基本上发生的是for循环的运行速度快于屏幕上的动画.矩形最终在实际击中JavaFX屏幕中的特定点之前变成红色.

In this program, I am trying to make rect turn red when x== 600 in the for-loop. What basically happens is that the for-loop runs faster than the animation on the screen. The rectangle ends up turning red before it actually hits that certain point within the JavaFX screen.

当它到达点x,y:(600,500)时,我想做的就是让蓝色矩形变成红色.

What I would like it to do it that when it hits point x,y:(600,500), make the blue rectangle turn red.

import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @author Owner
 */
public class TestPoint extends Application {

    @Override
    public void start(Stage primaryStage) {

        Pane root = new Pane();
        Scene scene = new Scene(root, 1000, 1000);
        Rectangle rect = new Rectangle();
        Rectangle rectTwo = new Rectangle();

        //Obstacle that other square must hit
        rectTwo.setWidth(100);
        rectTwo.setHeight(100);
        rectTwo.setX(500);
        rectTwo.setY(500);
        rectTwo.setFill(Color.PINK);

        //for loop that causes the animation to properly move
        for (int x = 800; x >= 0; x--) {
            rect.setWidth(100);
            rect.setHeight(100);
            rect.setX(800);
            rect.setY(500);
            rect.setFill(Color.BLUE);
            Timeline timeline = new Timeline();
            timeline.setCycleCount(1);
            timeline.setAutoReverse(true);
            final KeyValue kv = new KeyValue(rect.xProperty(), x);
            final KeyFrame kf = new KeyFrame(Duration.seconds(8), kv);
            timeline.getKeyFrames().add(kf);
            timeline.play();
            //if it hits the point of rectTwo, change to Color.RED
            System.out.println(x);
            if (x == 600) {
                rect.setFill(Color.RED);
                break;//end 
            }
        }

        root.getChildren().addAll(rect, rectTwo);
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

推荐答案

您误解了Timeline的工作方式.您的代码创建了201个Timeline并行运行的动画.在显示窗口之前 完成循环.以后,JavaFX会自动触发所有更新.

You misunderstood, how Timeline works. Your code creates 201 Timeline animations running in parallel. The loop is done before the window is shown. Any updates are automatically triggered by JavaFX later.

通过KeyFrame指定初始状态和目标状态就足够了. KeyFrame允许您指定要在特定时间执行的处理程序;这可以用来改变颜色.或者,可以使用onFinished处理程序为Rectangle着色.

Specifying the initial state and the target state via KeyFrames is sufficient. KeyFrames allow you to specify a handler to be executed at a specific time; this can be used to change the color. Alternatively the onFinished handler could be used for coloring the Rectangle.

rect.setWidth(100);
rect.setHeight(100);
rect.setY(500);
rect.setFill(Color.BLUE);

Timeline timeline = new Timeline(
        new KeyFrame(Duration.ZERO, new KeyValue(rect.xProperty(), 800)),
        new KeyFrame(Duration.seconds(8),
                evt -> rect.setFill(Color.RED),
                new KeyValue(rect.xProperty(), 600)));
timeline.play();

这篇关于JAVA FX-时间线动画(在动画过程中找到某些x,y点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆