AWT Frame and Canvas parented to LWJGL Display is resizing on creation

This is baffling me. For some reason, the AWT Frame and its child canvas that I’m using with LWJGL’s Display.setParent() is resizing itself away from the dimensions I set it as. This used to work fine, and looking through my revision history, I can’t figure out how I broke it.

Relevant parts of my code:


//Frame f is a static final that is initialized when it is declared.

setupFrame(Canvas c) {
        //determine the width and height to use for the game window, put them in resX, resY
        f.setSize(resX, resY);
        f.setResizable(false);
        f.setIgnoreRepaint(true);
        f.add(c);
        f.setVisible(true);
}

setupLWJGL() {
        //locate natives, then...
        try {
            Display.setDisplayMode(new DisplayMode(resX, resY));
            Display.setParent(cnv); //cnv is the same as c from the setupFrame(Canvas c)
            Display.create();
        } catch(LWJGLException ex) {
            Logger.getLogger(GameBase.class.getName()).log(Level.SEVERE, null, ex);
        }
        cnvW = Display.getWidth();
        cnvH = Display.getHeight();
        //...lighting, blending, matrices, etc....
}

Currently, the size of the frame should be 1200x675, and the canvas and Display should be a little less than that (1194x647) because of window borders. However, at some point in the code, the frame and canvas (and thus Display) sizes change from about 1194x647 to 1280x1002.

I’ve been mucking around in NetBean’s debug mode and also with a bunch of print statements to figure out where the canvas and frame end up changing their dimensions. This goes wrong in two different ways and seems to happen at two different spots:

  1. Display.setDisplayMode(new DisplayMode(resX, resY)); Before that line, the canvas dimensions are 1194x647, afterward they are 1280x1002. The game renders correctly, but in a window that takes up the whole screen instead of 1200x675.

  2. f.setVisible(true); The same dimension change occurs during that line. I tried to follow the source code, and it seems to happen when the Component calls peer.setVisible(true).

Sometimes, the window takes up the full screen (it is still a decorated window, it’s not in full screen mode), but only draws on a section of it that looks like it’s about the 1200x675 size the window is supposed to be. I can’t reliably reproduce this, but it happens sometimes. Also, which spot the frame and canvas change dimensions at is unpredictable, but seems to happen more on f.setVisible() when I’m in debug mode.

I’m guessing this isn’t actually due to something within the code I’m using so much as maybe a synchronization issue? Since it seems to change when it happens when I’m using debug mode, the timing seems important.

Any ideas?

EDIT: It looks like if I remove the Display.setDisplayMode(), which I think is supposed to be unnecessary anyway if I give the Display a parent, the resizing changes to during the Display.create() line, and I get the glitch where the window is still big, but the rendering is the right size and takes up only part of the window and is scaled oddly. No, wait… I just ran it again without setDisplayMode(), and it resized at Display.setParent(cnv) and rendered in the whole window. So, this is very inconsistent and seems to not be connected to any particular piece of code I’m writing.

Thaaaaat was weird. If I add another f.setVisible(true) before the f.add©, it works as it should. The window is the right size, the rendering is the right size and isn’t squashed or stretched.

I guess Frames need to be set to validated or made displayable or something before a Canvas is added to them, or the Frame can get resized (and the canvas with it) somewhere behind the scenes later. This even works if I then remove the f.setVisible(true) that came after f.add©, even though I thought that needed to be called to re-validate stuff after adding a component. Weird.

Sigh

Correction: It works sometimes. Other times, it still resizes the window to take up the whole screen.

EDIT: It also just used the right sized window, but the rendering was stretched, as if it was trying to stretch the correct size to fit a larger window than was there.

EDIT again: Looks like I’d also added an f.setMinimumSize() and f.setSize() after the second f.setVisible(). If I remove those, I get full screen sized window again.

Sorry for all the self-replies. I’m not sure whether to edit or add a new reply in cases like this, where it’s new information being added.

As best I can tell, the window is being maximized behind my back somewhere. When the window ends up big instead of the size it’s supposed to be, f.getExtendedState() is returning 6 of MAXIMIZED_BOTH instead of the 0 of NORMAL.