How can I get screen resolution in java?

Viewed 253983

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

I am using a JFrame and the java swing methods.

12 Answers

Here is a snippet of code I often use. It returns the full available screen area (even on multi-monitor setups) while retaining the native monitor positions.

public static Rectangle getMaximumScreenBounds() {
    int minx=0, miny=0, maxx=0, maxy=0;
    GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    for(GraphicsDevice device : environment.getScreenDevices()){
        Rectangle bounds = device.getDefaultConfiguration().getBounds();
        minx = Math.min(minx, bounds.x);
        miny = Math.min(miny, bounds.y);
        maxx = Math.max(maxx,  bounds.x+bounds.width);
        maxy = Math.max(maxy, bounds.y+bounds.height);
    }
    return new Rectangle(minx, miny, maxx-minx, maxy-miny);
}

On a computer with two full-HD monitors, where the left one is set as the main monitor (in Windows settings), the function returns

java.awt.Rectangle[x=0,y=0,width=3840,height=1080]

On the same setup, but with the right monitor set as the main monitor, the function returns

java.awt.Rectangle[x=-1920,y=0,width=3840,height=1080]

There's many answers but I still feel they're not adequate enough, my approach computes global variables related to the screen size once and also using a single loop of all the monitors:

public final class ScreenArea {
    public static final Rectangle RECTANGLE;
    public static final int 
        LEFT, RIGHT, 
        TOP, BOTTOM, 
        MIN_WIDTH, MAX_WIDTH, 
        MIN_HEIGHT, MAX_HEIGHT, 
        TOTAL_WIDTH, TOTAL_HEIGHT;
    
    static {
        // Initialise local vars
        int left, right, top, bottom, minWidth, maxWidth, minHeight, maxHeight;
        left = top = minWidth = minHeight = Integer.MAX_VALUE;
        right = bottom = maxWidth = maxHeight = Integer.MIN_VALUE;
        // In a single loop process all bounds
        Rectangle bounds;
        for (GraphicsDevice device : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
            bounds = device.getDefaultConfiguration().getBounds();
            if (left > bounds.x)
                left = bounds.x;
            if (right < bounds.x + bounds.width)
                right = bounds.x + bounds.width;
            if (top > bounds.y)
                top = bounds.y;
            if (bottom < bounds.y + bounds.height)
                bottom = bounds.y + bounds.height;
            if (minWidth > bounds.width)
                minWidth = bounds.width;
            if (maxWidth < bounds.width)
                maxWidth = bounds.width;
            if (minHeight > bounds.height)
                minHeight = bounds.height;
            if (maxHeight < bounds.height)
                maxHeight = bounds.height;
        }
        TOTAL_WIDTH = right - left;
        TOTAL_HEIGHT = bottom - top;
        RECTANGLE = new Rectangle(TOTAL_WIDTH, TOTAL_HEIGHT);
        // Transfer local to immutable global vars
        LEFT = left; RIGHT = right; TOP = top; BOTTOM = bottom;
        MIN_WIDTH = minWidth; MAX_WIDTH = maxWidth;
        MIN_HEIGHT = minHeight; MAX_HEIGHT = maxHeight;
    }
}

Then you can use anytime as is just like this :

System.out.printf("LEFT=%d, ", ScreenArea.LEFT);
System.out.printf("RIGHT=%d%n", ScreenArea.RIGHT);
System.out.printf("TOP=%d, ", ScreenArea.TOP);
System.out.printf("BOTTOM=%d%n", ScreenArea.BOTTOM);
System.out.printf("MIN_WIDTH=%d, ", ScreenArea.MIN_WIDTH);
System.out.printf("MAX_WIDTH=%d%n", ScreenArea.MAX_WIDTH);
System.out.printf("MIN_HEIGHT=%d, ", ScreenArea.MIN_HEIGHT);
System.out.printf("MAX_HEIGHT=%d%n", ScreenArea.MAX_HEIGHT);
System.out.printf("SCREEN_AREA=%s%n", ScreenArea.RECTANGLE);

Which on my dual monitor setup it prints :

LEFT=0, RIGHT=3840
TOP=0, BOTTOM=1080
MIN_WIDTH=1920, MAX_WIDTH=1920
MIN_HEIGHT=1080, MAX_HEIGHT=1080
SCREEN_AREA=java.awt.Rectangle[x=0,y=0,width=3840,height=1080]

Unfortunately Toolkit.getDefaultToolkit() does not help if you have multiple displays, and on Windows it also reports scaled values if you have changed font setting "Scale and Layout" from 100%. For example at 150% font scale my 1920x1080 screen is reported as 1280x720 which (unhelpfully) changes the resolution my app uses.

Instead I use this method which reads the default display modes of each GraphicsDevice to access the original screen position+dimensions, and returns set of rectangles sorted in left->right X position order per screen:

/** Get actual screen display sizes, ignores Windows font scaling, sort left to right */
public static List<Rectangle> getDisplays() {
  return Arrays.stream(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices())
     .map(GraphicsDevice::getDefaultConfiguration)
     // For scaled sizes use .map(GraphicsConfiguration::getBounds) instead of:
     .map(c -> {
            var dm = c.getDevice().getDisplayMode();
            var bounds = c.getBounds();
            return new Rectangle((int)bounds.getX(), (int)bounds.getY(), dm.getWidth(), dm.getHeight());
      })
     .sorted(Comparator.comparing(Rectangle::getX))
     .toList();
}

The above code runs under Windows and WSL.

Related