I want to make an app that behaves differently for different monitors (e.g. changing antialias techniques for LCD, CRT, pentile matrix, etc.). Is there any way to discover which monitor a given window is on? I only need this as an integer.
Yes, it's possible. I'll simplify my answer into grabbing the monitor based on the top x,y position of the Window
(so this would fail of course if you had top half of the frame off any monitor, in general this code isn't robust but an example to get you started).
public GraphicsDevice getMonitorWindowIsOn(Window window)
{
// First, grab the x,y position of the Window object
GraphicsConfiguration windowGraphicsConfig = window.getGraphicsConfiguration();
if (windowGraphicsConfig == null)
{
return null; // Should probably be handled better
}
Rectangle windowBounds = windowGraphicsConfig.getBounds();
for (GraphicsDevice gd : ge.getScreenDevices()) {
Rectangle monitorBounds = gd.getDefaultConfiguration().getBounds();
if (monitorBounds.contains(windowBounds.x, windowBounds.y))
{
return gd;
}
}
// um, return null I guess, should make this default better though,
// maybe to the default screen device, except, I am sure if no monitors are
// connected that may be null as well.
return null;
}
Regarding the initial call of window.getGraphicsConfiguration()
, it can return null:
Gets the GraphicsConfiguration associated with this Component. If the Component has not been assigned a specific GraphicsConfiguration, the GraphicsConfiguration of the Component object's top-level container is returned. If the Component has been created, but not yet added to a Container, this method returns null.