获取JavaFX中节点的全局坐标 [英] Getting the global coordinate of a Node in JavaFX

查看:2333
本文介绍了获取JavaFX中节点的全局坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在场景中获取节点的实际位置。绝对的位置,不管任何容器/转换。

How can I get the actual position of a node in the scene. The absolute position, regardless of any containers/transforms.

例如,我想翻译某个节点 a 因此它将暂时与另一个节点 b 重叠。所以我希望将他的 translateX 属性设置为 b.globalX-a.globalX

For example, I want to translate a certain node a so that it would temporarily overlap another node b. So I wish to set his translateX property to b.globalX-a.globalX.

文档说:


定义添加到

翻译的X坐标
转换此节点
的坐标以进行布局。容器
或执行布局的组将设置
相对于
layoutBounds.minX的此变量,以便将
定位在所需布局
位置的节点。

Defines the X coordinate of the translation that is added to the transformed coordinates of this Node for the purpose of layout. Containers or Groups performing layout will set this variable relative to layoutBounds.minX in order to position the node at the desired layout location.

例如,如果孩子应该有一个
finalX的最终位置:

For example, if child should have a final location of finalX:



 child.layoutX = finalX - child.layoutBounds.minX;

也就是说,任何节点的最终坐标应该是

That is, the final coordinates of any node should be

finalX = node.layoutX + node.layoutBounds.minX

但运行以下代码:

var rect;
Stage {
    title: "Application title"
    width: 250
    height:250
    scene: Scene {
        content: [
            Stack{content:[rect = Rectangle { width:10 height:10}] layoutX:10}
        ]
    }
}

println("finalX = {rect.layoutX+rect.layoutBounds.minX}");

给我 finalX = 0.0 而不是 finalX = 10.0 作为文档看起来状态。

gives me finalX = 0.0 instead of finalX = 10.0 as the docs seemingly state.

有没有一个明确的方法来获得绝对的最终定位坐标在JavaFX

Is there a clear method to get the absolutely final positioning coordinates in JavaFX?

推荐答案

目前我发现的唯一解决方案是

The only solution I found so far is

rect.localToScene(rect.layoutBounds.minX, rect.layoutBounds.minY) // a Point2D{x:Float y:Float} object

我似乎并不认为这是最好的方式(请注意,此函数未绑定)。仍然适用于JavaFX 1.2。

Which doesn't seem to me as the "best" way to do that (note that this function is not bound). Still it works for JavaFX 1.2.

这篇关于获取JavaFX中节点的全局坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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