1

Нужно сделать анимацию вращение в JavaFX. Использую класс RotateTransition, но там по умолчанию вращение вокруг центра фигуры. Как задать точку вращения (2D)?

1
  • Примите, пожалуйста, ответ, если он вам помог.
    – Axenow
    8 окт 2018 в 9:31

1 ответ 1

2

Как-то так.

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

Если надо сделать именно через RotateTransition (только для JavaFX 2.2 и JavaFX 8)

import javafx.animation.RotateTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;
import javafx.util.Duration;

public class RotateTransTest1 extends Application {

    @Override
    public void start(Stage primaryStage) {
        Rectangle rect = new Rectangle(1, 1, 40, 40);
        rect.setFill(Color.BLUE);

        movePivot(rect, 50, 50);

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

        Group root = new Group(rect);
        Scene tScene = new Scene(root, 140, 140);
        primaryStage.setScene(tScene);


        primaryStage.show();
    }

    // Вот тут вся магия
    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);
    }
}
2
  • Можно сказать, что класс RotateTransition может вращать фигуры только вокруг их центра? 8 окт 2018 в 11:54
  • @EvgeniyTkachenko На самом деле нет. Обновил ответ. Просто я этот способ сам использовал.
    – Axenow
    8 окт 2018 в 12:09

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.