How to get multi-monitor screen dimensions in Java
There is a nice example which seems to do exactly what I want - it’s even on the official documentation on GraphicsConfiguration.
But: If the left-most monitor is not the primary one - you should get negative X-coordinates - and that’s where the above example fails :(
After some trying I finally got this solution to it:
private Rectangle getVirtualScreenDimension() {
Rectangle virtualBounds = new Rectangle();
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
for (int j = 0; j < gs.length; j++) {
GraphicsDevice gd = gs[j];
GraphicsConfiguration[] gc = gd.getConfigurations();
for (int i=0; i < gc.length; i++) {
virtualBounds = virtualBounds.union(
gc[i].getBounds());
}
}
Rectangle defaultBounds = ge.getDefaultScreenDevice()
.getDefaultConfiguration().getBounds();
virtualBounds.x -= defaultBounds.x;
virtualBounds.y -= defaultBounds.y;
return virtualBounds;
}