获取javaFX 8中节点的屏幕坐标

我正在开发一个Windows 8.1 64位的JavaFX应用程序,带有4GB的RAM,JDK版本为8u45 64bit。

我想使用Robot捕获部分屏幕,但问题是我无法获取我想要捕获的锚定窗格的屏幕坐标,我不想使用snapshot因为输出质量很差。 这是我的代码。

我已经在这个链接中看到了这个问题在JavaFX中 获取 Node的全局坐标,并且这个在javaFX中获得了一个节点的真实位置,我尝试了每个答案,但没有任何工作,图像显示了屏幕的不同部分。

 private void capturePane() { try { Bounds bounds = pane.getLayoutBounds(); Point2D coordinates = pane.localToScene(bounds.getMinX(), bounds.getMinY()); int X = (int) coordinates.getX(); int Y = (int) coordinates.getY(); int width = (int) pane.getWidth(); int height = (int) pane.getHeight(); Rectangle screenRect = new Rectangle(X, Y, width, height); BufferedImage capture = new Robot().createScreenCapture(screenRect); ImageIO.write(capture, "png", new File("image.png")); } catch (IOException | AWTException ex) { ex.printStackTrace(); } } 

由于您使用的是本地(非布局)坐标,因此请使用getBoundsInLocal()而不是getLayoutBounds() 。 由于您想要转换为屏幕(而不是场景)坐标,请使用localToScreen(...)而不是localToScene(...)

 private void capturePane() { try { Bounds bounds = pane.getBoundsInLocal(); Bounds screenBounds = pane.localToScreen(bounds); int x = (int) screenBounds.getMinX(); int y = (int) screenBounds.getMinY(); int width = (int) screenBounds.getWidth(); int height = (int) screenBounds.getHeight(); Rectangle screenRect = new Rectangle(x, y, width, height); BufferedImage capture = new Robot().createScreenCapture(screenRect); ImageIO.write(capture, "png", new File("image.png")); } catch (IOException | AWTException ex) { ex.printStackTrace(); } }