0

我有一个桌面 java 应用程序(java 1.4.2),它需要确定有关 linux 环境中两个屏幕的信息:

# cat /etc/redhat-release
Red Hat Enterprise Linux WS release 4 (Nahant Update 7)
# lsb_release
cat /proc/versionLSB Version:   
:core-3.0-ia32:core-3.0-noarch:graphics-3.0-ia32:graphics-3.0-noarch
# cat /proc/version
Linux version 2.6.9-78.ELsmp (brewbuilder@hs20-bc2-3.build.redhat.com) 
(gcc version 3.4.6 20060404 (Red Hat 3.4.6-10)) #1 SMP Wed Jul 9 15:39:47 EDT 2008

屏幕为 2048x2048 和 1600x1200。

代码是

GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] allScreens = env.getScreenDevices();
log("=============================================");
log("Total num. of screen = " + allScreens.length);
for (int i = 0; i < allScreens.length; i++) {
    log("--------------------------------------");

    log(
        allScreens[i].getIDstring() + " width: " + allScreens[i].getDisplayMode().getWidth() + 
        " - height: " + allScreens[i].getDisplayMode().getHeight());

    GraphicsConfiguration dgc =
        allScreens[i].getDefaultConfiguration();
    Rectangle bounds = dgc.getBounds();
    Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(dgc);
    log("Bounds: " + bounds);
    log("Insets: " + insets);

    log("--------------------------------------");
}
log("=============================================");

但输出是

=============================================
Total num. of screen = 2
--------------------------------------
:0.0 width: 2048 - height: 2048
Bounds: java.awt.Rectangle[x=0,y=0,width=2048,height=2048]
Insets: java.awt.Insets[top=0,left=0,bottom=0,right=0]
--------------------------------------
--------------------------------------
:0.1 width: 2048 - height: 2048
Bounds: java.awt.Rectangle[x=0,y=0,width=1600,height=1200]
Insets: java.awt.Insets[top=0,left=0,bottom=0,right=0]
--------------------------------------
=============================================

屏幕:0.1 使用 allScreens[i].getDisplayMode() 时为 2048x2048,使用 getDefaultConfiguration().getBounds() 时为 1600x1200:

为什么我有不同的结果?

getDisplayMode() 的 API 代码是

public DisplayMode getDisplayMode() {
    GraphicsConfiguration gc = getDefaultConfiguration();
    Rectangle r = gc.getBounds();
    ColorModel cm = gc.getColorModel();
    return new DisplayMode(r.width, r.height, cm.getPixelSize(), 0);
}

所以值应该是相同的:为什么不同?

谢谢

4

1 回答 1

0

这也是我在自己的应用程序中注意到的,它需要前端 gui 以适应多显示器环境中的不同显示器。问题与显卡有关,如果您使用 allScreens[i].getDisplayMode(),英特尔显卡将为您提供不同显示器的相同宽度和高度,基本上是主显示器,而 NVidia 和 ATI(AMD)那些使用相同的功能为您提供与每台显示器相对应的实际分辨率值。

因此,无论显卡如何,在多显示器环境中为每个显示器获得正确分辨率的正确方法是使用 getDefaultConfiguration().getBounds().width 或 height。

希望能帮助到你。

于 2014-01-23T15:46:03.910 回答