The point of origin in my GUI is off by about 25/26 pixels

前端 未结 1 1299
抹茶落季
抹茶落季 2021-01-25 07:00

My point of origin (0,0) on my GUI is moved up by about 25/26 pixels. So the very top-left corner of my GUI seems to represent 0,25 or 0,26. I am using a double buffer to draw i

相关标签:
1条回答
  • 2021-01-25 07:32

    No it's not. You painting directly to the frame, which has it's border decorations painted WITHIN the frames boundaries. Welcome to the wonderful world of why you shouldn't paint directly to top level containers.

    Instead, move you custom painting to another component, such as a JPanel and add this directly to the frame. This will be laid out in such away so as to positioned within the frames border decorations

    By overriding the panel's getPreferredSize method and returning an appropriate value, you can utilise JFrame#pack to "pack" the frame so that it meets the preferred size requirements of the content. This will ensure that the content is at it's preferred size and the window decorations surround it.

    Also remember, Swing components are double buffered by default, so you won't have to reinvent the wheel, unless you have a particular need to do so...

    For more information, take a look at How can I set in the midst? and How to get the EXACT middle of a screen, even when re-sized and Why is overriding paint in a top-level container so bad? and Why not to draw directly inside JFrame for more details

    0 讨论(0)
提交回复
热议问题