I try to draw a rectangular in Java. I set the frame size (800,400) and resizable(false) rectangular’s x = 50, y = 50 width = 700 height = 300. Why isn’t it in the middle? Thank you.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Without any evidence otherwise, I’d guess you’ve overriden the
paintmethod of something like aJFrameand are painting directly to it.The problem is, frames have decoration (a border and title bar for example), which takes up space inside the frame…
Technically, this is correct. The rectangle is painted in the center of frame, but the because of the frame’s decorations, it looks like it’s slightly high…
Instead, you should be painting onto the frame’s content area instead.
Here the rectangle now looks correctly centered. In my tests, I set the first frame (bad) to 800×400, I made the second frame’s content pane’s preferred size 800×400, which made the frame size actually 816×438, as the frame’s decorations are now outside of the paint area.
This is, one of many reasons, why you should not override the
paintmethod of top level containers 😉