我想制作一款适用于不同显示器的应用程序(例如,更改LCD,CRT,pentile矩阵等的抗锯齿技术)。 有没有办法发现给定窗口所在的监视器?我只需要这个整数。
答案 0 :(得分:3)
是的,这是可能的。我会简化我的答案,根据Window
的顶部x,y位置抓住显示器(所以如果你的任何一个显示器上方有一半的帧,那么这会失败,一般来说这个代码不是''很健壮,但是一个让你入门的例子。)
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;
}
关于window.getGraphicsConfiguration()
的初始通话,it can return null:
获取与此Component关联的GraphicsConfiguration。如果尚未为Component分配特定的GraphicsConfiguration,则返回Component对象的顶级容器的GraphicsConfiguration。如果已创建Component但尚未添加到Container,则此方法返回null。