The problem is likely that getWidth()
in closePol
is called outside the scope of the paintComponent
method, so the current width is is not yet taken into account (or may still be zero), which could happen due a a few reasons.
As a general rule, when painting, you will want to call getWidth()/getHeight()
in the paint[Component]()
method. For example when expanding the frame, if getWidth()
is not called with the method, the new width will not be taken into account, and there will be no dynamic resizing of the graphic (if that's what you want.
You will also want to consider overriding getPreferredSize()
when doing custom painting, as to give the panel some sizing hints.