Explain Codes LogoExplain Codes Logo

How can I get screen resolution in java?

java
interface-design
multiple-screens
graphics-environment
Nikita BarsukovbyNikita Barsukov·Dec 14, 2024
TLDR

Use Java's Toolkit and Dimension classes to retrieve screen resolution swiftly:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); System.out.printf("Screen resolution: %dx%d\n", screenSize.width, screenSize.height);

This snippet provides the current screen's width and height in pixels.

Tackling multiple screens

For instances where your setup involves multiple screen displays, turn to GraphicsEnvironment and GraphicsDevice:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] screens = ge.getScreenDevices(); for (GraphicsDevice screen : screens) { DisplayMode mode = screen.getDisplayMode(); // Specifications of each screen, because every pixel matters System.out.printf("Resolution of %s: %dx%d\n", screen.getIDstring(), mode.getWidth(), mode.getHeight()); }

Here, each screen's individual resolution is reported, enabling you to account for all display setups.

Understanding DPI metrics

When your pursuit is the number of dots per inch (DPI) of the display, pivotal to size calibrations of graphic objects and text, take Toolkit.getScreenResolution() out for a spin:

int screenDPI = Toolkit.getDefaultToolkit().getScreenResolution(); // Because blurry text and images are not fun! System.out.println("Screen DPI: " + screenDPI);

These are nuggets of information critical to interface design and applications requiring precise visual elements reproduction.

Deploying GraphicsConfiguration

Let's talk about the valuable tool that is GraphicsConfiguration. This class supplies you with the screen bounds:

GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); GraphicsConfiguration config = gd.getDefaultConfiguration(); Rectangle bounds = config.getBounds(); // Your screen's 'latitude' and 'longitude' in the digital world System.out.println("Screen bounds: " + bounds);

Thus, you tap into not only the device's resolution but its position relative to the entire virtual desktop, a need for multiple screens applications.

Tackling font scaling on Windows

Due to font scaling settings on Windows which users might engage for high-DPI displays, getScreenSize() could sometimes return scaled values. To avoid this potential conflict, resort to getBounds() and getDisplayMode() for true resolution:

GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] devices = env.getScreenDevices(); for (GraphicsDevice device : devices) { GraphicsConfiguration[] configurations = device.getConfigurations(); for (GraphicsConfiguration config : configurations) { Rectangle gcBounds = config.getBounds(); // No more tall tales from Windows, just pure pixel truth! System.out.printf("Unscaled resolution: %dx%d\n", gcBounds.width, gcBounds.height); } }

Maximising across monitors

To ascertain the total usable space (yes, all of it) spanning your monitors, think about totals and maximums:

int totalWidth = Stream.of(devices) .mapToInt(device -> device.getDefaultConfiguration().getBounds().width) .sum(); int totalHeight = Stream.of(devices) .mapToInt(device -> device.getDefaultConfiguration().getBounds().height) .max() .orElseThrow(() -> new RuntimeException("No screens found, this is a developer's nightmare!")); // More space than Elon Musk could ask for! System.out.printf("Total usable space: %dx%d\n", totalWidth, totalHeight);

We've now calculated the overhead width and peak height, covering pretty much everything you need to know about designing applications for multiple displays setups.