Code‎ > ‎

JavaFX


Find Node's absolute center:

Groovy snippet to find out the center of a JavaFX Node in screen (absolute) coordinates:

static Point centerOf( Node node ) {
  def windowPos = new Point( node.scene.window.x.intValue(), node.scene.window.y.intValue() )
  def scenePos = new Point( node.scene.x.intValue(), node.scene.y.intValue() )
  def boundsInScene = node.localToScene node.boundsInLocal
  def absX = windowPos.x + scenePos.x + boundsInScene.minX
  def absY = windowPos.y + scenePos.y + boundsInScene.minY
  [ ( absX + boundsInScene.width / 2 ).intValue(),
  ( absY + boundsInScene.height / 2 ).intValue() ] as Point
}




Comments