Determine windows display number and/or layout via java

百般思念 提交于 2019-11-28 05:28:13

问题


I have a fullscreen java app that will run on an 8 monitor digital signage type display on a Windows 7 machine. I need to be able to display content on specific physical monitors. Ideally I would like the displays ordered 1-8 in Display Properties -> Settings, however many attempts of unplugging/plugging in and reordering have failed to get the physical monitors to appear in any deterministic order via the Display Properties->Settings. I can reorder them fine, but when my java program retrieves information on the displays it is not in the layout/order that windows has them configured.

GraphicsEnvironment ID returns Strings such as Device0 and Device1, but these do not match the Windows Display numbering as seen in the Display properties. For instance if the layout is 7,4,1,2,3,4,5,6 I still get back Device0, Device1... in which Device0 corresponds to identified screen 1 (not 7 which is the first screen on the left). Is there a way to query the OS to determine what layout the displays are in and/or some other technique to display fullscreen on a specific physical monitor?


回答1:


You can get the bounds of the screens relative to the big virtual desktop:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
for (GraphicsDevice gd : ge.getScreenDevices()) {
    Rectangle bounds = gd.getDefaultConfiguration().getBounds();
    System.out.println(bounds.toString());
}

for my setup this gives:

java.awt.Rectangle[x=0,y=0,width=1280,height=1024]
java.awt.Rectangle[x=1280,y=304,width=1280,height=720]

You can use this information to determine their order. E.g. if you are absolutely sure that your monitors are in a nice grid you can go fullscreen on the upper right monitor like this:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gdUpperRight = null;
Rectangle bUpperRight = null;
for (GraphicsDevice gd : ge.getScreenDevices()) {
    Rectangle b = gd.getDefaultConfiguration().getBounds();
    if (bUpperRight == null || b.x > bUpperRight.x || b.y > bUpperRight.y) {
        bUpperRight = b;
        gdUpperRight = gd;
    }
}

gdUpperRight.setFullScreenWindow(myFrame);


来源:https://stackoverflow.com/questions/10888131/determine-windows-display-number-and-or-layout-via-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!