0

I need a function that gives me the resolutions of my individual monitors.

I do know that Toolkit.getDefaultToolkit().getScreenSize() gives me the cumulative resolution of all monitors, but to limit the size of some frames I draw I want to know the resolutions of the individual screens.

I tried using GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices() which returns an array of all screens, but I could not find any resolution information in there.

The header of the function should be something like

/**
 * Returns the Dimension of all available screen devices in order of appearance in  
 * GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()
 * 
 * @return the Dimensions of all available screen devices.
 */
public Dimension[] getScreenResolutions() {
    [ . . . ]
}
Nico Wawrzyniak
  • 317
  • 1
  • 3
  • 16
  • Are you sure you need this? The most common reasons for wanting the screen bounds are usually cases that are properly handled by other methods. To center a window, use setLocationRelativeTo(null). To fill a screen, use Frame.setExtendedState(Frame.MAXIMIZED_BOTH) or GraphicsDevice.setFullScreenWindow. – VGR Jul 04 '22 at 22:48
  • First I need to read in an image file, scale it down if it is too big and calculate the size of the frames. Window state is supposed to be non-maximized (the frame fills ~80% of the screen), so for this specific case it now works as intended. – Nico Wawrzyniak Jul 05 '22 at 14:58
  • Centering is done later, when the frame size is already calculated. – Nico Wawrzyniak Jul 05 '22 at 20:16

2 Answers2

1

You need to use GraphicsConfiguration. The documentation shows how to get the screen bounds.

Rob Spoor
  • 6,186
  • 1
  • 19
  • 20
1

I just found out the same, while Rob Spoor answered, so I'm gonna give a full answer to my question:

GraphicsDevice has a function getDefaultConfiguration() which returns a GraphicsConfiguration which has a getBounds() method which again returns the values as a Rectangle.

An implementation of my function header would therefore be:

/**
 * Returns the Dimension of all available screen devices in order of appearance in  
 * GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()
 * 
 * @return the Dimensions of all available screen devices.
 */
public Dimension[] getScreenResolutions() {
    // step 1: get amount of screens and allocate space
    int deviceAmount = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices().length;
    Dimension[] dimensions = new Dimension[deviceAmount];
    Rectangle[] rectangles = new Rectangle[deviceAmount];
    // step 2: get values as Rectangle[]
    for (int i = 0; i < deviceAmount; i++) { 
        rectangles[i] = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[i].getDefaultConfiguration().getBounds();
    }
    // step 3: convert to Dimension[]
    for (int i = 0; i < rectangles.length; i++) {
        dimensions[i] = new Dimension(r.getWidth(), r.getHeight());
    }
    // step 4: return
    return dimensions;
}
Nico Wawrzyniak
  • 317
  • 1
  • 3
  • 16
  • You can use a stream for that ;): `Arrays.stream(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()).map(GraphicsDevice::getDefaultConfiguration).map(GraphicsConfiguration::getBounds).map(Rectangle::getSize).toArray(Dimension[]::new)` – Rob Spoor Jul 04 '22 at 19:13
  • See also this if you use [font scaling on Windows](https://stackoverflow.com/questions/3680221/how-can-i-get-screen-resolution-in-java/70294946#70294946) – DuncG Jul 04 '22 at 19:14