Get screen coordinates of a node in javaFX 8

Waxren

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JavaFx - Get the coordinates(x,y) of a node or control whose parent is not a Pane

From Dev

Delphi, TTreeView: how to get the screen coordinates of the given node and its icon?

From Dev

How to get object coordinates from screen coordinates?

From Dev

Caret position relative to screen coordinates in javaFX

From Dev

Get the center coordinates of phone screen

From Dev

Get Screen-Coordinates of a QuickControl

From Dev

Get the center coordinates of phone screen

From Dev

Get coordinates (screen) of GridLayout items

From Dev

JavaFX 8 is there way to get the FXML reference from a Node?

From Dev

How to get node bounds according to its specific ancestor in JavaFX 8?

From Dev

How to get coordinates for the center of Path in javafx?

From Dev

JavaFX - get scene coordinates of child in ScrollPane

From Dev

How to get world coordinates from screen coordinates in Vispy

From Dev

Get Canvas X and Y coordinates and show on screen

From Dev

Get the coordinates at the edge of the screen from a given angle

From Dev

Get the coordinates of visible part of screen when zoomed in

From Dev

How to get coordinates of touch screen points in ogl?

From Dev

Get the coordinates at the edge of the screen from a given angle

From Dev

JavaFX 8 Dynamic Node scaling

From Dev

How can I get a component's position on screen (screen coordinates)

From Dev

Get real position of a node in JavaFX

From Dev

JavaFX: Get Node by row and column

From Dev

How to get current screen details in javafx?

From Dev

Get screen size on iOS 8

From Dev

Can't get mouse event from any other javafx 8 node after getting MOUSE_PRESSED event from one node

From Dev

Tool to easily select a pixel on screen and get color and absolute coordinates

From Dev

QML-maps: get coordinates when tap the screen

From Dev

Tool to easily select a pixel on screen and get color and absolute coordinates

From Dev

How to get screen coordinates of view inside of keyboard accessory view?

Related Related

  1. 1

    JavaFx - Get the coordinates(x,y) of a node or control whose parent is not a Pane

  2. 2

    Delphi, TTreeView: how to get the screen coordinates of the given node and its icon?

  3. 3

    How to get object coordinates from screen coordinates?

  4. 4

    Caret position relative to screen coordinates in javaFX

  5. 5

    Get the center coordinates of phone screen

  6. 6

    Get Screen-Coordinates of a QuickControl

  7. 7

    Get the center coordinates of phone screen

  8. 8

    Get coordinates (screen) of GridLayout items

  9. 9

    JavaFX 8 is there way to get the FXML reference from a Node?

  10. 10

    How to get node bounds according to its specific ancestor in JavaFX 8?

  11. 11

    How to get coordinates for the center of Path in javafx?

  12. 12

    JavaFX - get scene coordinates of child in ScrollPane

  13. 13

    How to get world coordinates from screen coordinates in Vispy

  14. 14

    Get Canvas X and Y coordinates and show on screen

  15. 15

    Get the coordinates at the edge of the screen from a given angle

  16. 16

    Get the coordinates of visible part of screen when zoomed in

  17. 17

    How to get coordinates of touch screen points in ogl?

  18. 18

    Get the coordinates at the edge of the screen from a given angle

  19. 19

    JavaFX 8 Dynamic Node scaling

  20. 20

    How can I get a component's position on screen (screen coordinates)

  21. 21

    Get real position of a node in JavaFX

  22. 22

    JavaFX: Get Node by row and column

  23. 23

    How to get current screen details in javafx?

  24. 24

    Get screen size on iOS 8

  25. 25

    Can't get mouse event from any other javafx 8 node after getting MOUSE_PRESSED event from one node

  26. 26

    Tool to easily select a pixel on screen and get color and absolute coordinates

  27. 27

    QML-maps: get coordinates when tap the screen

  28. 28

    Tool to easily select a pixel on screen and get color and absolute coordinates

  29. 29

    How to get screen coordinates of view inside of keyboard accessory view?

HotTag

Archive