I've made a couple games in Java using Swing/AWT, using the following method:
- Use a JComponent (not a subclass) to draw graphics to.
- Call the add() method of the JFrame's content pane to add my JComponent.
- Create a loop for game math and repaint(), etc.
The only problem is, I can't call the JFrame's pack() method, even when I call setPreferredSize(), setMaximumSize(), and setMinimumSize() on the JComponent, the window goes to smallest possible size. So instead I use the resize() method. This works for the most part, but if I decided to draw an image of width and height (WIDTH, HEIGHT), I can't call resize(WIDTH, HEIGHT) without the JFrame's size being off by 10-20 pixels.
And not only that, but when I resize it properly using offsets, the results are different on Windows XP, Windows Vista, and Linux (GNOME).
So, is there a way I can use Swing to set the JFrame to the proper size, so that the viewed area is the same size (in pixels) as the two numbers I specify? Normally I can use the 3 sizing methods I mentioned and call JFrame.pack(), no problems.