You can now render on
multiple screens by creating Frame, JFrame,
Window, or JWindow objects with the
GraphicsConfiguration of the target
GraphicsDevice.
The new Frame constructor,
Frame(GraphicsConfiguration),
enables creation of a Frame object on a different
screen device.
The new Window constructor,
Window(Window, GraphicsConfiguration),
constructs a new invisible window with the specified window as its
owner and the GraphicsConfiguration of a screen device.
The new GraphicsConfiguration
getBounds method
returns the bounds of the GraphicsConfiguration
in device coordinates. If you have a virtual device, then the
device coordinates returned from getBounds are
virtual device coordinates.
Finally, the new Component
getGraphicsConfiguration method
returns the GraphicsConfiguration with which the
Component was created.
In a virtual device configuration consisting of more than
one physical screen device, the GraphicsConfiguration
objects' coordinates are relative to the virtual coordinate
system. For this reason, virtual coordinates must be used when
calling the setLocation method of a Frame or
Window. Similarly, calling getBounds
of a GraphicsConfiguration in a virtual device
environment returns virtual device coordinates.
The following code sample creates a
JFrame object for each GraphicsConfiguration
on each screen device in the GraphicsEnvironment.
It offsets the coordinates of the intended location of
the JFrame with the bounds of
the GraphicsConfiguration to ensure that the
JFrame appears on the screen of the specified
GraphicsConfiguration.
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++) {
JFrame f = new JFrame(gs[j].getDefaultConfiguration());
Canvas c = new Canvas(gc[i]);
Rectangle gcBounds = gc[i].getBounds();
int xoffs = gcBounds.x;
int yoffs = gcBounds.y;
f.getContentPane().add(c);
f.setSize(300, 150);
f.setLocation((i*50)+xoffs, (i*60)+yoffs);
f.show();
}
}
If the bounds of a GraphicsConfiguration is not
taken into account in this sample, a JFrame
would appear at location (i*50, i*60) on the primary screen,
which might be different than the screen of the specified
GraphicsConfiguration.
For more information on how the Java 2D API supports
multi-screen environments, see
Rendering in a Multi-Screen Environment in the
Programmer's Guide to the
Java 2D API.