Java 围绕轴心旋转变换?

Java 围绕轴心旋转变换?,java,javafx,Java,Javafx,我使用RotateTransiton旋转一条线,但它似乎通过线的中心旋转。我想旋转它作为一个轴线的一端。如何做到这一点?旋转转换通过更改来工作,正如您所观察到的,它定义了围绕节点中心的旋转 如果要围绕不同的点旋转,请定义轴,设置轴,将其添加到线的轴,然后使用时间线操纵其角度 下面是一个例子: import javafx.animation.Animation; import javafx.animation.KeyFrame; import javafx.animation.KeyValue;

我使用RotateTransiton旋转一条线,但它似乎通过线的中心旋转。我想旋转它作为一个轴线的一端。如何做到这一点?

旋转转换通过更改来工作,正如您所观察到的,它定义了围绕节点中心的旋转

如果要围绕不同的点旋转,请定义轴,设置轴,将其添加到线的轴,然后使用时间线操纵其角度

下面是一个例子:

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;

public class RotateLineAboutEnd extends Application {

    @Override
    public void start(Stage primaryStage) {
        Line line = new Line(200, 200, 200, 350);
        Pane pane = new Pane(line);
        Rotate rotation = new Rotate();
        rotation.pivotXProperty().bind(line.startXProperty());
        rotation.pivotYProperty().bind(line.startYProperty());

        line.getTransforms().add(rotation);

        Timeline timeline = new Timeline(
                new KeyFrame(Duration.ZERO, new KeyValue(rotation.angleProperty(), 0)),
                new KeyFrame(Duration.seconds(1), new KeyValue(rotation.angleProperty(), 360)));

        Button button = new Button("Rotate");
        button.setOnAction(evt -> timeline.play());
        button.disableProperty().bind(timeline.statusProperty().isEqualTo(Animation.Status.RUNNING));

        HBox controls = new HBox(button);
        controls.setAlignment(Pos.CENTER);
        controls.setPadding(new Insets(12));

        BorderPane root = new BorderPane(pane, null, null, controls, null);
        Scene scene = new Scene(root, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
RotateTransfion通过更改来工作,正如您所观察到的,它定义了围绕节点中心的旋转

如果要围绕不同的点旋转,请定义轴,设置轴,将其添加到线的轴,然后使用时间线操纵其角度

下面是一个例子:

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;

public class RotateLineAboutEnd extends Application {

    @Override
    public void start(Stage primaryStage) {
        Line line = new Line(200, 200, 200, 350);
        Pane pane = new Pane(line);
        Rotate rotation = new Rotate();
        rotation.pivotXProperty().bind(line.startXProperty());
        rotation.pivotYProperty().bind(line.startYProperty());

        line.getTransforms().add(rotation);

        Timeline timeline = new Timeline(
                new KeyFrame(Duration.ZERO, new KeyValue(rotation.angleProperty(), 0)),
                new KeyFrame(Duration.seconds(1), new KeyValue(rotation.angleProperty(), 360)));

        Button button = new Button("Rotate");
        button.setOnAction(evt -> timeline.play());
        button.disableProperty().bind(timeline.statusProperty().isEqualTo(Animation.Status.RUNNING));

        HBox controls = new HBox(button);
        controls.setAlignment(Pos.CENTER);
        controls.setPadding(new Insets(12));

        BorderPane root = new BorderPane(pane, null, null, controls, null);
        Scene scene = new Scene(root, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
关于JavaFX2.2和JavaFX8 在我看来,最好的方法是转换包含轴心点的节点的layoutBounds,并以相反的方式转换节点本身

例如:

关于JavaFX2.2和JavaFX8 在我看来,最好的方法是转换包含轴心点的节点的layoutBounds,并以相反的方式转换节点本身

例如:


我尝试了下面的答案,但它对我不起作用。然而,我发现这对我来说非常有效。我尝试了下面的答案,但它对我不起作用。然而,我发现这对我来说非常有效。