tags:

views:

232

answers:

3

When my program starts, the main window places itself where it was when it was last closed. I want to modify this behavior some so if the window is off-screen (or partially off-screen) it moves itself to fully on screen.

I've got this working perfectly. Here's the code:

 int x = gameConfig.windowX;
 int y = gameConfig.windowY;
 int width = gameConfig.windowWidth;
 int height = gameConfig.windowHeight;

 if( x < 0 ) x = 0;
 if( y < 0 ) y = 0;

 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

 if( x + width > screenSize.width ) x = screenSize.width - width;
 if( y + height > screenSize.height ) y = screenSize.height - height;

 if( width > screenSize.width ) width = screenSize.width;
 if( height > screenSize.height ) height = screenSize.height;

 this.setLocation(x, y);
 this.setSize(width, height );

 if( gameConfig.screenMaximized ) {
  this.setExtendedState(getExtendedState() | MAXIMIZED_BOTH );
 }



This works as expected, but with one big exception; it doesn't account for taskbars. On windows, if the window is past the bottom of the screen, this code will correct it, but it still leaves a piece of the window blocked by the taskbar.

I'm not sure how to do this. Is there someway to ask java about any taskbars in the system, and what their width/height is?

+3  A: 

Use getScreenInsets (Java 4+):

static public Insets getScreenInsets(Window wnd) {
    Insets                              si;

    try {
        if(wnd==null) { si=Toolkit.getDefaultToolkit().getScreenInsets(new Frame().getGraphicsConfiguration()); }
        else          { si=wnd.getToolkit()           .getScreenInsets(wnd.getGraphicsConfiguration());         }
        } catch(NoSuchMethodError thr) { si=new Insets(0,0,0,0); }
    return si;
    }

(This method allows for multiple screens, and older JVM's that don't support the API).

And, always remember the task bar may be on any edge of the screen, not just the bottom.

Software Monkey
"Task bar may be on any edge of the screen" -- that's my taskbar you're talking about (at the right of the screen). Too many applications *forced* me to move it to the left (arghh!), and still some don't work correctly.
Hosam Aly
Yeah - Sun didn't help us though, waiting until Java 4 to include the ability to determine where the darn thing was.
Software Monkey
A: 

Thanks that worked perfectly.

Do you know how to get it so Java will reflect the total screen size of both of my monitors when I call getScreenSize() ? Right now it is returning 1600x1200, when it's really 3200x1200, spanned across two monitors.

The Java API suggests that GraphicsConfiguration.getBounds() would do the trick, but that still returns the rectangle {0, 0, 1600, 1200}.

JoshuaD
Try making this a separate question, and maybe linking to it in this one. This will probably be more beneficial to anyone searching for the same info.
Hosam Aly
BTW: You can't logically the total size of both screens because you'd end up with an irregular shape that a rectangle (top,left,width,height) can't describe if your monitors are different resolutions (like mine at 1920x1600 and 1280x1024).
Software Monkey
A: 

On the issue of multiple screens, I've not actually done that, but I believe each screen has it's own graphics config. As I understand it, you can enumerate the configurations to find the one you want - then you use the same API's as we've already discussed.

Looks like the doco for GraphicConfiguration has the detail you need:

GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[]    gs=ge.getScreenDevices();
for(int j = 0; j<gs.length; j++) { 
    GraphicsDevice gd=gs[j];
    GraphicsConfiguration[] gc=gd.getConfigurations();
    for (int i=0; i<gc.length; i++) {
        ...
        }
    }
Software Monkey