1

I've been using Java Swing for quite some time now and I never found a solution to this problem. When I create a JFrame the window surrounding it is actually smaller than the frame. In the included picture below my JFrame size is 800x600. The 2 white lines crosses at the center of the frame, 400,300. As you can see they are not at the center of the window. If I stretch the window right and down I can see some of the black background of the frame was hidden. When the black background is revealed you can see the the lines do indeed cross at the center (2nd picture).

Why is it working like that? Anything I can do to solve this problem? Im making a game where the playable character is in the center of the screen so this causes me a lot of problem. The 1st image is larger because i've left the code in the background. As we can see it's a standard JFrame creation.

Not centered because part of the frame is hidden:

Not centered because part of the frame is hidden

centered when frame is fully revealed:

centered when frame is fully revealed

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 5
    The frame has decorations (ie. the title bar and borders). The panel where you do the painting is added to the frame, so therefore is will be less than the size of the frame. Your problem is that you are doing things backward. The proper approach is to override the `getPreferredSize()` method of the JPanel where you do the custom painting to return the desired size of the panel. Then after you add the panel to the frame you invoke the pack() method on the frame. Now the frame will be sized to fit the complete panel and your painting will be accurate. – camickr Jan 03 '20 at 21:54
  • @camickr please turn this into an answer for upvote – TheCoolDrop Jan 03 '20 at 23:11
  • For [example](https://stackoverflow.com/questions/13734069/how-can-i-set-in-the-midst/13734319#13734319), [example](https://stackoverflow.com/questions/24858259/the-point-of-origin-in-my-gui-is-off-by-about-25-26-pixels/24858285#24858285) and [example](https://stackoverflow.com/questions/13457237/how-to-get-the-exact-middle-of-a-screen-even-when-re-sized/13460914#13460914) of what Camickr has commented and I'm sure he also has his own answers – MadProgrammer Jan 04 '20 at 00:17
  • @camickr It's working, tks! Can you turn your comment as an answer so i can select it as the proper solution pls? Otherwise i would have to create the answer and you wont get the credit for it... – Frédéric Bélanger Jan 04 '20 at 21:36
  • If the answer was helpful consider upvoting it as well. – c0der Mar 24 '20 at 04:39
  • tks, did that with my other post as well :-) – Frédéric Bélanger Mar 25 '20 at 21:37

1 Answers1

3

my JFrame size is 800x600

You are doing things backward.

The frame has decorations (ie. the title bar and borders). The panel where you do the painting is added to the frame, so therefore it will be less than the size of the frame.

The proper approach is to override the getPreferredSize() method of the JPanel where you do the custom painting to return the desired size of the panel.

Then you add the panel to the frame you invoke the pack() method on the frame. Now the frame will be sized slightly larger (to fit the complete panel and the frame decorations) and your painting will be accurate.

camickr
  • 321,443
  • 19
  • 166
  • 288