TranslateTransition不会改变(X,Y)坐标 [英] TranslateTransition does not change (X,Y) co-ordinates

查看:371
本文介绍了TranslateTransition不会改变(X,Y)坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习 javafx.animation API时,我尝试了以下代码,
只需在(100,100)绘制一个矩形,翻译它在2秒内达到(200,200)。

While learning the javafx.animation API, I tried the following code, which simply draws a rectangle at (100,100) and translates it to (200,200) in 2 seconds.

import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;

public class SimpleAnimation extends Application {

  @Override
  public void start(Stage stage) {
    Group root = new Group();
    Scene scene = new Scene(root, 500, 500);
    stage.setScene(scene);

    VBox vb = new VBox();

    Rectangle rect = new Rectangle (100, 100, 100, 100);
    rect.setArcHeight(50);
    rect.setArcWidth(50);
    rect.setFill(Color.VIOLET);

    final Duration SEC_2 = Duration.millis(2000);

    System.out.println("Location before relocation = "+rect.getX()+","+rect.getY()+")");

    TranslateTransition tt = new TranslateTransition(SEC_2,rect);
    tt.setByX(100f);
    tt.setByY(100f);
    tt.setOnFinished(new EventHandler<ActionEvent>() {
                       @Override
                       public void handle(ActionEvent event) {
                         System.out.println("Location after relocation = " + rect.getX() + "," + rect.getY() + ")");
                       }
                     });
    tt.play();

    vb.getChildren().add(rect);

        scene.setRoot(vb);
        stage.show();
      }

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

在此translationTransition之后,预期矩形将具有(x,y)坐标为(200,200)对吗?

为了检查这一点,我尝试在 OnFinished()事件中打印坐标处理器。

不知道为什么它仍然打印(100,100)

After this translationTransition, the rectangle is expected to have the (x,y) coordinates as (200,200) right?
To check this, I tried to print the coordinates in the OnFinished() event handler.
Don't know why it still prints (100,100) ?

推荐答案

Javadocs


此Transition创建一个跨越
持续时间的移动/翻译动画。这是通过定期更新节点的translateX,translateY和
translateZ变量来完成的。

This Transition creates a move/translate animation that spans its duration. This is done by updating the translateX, translateY and translateZ variables of the node at regular interval.

所以,如果你检查过渡结束时 translateX translateY 属性,你会发现它们都等于 100 ;即矩形已经从原来的位置翻译了两个方向的 100 单位。

So if you check the translateX and translateY properties at the end of the transition, you will find they are both equal to 100; i.e. the rectangle has been translated from its original position by 100 units in both directions.

具体取决于你想要这样做,你可以使用 translateX translateY 属性,或者使用 boundsInParent property,它给出父节点坐标系中节点的边界(因此包括任何变换,例如翻译)。

Depending on exactly what you are wanting to do, you can either work with the translateX and translateY properties, or work with the boundsInParent property, which gives the bounds of a node in its parent's coordinate system (and thus includes any transforms, such as the translation).

如果你喜欢有 x y Rectangle 的属性直接从动画,使用 时间轴 ,允许您指定更改的属性(或属性):

If you prefer to have the x and y properties of the Rectangle change directly from the animation, use a Timeline, which allows you to specify the property (or properties) that change:

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class SimpleAnimation extends Application {

    @Override
    public void start(Stage stage) {

        VBox vb = new VBox();

        Rectangle rect = new Rectangle(100, 100, 100, 100);
        rect.setManaged(false);
        rect.setArcHeight(50);
        rect.setArcWidth(50);
        rect.setFill(Color.VIOLET);

        final Duration SEC_2 = Duration.millis(2000);

        System.out.println("Location before relocation = " + rect.getX() + ","
                + rect.getY() + ")");

        Timeline timeline = new Timeline();

        KeyFrame end = new KeyFrame(SEC_2,
                new KeyValue(rect.xProperty(), 200),
                new KeyValue(rect.yProperty(), 200));

        timeline.getKeyFrames().add(end);

        timeline.setOnFinished(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Location after relocation = " + rect.getX()
                        + "," + rect.getY() + ")");
            }
        });
        timeline.play();

        vb.getChildren().add(rect);

        Scene scene = new Scene(vb, 500, 500);
        stage.setScene(scene);

        stage.show();
    }

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

请注意,为此,我添加了通话 rect.setManaged(假)。由于您在 VBox 中有 Rectangle ,它管理其子节点的布局,因此更改 x y 属性将无效。 (另一种选择是用 Pane 替换 VBox ,它不管理其子节点的放置。)

Note that for this to work, I added the call rect.setManaged(false). Since you have the Rectangle in a VBox, which manages layout of its child nodes, changing the x and y properties will have no effect otherwise. (The other option would be to replace the VBox with a Pane, which doesn't manage placement of its child nodes.)

这篇关于TranslateTransition不会改变(X,Y)坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆