Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get screen coordinates of a node in javaFX 8

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();
    }
}
like image 947
Waxren Avatar asked Aug 04 '15 10:08

Waxren


People also ask

What is StackPane in JavaFX?

StackPane lays out its children in a back-to-front stack. The z-order of the children is defined by the order of the children list with the 0th child being the bottom and last child on top. If a border and/or padding have been set, the children will be layed out within those insets.

Is a scene a node JavaFX?

A scene graph is a set of tree data structures where every item has zero or one parent, and each item is either a "leaf" with zero sub-items or a "branch" with zero or more sub-items. Each item in the scene graph is called a Node .

What is a pulse in JavaFX?

A pulse is an event that indicates to the JavaFX scene graph that it is time to synchronize the state of the elements on the scene graph with Prism. A pulse is throttled at 60 frames per second (fps) maximum and is fired whenever animations are running on the scene graph.

What is root node in JavaFX?

The individual items held within the JavaFX scene graph are known as nodes. Each node is classified as either a branch node (meaning that it can have children), or a leaf node (meaning that it cannot have children). The first node in the tree is always called the root node, and it never has a parent.


1 Answers

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();
    }
}
like image 51
James_D Avatar answered Sep 22 '22 07:09

James_D