RotateTransition绕一个支点?

时间:2015-02-21 22:29:00

标签: java javafx

我正在使用RotateTransiton来旋转线条,但它似乎在线条的中心旋转。我想用枢轴旋转它作为线的一端。怎么做?

2 个答案:

答案 0 :(得分:3)

(在JavaFX 2.2和JavaFX 8上) 在我看来,最好的方法是转换节点的layoutBounds(包含枢轴点)并以相反的方式转换节点本身。

一个例子:     

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Group root = new Group();
        primaryStage.setScene(new Scene(root, 140, 140));

        Rectangle rect = new Rectangle(1, 1, 40, 40);

        // comment movePivot to get the default rotation
        movePivot(rect, -20, -20);

        RotateTransition rt = new RotateTransition(Duration.seconds(4),rect);
        rt.setToAngle(720);
        rt.setCycleCount(Timeline.INDEFINITE);
            rt.setAutoReverse(true);
        rt.play();

        primaryStage.show();
    }

    // this is the function you want
    private void movePivot(Node node, double x, double y){
        node.getTransforms().add(new Translate(-x,-y));
        node.setTranslateX(x); node.setTranslateY(y);
    }

    public static void main(String[] args) {
        launch(args);
    }
}

答案 1 :(得分:2)

RotateTransition通过更改rotate property来发挥作用,正如您所观察到的那样,它定义了围绕Node中心的旋转。

如果要围绕其他点旋转,请定义Rotate transform,设置其轴心,将其添加到线list of transforms,然后使用Timeline来操纵其角度。< / p>

以下是一个例子:

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);
    }
}