首页 > 解决方案 > 让 rotateTransition 和 pathtransition 在可调整大小的窗口上工作

问题描述

我在开发一个测试 Javafx 程序时遇到了一点问题,该程序处理从不同方向在屏幕上传递矩形对象。为了完成这项工作,我创建了矩形对象,然后创建了三个线对象。然后,我创建了一个 PathTransition 对象,该对象将沿着每个线对象移动一个矩形。从那里,我创建了一个 Pane 对象,并在其顶部添加了三个矩形和三条线。为了模拟矩形来自不同方向,我使用了 RotateTransition 对象并将 Pane 对象用作其节点。基本上,该程序只是一个旋转窗格,其中包含三行,每行都有一个矩形沿着所述行的路径向下移动。但是,当我想调整窗口大小时,我不会 不知道如何在不弄乱动画的情况下准确地使窗格的大小随之变化。任何帮助将不胜感激。谢谢!这是代码:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.animation.PathTransition;
import javafx.animation.RotateTransition;
import javafx.animation.Timeline;

public class Test extends Application {

    @Override
    public void start(Stage primaryStage) {
        // Pane object
        Pane pane = new Pane();

        // Rectangle objects
        Rectangle yellowRectangle = new Rectangle(10,10,10,10);
        yellowRectangle.setFill(Color.YELLOW);
        Rectangle greenRectangle = new Rectangle(10,10,10,10);
        greenRectangle.setFill(Color.GREEN);
        Rectangle blueRectangle = new Rectangle(10,10,10,10);
        blueRectangle.setFill(Color.BLUE);

        Line yellowLine = new Line(0,0,500,500);
        yellowLine.setStroke(Color.YELLOW);

        Line greenLine = new Line(500,0,0,500);
        greenLine.setStroke(Color.GREEN);

        Line blueLine = new Line(0,250,500,250);
        blueLine.setStroke(Color.BLUE);

        pane.getChildren().addAll(yellowRectangle, greenRectangle, blueRectangle, yellowLine, greenLine, blueLine);

        // Pathtransitions
        PathTransition yellowPath = new PathTransition();
        yellowPath.setDuration(Duration.millis(4000));
        yellowPath.setPath(yellowLine);
        yellowPath.setNode(yellowRectangle);
        yellowPath.setCycleCount(Timeline.INDEFINITE);
        yellowPath.setAutoReverse(true);
        yellowPath.play();

        PathTransition greenPath = new PathTransition();
        greenPath.setDuration(Duration.millis(4000));
        greenPath.setPath(greenLine);
        greenPath.setNode(greenRectangle);
        greenPath.setCycleCount(Timeline.INDEFINITE);
        greenPath.setAutoReverse(true);
        greenPath.play();

        PathTransition bluePath = new PathTransition();
        bluePath.setDuration(Duration.millis(4000));
        bluePath.setPath(blueLine);
        bluePath.setNode(blueRectangle);
        bluePath.setCycleCount(Timeline.INDEFINITE);
        bluePath.setAutoReverse(true);
        bluePath.play();

        // Rotate Transition
        RotateTransition yellowRotate = new RotateTransition();
        yellowRotate.setAxis(Rotate.Z_AXIS);
        yellowRotate.setByAngle(360);
        yellowRotate.setCycleCount(500);
        yellowRotate.setDuration(Duration.millis(10000));
        yellowRotate.setAutoReverse(true);
        yellowRotate.setNode(pane);
        yellowRotate.play();

        // Borderpane
        BorderPane borderPane = new BorderPane();
        borderPane.setCenter(pane);
        Scene scene = new Scene(borderPane, 1000, 1000);

        // Bind the BorderPane to the scene
        borderPane.prefHeightProperty().bind(scene.heightProperty());
        borderPane.prefWidthProperty().bind(scene.widthProperty());
        primaryStage.setTitle("Test");
        primaryStage.setScene(scene);
        primaryStage.setResizable(true);
        primaryStage.show();
    }
}

标签: animationjavafx

解决方案


推荐阅读