Getting the global coordinate of a Node in JavaFX
Asked Answered
K

3

11

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

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.

The documentation says:

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.

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

However running the following code:

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

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

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

Kursh answered 7/10, 2009 at 13:7 Comment(5)
Just wondering, what format is the code above in? It isn't Java, maybe JSON?Enchase
It's JavaFX, a new programming language from Sun/Oracle.Kursh
The format, I know it's JavaFX, but it isn't Java code.Enchase
@chris13524 JavaFX had it's own format back in the JavaFX 1.x days.Norine
JavaFX is not a "programming language" - its a framework for / within Java ... Oracle ships it with every JRE and JDK, no additional packaging needed (anymore)Starbuck
K
5

The only solution I found so far is

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

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.

Kursh answered 7/10, 2009 at 13:26 Comment(0)
S
14

For bounds:

bounds = rect.localToScene(rect.getBoundsInLocal());

Work for JavaFx 1 and 2.

Secretary answered 3/10, 2012 at 20:21 Comment(0)
K
5

The only solution I found so far is

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

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.

Kursh answered 7/10, 2009 at 13:26 Comment(0)
C
2

Since JavaFX 8, there are additional methods converting local coordinates to screen coordinates. Their names start with "localToScreen"

You can check following link http://docs.oracle.com/javase/8/javafx/api/javafx/scene/Node.html#localToScreen-double-double-

Chook answered 10/7, 2014 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.