How can I get screen resolution in java?

AndreDurao picture AndreDurao · Sep 9, 2010 · Viewed 229.6k times · Source

How can one get the screen resolution (width x height) in pixels?

I am using a JFrame and the java swing methods.

Answer

Colin Hebert picture Colin Hebert · Sep 9, 2010

You can get the screen size with the Toolkit.getScreenSize() method.

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();

On a multi-monitor configuration you should use this :

GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();

If you want to get the screen resolution in DPI you'll have to use the getScreenResolution() method on Toolkit.


Resources :