【问题标题】:Get screen coordinates of a node in javaFX 8在javaFX 8中获取节点的屏幕坐标
【发布时间】:2015-10-26 17:49:46
【问题描述】:

我正在 Windows 8.1 64 位和 4GB RAM 和 JDK 版本 8u45 64 位上开发 JavaFX 应用程序。

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

我在这个链接中看到了这个问题 Getting the global coordinate of a Node in JavaFX 和这个 get real position of a node in 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();
    }
}

【问题讨论】:

  • 快照的“输出质量”差在哪方面?
  • 有时图像不清晰。
  • 你试过localToScreen()吗,因为本地到场景会返回场景内的坐标,当你放入机器人时会捕捉到不同的位置
  • @TomasBisciak 我刚刚尝试了 localToScreen() 并且图像进一步显示了屏幕的一角

标签: java javafx awt javafx-8 awtrobot


【解决方案1】:

由于您从本地(而非布局)坐标开始,请使用 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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 2012-02-02
    • 2020-04-13
    • 2021-10-22
    • 2015-08-01
    • 2016-10-09
    相关资源
    最近更新 更多