Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 trying to make a maximize button. On the default monitor it works perfectly, but on a second monitor if I click the maximize button it will maximize to the default screen. How would I get the X & Y coordinates of the screen the application is currently on?

I.E. I have 2 monitors both at 1600x900, so if the application is on monitor 1, the X & Y would be 0 & 0, but if it is one the second monitor it would be 1600 & 0.

But I need it so it works on all sized monitors i.e. 1200x800, or if the monitors are positioned vertical instead of horizontal.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
251 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...