JavaFX路径转换绑定

时间:2017-03-13 00:40:45

标签: java animation javafx

我现在正在学习JavaFX。我在使用带有属性绑定的PathTransition时遇到了麻烦。以下代码的目的是使矩形在圆轨道上移动。此外,我将圆圈的中心与窗格的高度和宽度除以2,这意味着窗格的中心。

import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * Created by Younghee on 2017-03-12.
 */
public class PathTransitionDemo extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane pane = new Pane();
        Circle circle = new Circle(50);
        Rectangle rec = new Rectangle(20,10);

        circle.setFill(Color.WHITE);
        circle.setStroke(Color.BLACK);
        circle.centerXProperty().bind(pane.widthProperty().divide(2));
        circle.centerYProperty().bind(pane.heightProperty().divide(2));
        rec.setStroke(Color.WHITE);
        rec.setFill(Color.ORANGE);
        pane.getChildren().addAll(circle, rec);
        pane.setPadding(new Insets(11,11,11,11));

        PathTransition pt = new PathTransition();
        pt.setNode(rec);
        pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
        pt.setDuration(Duration.millis(4000));
        pt.setPath(circle);
        pt.setCycleCount(Timeline.INDEFINITE);
        pt.setAutoReverse(true);
        pt.play();

        primaryStage.setScene(new Scene(pane));
        primaryStage.setTitle("Path Transition Demo");
        primaryStage.show();
    }
}

但是,矩形不会移动到指定的圆上,而是移动到中心为(0,0)的另一个圆轨道上。我该如何解决?

1 个答案:

答案 0 :(得分:0)

我可以给你一个未完成的答案。您可以在中心之后创建PathTransition(这应该是根本原因),如下所示。

public class PathTransitionDemo extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane pane = new Pane();
        Circle circle = new Circle(50);
        Rectangle rec = new Rectangle(20,10);

        circle.setFill(Color.WHITE);
        circle.setStroke(Color.BLACK);
        circle.centerXProperty().bind(pane.widthProperty().divide(2));
        circle.centerYProperty().bind(pane.heightProperty().divide(2));
        rec.setStroke(Color.WHITE);
        rec.setFill(Color.ORANGE);
        pane.getChildren().addAll(circle, rec);
        pane.setPadding(new Insets(11,11,11,11));
        circle.centerXProperty().addListener(new ChangeListener<Number>() {

          @Override
          public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
            PathTransition pt = new PathTransition();
            pt.setNode(rec);
            pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
            pt.setDuration(Duration.millis(4000));
            pt.setPath(circle);
            pt.setCycleCount(Timeline.INDEFINITE);
            pt.setAutoReverse(true);
            pt.play();
          }
        });

        primaryStage.setScene(new Scene(pane));
        primaryStage.setTitle("Path Transition Demo");
        primaryStage.show();
    }
    public static void main(String[] args) {
      launch(args);
    }
}