0

I am trying to fetch the coordinates of the top left position in a multimonitor setup for my screen capture program. Here is what I've currently got for the program:

        int dwidth = 0, dheight = 0; //max dimensions of all monitors
    for (GraphicsDevice gd : GraphicsEnvironment
            .getLocalGraphicsEnvironment().getScreenDevices()) {
        if (gd == GraphicsEnvironment.getLocalGraphicsEnvironment()
                .getDefaultScreenDevice()) {
            minx = -width; //My attempt at finding the minimum X value did not work
        }
        dwidth += gd.getDisplayMode().getWidth();
        dheight += gd.getDisplayMode().getHeight();
    }

Basically I want to be able to run my program across all of the monitors. For full code view here: https://gist.github.com/fletchto99/5788659

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Matt Langlois
  • 305
  • 3
  • 17
  • somebody told me that swing can't handle multiple monitors. – Jens Schauder Jun 15 '13 at 19:08
  • @JensSchauder My program works in my dual screen setup, but not my friends triple screen setup. – Matt Langlois Jun 15 '13 at 19:47
  • 1
    Take a look at [this answer](http://stackoverflow.com/questions/11714215/detect-current-screen-bounds/11714302#11714302). It discusses how to to calculate the virtual bounds of the desktop, but it does so but listing concaternating the screen bounds of each screen – MadProgrammer Jun 15 '13 at 22:05
  • @MattLanglois Don't forget, an up vote is always appreciated ;) – MadProgrammer Jun 16 '13 at 03:43
  • @MadProgrammer Is there a way I can make the comment my accepted answer? -- I don't use this site too often. If you want to answer it with the same thing, I can accept that so others can see. – Matt Langlois Jun 16 '13 at 04:16
  • @MattLanglois I create an answer which more directly answers your question – MadProgrammer Jun 16 '13 at 05:23

2 Answers2

1

In Java, the coordinate system in the graphic libraries starts at the top-left part of the screen. So your code should always be equivalent to simply stating: 0, 0.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • This bring up the top left corner for the main monitor, I'm trying to put the window to the top left corner of the top left monitor. I've already tried this, but if the left monitor isn't the main one then the coordinates can be negative. For example this setup here: http://gyazo.com/82bded44baa10fd347c93fa7804f6347.png – Matt Langlois Jun 15 '13 at 19:01
  • Not true. The screens can appear at < 0 and are based on the position relative to the default/master screen – MadProgrammer Jun 15 '13 at 22:02
1

You can get a list of GraphicDevices from the GraphicsEnvironment. Each GraphicsDevice would represent a screen.

Now, you have a number of choices...

You could simply look through the list of GraphicsDevices and simply find the one with the lowest x/y coordinates or you could combine them all into a "virtual screen" and simply extract the top/left coordinates from it.

For example...

public static Rectangle getVirtualScreenBounds() {

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice lstGDs[] = ge.getScreenDevices();

    Rectangle bounds = new Rectangle();
    for (GraphicsDevice gd : lstGDs) {

        bounds.add(gd.getDefaultConfiguration().getBounds());

    }

    return bounds;

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366