How can I get the width and height of the screen with java?

4

I'm trying to take a screenshot with Robot() and doing some research I found that I could do it this way

robotAwt.createScreenCapture

The problem is that the createScreenCapture method receives a rectangle so I figured I could do something like that robotAwt.createScreenCapture(new Rectangle(y, x))

rectangle receives 2 int the height and width if what I need is to take the full screen capture how do I get these sizes? that is, how I get the height and width of the screen.

    
asked by JHon Dickertson 04.10.2017 в 18:04
source

2 answers

3

To know the resolution of the screen with Java you can support the AWT framework . The class that represents the AWT framework in general is Toolikt . Toolikt is an abstraction and allows you to hook with native implementations of the framework.

The first thing is to instantiate the framework:

Toolkit t = Toolkit.getDefaultToolkit();

Once the framework is instantiated, use the Toolikt method to know the resolution of the screen. Specifically, use the .getScreenSize () method. This method will return a Dimensions class, which serves to accommodate the dimensions of any AWT component.

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

Only the height and width of Dimensions properties are now available to see the resolution of the screen.

System.out.println("Tu resolución es de " + screenSize.width + "x" + screenSize.height);

or

int ancho = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
int alto = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
    
answered by 04.10.2017 / 18:23
source
1

You can try this:

Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
double h= screenSize.getHeight();
double w= screenSize.getWidth();

But you do not need to do those steps, you can do this directly:

Rectangle a = new Rectangle(screenSize);
    
answered by 04.10.2017 в 18:22