I am using frame.setSize(480, 400)
to set the size of my JFrame. Despite this, when I am doing some collision checking (I am making a breakout game), the ball goes completely off the bottom of the screen, then finally bounces. This is the collision code I'm using: if (YPos + height >= MainClass.height) { YVel = YVel * -1; }
if (YPos + height >= MainClass.height) { YVel = YVel * -1; }
. The static height variable is what is set to 400, and used in the frame.setSize
call. This has happened in my pong game aswell, although I fixed it with some hard set values. I wouldn't like to repeat this though, as this is bad practice.
Thanks for any responses, Rees.
don't to set frame.setSize(480, 400)
or frame.setPreferredSize(480, 400)
for painting to use JPanel
/ JComponent
, then return coordinates from this container
painting in Swing by default never returns any PreferredSize
have to override PreferredSize
for JPanel
/ JComponent
, (see dim.width
, dim.height
this methods returns proper coordinates for Object
(s) added to JPanel
/ JComponent
, after resize too)
then call (last code lines) JFrame.pack()
and JFrame.setVisible(true)
5. JFrame
doesn't returns proper coordinated have to calculating with its Borders
and Toolbar
, but ContentPane
return inner and proper size
Frames have borders. These borders sit INSIDE the available space provided, meaning the available space for contents is the frame size - border insets
Instead, set the preferred size of the content and use JFrame#pack
to compute the frame size
Check out
For some examples and more details
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.