0

I seem to struggle to get the size of more than one monitor in Java;

So I made a little window that is supposed to display a screen's dimensions, it works fine for my primary monitor, now I would like to be able to determine the size of the monitor it's on, I used getLocation() to know where my JFrame is, but I don't know how to get the size of that monitor, i can only get the primary one, or even their total size.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
fen
  • 73
  • 6
  • Please get into the habit of searching Stack Overflow for existing answers before posting questions. There's a very good chance that what you're asking has been asked before. – MarsAtomic Apr 27 '15 at 23:10

1 Answers1

2

You need to get down into the GraphicsEnvironment which will give you access to all GraphicsDevice available on the system.

Essentially from there, you need loop through each GraphicsDevice and test to see if the window is within the bounds of the a given GraphicsDevice

The fun part, is what to do if the window spans across multiple screens...

public static GraphicsDevice getGraphicsDevice(Component comp) {

    GraphicsDevice device = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice lstGDs[] = ge.getScreenDevices();
    ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length);

    if (comp != null && comp.isVisible()) {
        Rectangle parentBounds = comp.getBounds();
        /*
         * If the component is not a window, we need to find its location on the
         * screen...
         */
        if (!(comp instanceof Window)) {
            Point p = new Point(0, 0);
            SwingUtilities.convertPointToScreen(p, comp);
            parentBounds.setLocation(p);
        }

        // Get all the devices which the window intersects (ie the window might expand across multiple screens)
        for (GraphicsDevice gd : lstGDs) {
            GraphicsConfiguration gc = gd.getDefaultConfiguration();
            Rectangle screenBounds = gc.getBounds();
            if (screenBounds.intersects(parentBounds)) {
                lstDevices.add(gd);
            }
        }

        // If there is only one device listed, return it...
        // Otherwise, if there is more then one device, find the device
        // which the window is "mostly" on
        if (lstDevices.size() == 1) {
            device = lstDevices.get(0);
        } else if (lstDevices.size() > 1) {
            GraphicsDevice gdMost = null;
            float maxArea = 0;
            for (GraphicsDevice gd : lstDevices) {
                int width = 0;
                int height = 0;
                GraphicsConfiguration gc = gd.getDefaultConfiguration();
                Rectangle bounds = gc.getBounds();
                Rectangle2D intBounds = bounds.createIntersection(parentBounds);
                float perArea = (float) ((intBounds.getWidth() * intBounds.getHeight()) / (parentBounds.width * parentBounds.height));
                if (perArea > maxArea) {
                    maxArea = perArea;
                    gdMost = gd;
                }
            }

            if (gdMost != null) {
                device = gdMost;
            }
        }
    }

    return device;
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366