获取一个节点的屏幕坐标JavaFX中8 [英] Get screen coordinates of a node in javaFX 8

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

问题描述

我开发在Windows 8.1中的64位JavaFX应用程序与使用JDK版本8u45 64 4GB内存。

我想用捕捉屏幕的一部分机器人,但问题是,我不能让锚面板的屏幕坐标,我想捕捉和我不想使用快照,因为输出质量差。这里是我的code。

我已经看到了这个环节的问题
<一href=\"https://stackoverflow.com/questions/1531538/getting-the-global-coordinate-of-a-node-in-javafx\">Getting JavaFX中节点的全局坐标
而这一次
得到的JavaFX 节点的实际位置
我想尽一切答案,但没有什么工作,图像显示在屏幕的不同部分。

 私人无效capturePane(){
    尝试{
        边界边界= pane.getLayoutBounds();
        的Point2D坐标= pane.localToScene(bounds.getMinX(),bounds.getMinY());
        INT X =(int)的coordinates.getX();
        INT Y =(int)的coordinates.getY();
        INT宽度=(INT)pane.getWidth();
        INT高度=(int)的pane.getHeight();
        矩形screenRect =新的Rectangle(X,Y,宽,高);
        BufferedImage的捕获=新的机器人()createScreenCapture(screenRect)。
        ImageIO.write(捕​​获,PNG,新的文件(image.png));
    }赶上(IOException异常|的AWTException前){
        ex.printStackTrace();
    }
}


解决方案

既然你已经开始与当地(未布局)坐标,使用 getBoundsInLocal()而不是<$的C $ C> getLayoutBounds()。既然你是想转换到屏幕(未场景)坐标,使用 localToScreen(...)而不是 localToScene(...)

 私人无效capturePane(){
    尝试{
        边界边界= pane.getBoundsInLocal();
        边界screenBounds = pane.localToScreen(边界);
        INT X =(int)的screenBounds.getMinX();
        INT Y =(int)的screenBounds.getMinY();
        INT宽度=(INT)screenBounds.getWidth();
        INT高度=(int)的screenBounds.getHeight();
        矩形screenRect =新的Rectangle(X,Y,宽,高);
        BufferedImage的捕获=新的机器人()createScreenCapture(screenRect)。
        ImageIO.write(捕​​获,PNG,新的文件(image.png));
    }赶上(IOException异常|的AWTException前){
        ex.printStackTrace();
    }
}

I am developing a JavaFX application on Windows 8.1 64bit with 4GB of RAM with JDK version 8u45 64bit.

I want to capture part of the screen using Robot but the problem is that I can't get the screen coordinates of the anchor pane that I want to capture and I don't want to use snapshot because the output quality is bad. Here is my code.

I have seen the question in this link Getting the global coordinate of a Node in JavaFX and this one get real position of a node in javaFX and I tried every answer but nothing is working, the image shows different parts of the screen.

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

解决方案

Since you are starting with local (not layout) coordinates, use getBoundsInLocal() instead of getLayoutBounds(). And since you are wanting to transform to screen (not scene) coordinates, use localToScreen(...) instead of 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();
    }
}

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

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