如何在父母的坐标系中平移节点? [英] How to translate a node in a parent's coordinate system?

查看:98
本文介绍了如何在父母的坐标系中平移节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个节点,该节点是本地组的子级,而该节点是全局组的子级。我想在全局组的坐标中平移子级,但是 TranslateTransition 在局部组的坐标中移动子级。

I have a node which is a child of a "local" group which is a child of a "global" group. I want to translate the child in the coordinates of the global group but TranslateTransition moves it in the local group coordinates.

在此示例中,我具有以下父级层次结构:

In this example I have this parent hierarchy:

parent group
|- red circle
|- child group
  |- blue circle

我想让蓝色圆圈位于红色圆圈。如果我将其转换为红色圆圈的坐标,则它会远离它,因为它在自己的组中。

I want to get the blue circle on top of the red circle. If I translate it to the coordinates of the red circle it moves away from it because it's in its own group. If i translate the whole group it works.

import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class AnimTest extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Circle c = new Circle(5, Color.BLUE);
        Group group = new Group(c);
        group.setTranslateX(40);
        group.setTranslateY(50);

        Circle target = new Circle(10, Color.RED);
        target.setTranslateX(20);
        target.setTranslateY(20);

        Group parent = new Group(target, group);

        TranslateTransition t1 = new TranslateTransition(Duration.seconds(1), c); // 'group' works
        t1.setToX(target.getTranslateX());
        t1.setToY(target.getTranslateY());

        Button next = new Button("Play");
        next.setOnAction(e -> t1.play());

        Pane p = new Pane(parent);
        p.setPrefSize(200, 200);
        VBox root = new VBox(p, next);
        stage.setScene(new Scene(root));
        stage.show();
    }

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

如何翻译蓝色圆圈

请理解,这是一个简单的示例。实际上,层次结构更大,看起来像

Please understand that this is a simple example. In truth the hierarchy is bigger and looks something like

parent group
|- target node
|- group1
  |- group2
    |- group3
      |- moving node

而且我也可以使用目标的边界而不是其转换属性来找到最终的目的地,但是对于示例而言,转换就足够了。

And also i can use the bounds of the target and not its translate properties to find the final destination but translate were enough for the example.

推荐答案

您可以组合使用 Node.localToScene Node.sceneToLocal 在同一场景中的坐标系之间进行转换(假设转换是可逆的,并且您没有按0缩放或类似的比例)。

You can combine Node.localToScene and Node.sceneToLocal to convert between coordinate systems in the same scene (assuming the transformations are invertible and you don't scale by 0 or something like this).

private static Point2D convertCoordinater(Node source, Node target, double x, double y) {
    Point2D sceneCoords = source.localToScene(x, y);
    return target.sceneToLocal(sceneCoords);
}





Point2D targetCoordinates = convertCoordinater(parent, group, target.getTranslateX(), target.getTranslateY());
t1.setToX(targetCoordinates.getX());
t1.setToY(targetCoordinates.getY());

这篇关于如何在父母的坐标系中平移节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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