I came accross this thread when trying to implement a way to capture all screens on any given computer in a single screenshot, and could not find an answer as to how to accomplish this anywhere else.
In my case, Toolkit.getDefaultToolkit().getScreenSize()
would only return the resolution of the primary monitor. I made the following method to create a Rectangle
that has an origin at the top-leftmost point between all monitors, and is large enough to capture all the content on all the monitors supplied by GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()
when passed to Robot().createScreenCapture
.
private Rectangle allScreensRectangle()
{
// make array of all screens
GraphicsDevice[] screens = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
// start out with the rectangle of the primary monitor
Rectangle allScreens = screens[0].getDefaultConfiguration().getBounds();
for (int i = 1; i < screens.length; i++)
{
Rectangle screenRect = screens[i].getDefaultConfiguration().getBounds();
// expand the size of the capture region to include any area in the x/y dimension that is outside the already-defined rectangle
allScreens.width += (screenRect.width - (screenRect.width - Math.abs(screenRect.x)));
allScreens.height += (screenRect.height - (screenRect.height - Math.abs(screenRect.y)));
// ensure the origin point is always at the top-leftmost point of all monitors.
allScreens.x = Math.min(allScreens.x, screenRect.x);
allScreens.y = Math.min(allScreens.y, screenRect.y);
}
return allScreens;
}