【问题标题】:Map physical screen position to an array of GraphicsDevice with Swing使用 Swing 将物理屏幕位置映射到 GraphicsDevice 数组
【发布时间】:2013-09-27 10:16:52
【问题描述】:

在 Windows 中,每个屏幕都分配了一个编号或标识,我认为这与我物理连接显示器电缆的方式有关。我的问题的关键是我可以重新配置这些屏幕,但它们会保留自己的身份。

Java 对GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices() 的调用将给我一个GraphicsDevice 数组。

如何将阵列中的屏幕顺序与窗口编号相关联?我会对跨平台解决方案感兴趣。

例如,我的 Windows 配置如下所示

但是返回的数组是这样的

screens[0] = relates to screen 2
screens[1] = relates to screen 3
screens[2] = relates to screen 1

NB我想使用的代码是这样的

frame.setLocation(
     screens[i].getDefaultConfiguration().getBounds().x, frame.getY());

i 应该是物理编号,而不是数组中的位置(或者它的映射,如果你明白我的意思的话)。

【问题讨论】:

    标签: java swing


    【解决方案1】:

    您可以按位置对屏幕设备进行排序:

    Arrays.sort(screens, new Comparator<GraphicsDevice>() {
        public int compare(GraphicsDevice screen1,
                           GraphicsDevice screen2) {
            Rectangle bounds1 = screen1.getDefaultConfiguration().getBounds();
            Rectangle bounds2 = screen2.getDefaultConfiguration().getBounds();
            int c = bounds1.y - bounds2.y;
            if (c == 0) {
                c = bounds1.x - bounds2.x;
            }
            return c;
        }
    });
    

    【讨论】:

    • 需要注意的是,如果你改变从GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()返回的数组的顺序,你的主屏幕也会改变(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() )。所以最好将返回的数组复制一份,以免丢失主设备。
    • 哇。这是一个严重的错误,而且让我感到非常惊讶,因为 Java 通常非常擅长使用 defensive copying。与臭名昭著的Class.getSigners() security bug相比。
    【解决方案2】:

    无法保证 getScreenDevices() 返回的 GraphicsDevice 顺序与您在操作系统设置中看到的相同。另外我不知道有什么方便的方法来检索系统屏幕顺序(我怀疑是否有,你肯定必须调用一些本机操作系统 API 才能获得确切的顺序)。

    您可以使用有关 GraphicsDevice 的可用信息在您的应用程序中订购它们:

    • 默认设备 - GraphicsEnvironment.getLocalGraphicsEnvironment ().getDefaultScreenDevice ()

    • 设备边界 - graphicsDevice.getDefaultConfiguration ().getBounds ()

    设备 ID 也可能会告诉您一些信息 - graphicsDevice.getIDstring () - 虽然我现在无法检查它是否受到系统屏幕顺序的影响。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      • 2020-10-07
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多