I have a JComponent
subclass that I am using to draw shapes onto my screen. In the constructor, I am trying to set ballX
and ballY
to half
Override getPreferredSize() in your JComponent
to return your preferred size, and start with half the width and height of that Dimension
. To the same end, this KineticModel invokes setPreferredSize()
in DisplayPanel.
Addendum: By way of explanation, your current approach fails because the results from getWidth()
and getHeight()
are invalid until validate()
has been called on the enclosing container, typically as the result of pack().