Detect current screen bounds

后端 未结 2 1013
忘了有多久
忘了有多久 2020-12-11 22:20

I am working on an application that has setDecoration(false) and I have a MouseMotionlistener so I can move it around, and at the moment I am tryin

相关标签:
2条回答
  • 2020-12-11 22:56

    The answer depends on the definition of screen. Do you want the default screen bounds or a specific screen bounds?

    I use the following (& variations of it) to determine the screen bounds for an individual screen

    public static GraphicsDevice getGraphicsDeviceAt(Point pos) {
        GraphicsDevice device = null;
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice lstGDs[] = ge.getScreenDevices();
        ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length);
    
        for (GraphicsDevice gd : lstGDs) {
            GraphicsConfiguration gc = gd.getDefaultConfiguration();
            Rectangle screenBounds = gc.getBounds();
            if (screenBounds.contains(pos)) {
                lstDevices.add(gd);
            }
        }
    
        if (lstDevices.size() == 1) {
            device = lstDevices.get(0);
        }
        return device;
    }
    
    public static Rectangle getScreenBoundsAt(Point pos) {
        GraphicsDevice gd = getGraphicsDeviceAt(pos);
        Rectangle bounds = null;
    
        if (gd != null) {
            bounds = gd.getDefaultConfiguration().getBounds();
        }
        return bounds;
    }
    

    The basic idea is to provide a screen location and find the screen that matches it. I have variations the take a Component or Window but essentially, it boils down to this.

    From a MouseEvent, you can obtain the screen coordinates simply enough with a call to MouseEvent.getLocationOnScreen

    Now, from your question, it sounds like you want to know the entire "virtual" screen bounds (I might be wrong), but I use this method (actually I use it to create multi-monitor wallpapers on the fly, but that's another question)

    public static Rectangle getVirtualScreenBounds() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice lstGDs[] = ge.getScreenDevices();
    
        Rectangle bounds = new Rectangle();
        for (GraphicsDevice gd : lstGDs) {
            bounds.add(gd.getDefaultConfiguration().getBounds());
        }
        return bounds;
    }
    

    Basically, it just walks all the screen devices and add's there regions together to form a "virtual" rectangle. You could use the same concept to return the bounds of each screen device as an array instead.

    0 讨论(0)
  • 2020-12-11 23:05

    You could use this to get the screen dimensions.

    Toolkit toolkit =  Toolkit.getDefaultToolkit ();
    Dimension dim = toolkit.getScreenSize();
    System.out.println("Width of Screen Size is "+dim.width+" pixels");
    System.out.println("Height of Screen Size is "+dim.height+" pixels");
    
    0 讨论(0)
提交回复
热议问题