java rotatetransition,RotateTransition围绕一个支点?

I am using RotateTransiton to rotate a line, but it seems to rotate through center of the line. I would like to rotate it with pivot as one end of the line. How to do this?

解决方案

The RotateTransition works by changing the rotate property, which - as you have observed - defines a rotation around the center of the Node.

If you want to rotate around a different point, define a Rotate transform, set its pivot, add it to the line's list of transforms, and use a Timeline to manipulate its angle.

Here's an example:

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

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值