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.
Asked
Active
Viewed 305 times
3
-
2I'm sure this is going to vary greatly depending on what platform you're talking about. Windows? Linux? Mac? – Falmarri Feb 19 '15 at 01:02
-
I'm not sure that there's a good way to do that - even if there is a way to get the model from the system, you'd probably have to find some library that can tell you, based on that string, what type of device it is. That said, is there a reason you can't have the user select their monitor type from a menu? I know it's annoying, but if you're going to the trouble to optimize for different types, it might not be too much to ask. – childofsoong Feb 19 '15 at 01:03
-
Sorry, I should have specified. I'm making a preference that a user can set for which antialiasing technique a user pieces on each individual monitor. – Ky - Feb 19 '15 at 01:18
1 Answers
3
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.

NESPowerGlove
- 5,496
- 17
- 28