java - 有什么方法可以检测窗口在哪个监视器上吗?

标签 java antialiasing multiple-monitors

<分区>

我想制作一个针对不同显示器表现不同的应用程序(例如,更改 LCD、CRT、pentile 矩阵等的抗锯齿技术)。 有什么方法可以发现给定窗口在哪个监视器上吗?我只需要它作为一个整数。

最佳答案

是的,这是可能的。我会将我的答案简化为根据 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 :

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.

关于java - 有什么方法可以检测窗口在哪个监视器上吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28596967/

相关文章:

java - JPA如何使用级联删除删除作为onetoone目标的实体

java - 如果检测到不适当的依赖项,Gradle 构建会失败?

ios - UIImage 的 drawInrect : smoothes image

python - 如何平滑 pyqt4 上曲线和对角线上的锯齿状边缘?

C#有选择地关闭监视器

java - 选择在哪个监视器中打开 JavaFX 窗口

java - Ant 构建中参数不完整的异常处理

java - 哪个引用变量为空 - 尝试从空对象引用的字段 'android.os.Handler android.support.v4.a.m.a' 读取

opengl-es - SSAO 深度后处理期间的抗锯齿

delphi - 在第二台显示器上启动程序?