Your call to repaint()
is repainting the class Canvas
, but the painting is done on the JPanel pane
. The resizing causes an automatic repaint of the panel. So to fix you want to pane.repaint()
, but you can't do that unless you put the panel as a class member, so you can access it from the listener method. Right now, it's currently locally scoped in the createFrame()
method.
Also, you should probably add the listener to the panel instead, and not even extend Canvas
, since you're not even using it
Other Notes:
Look into using Key Bindings instead of low-level KeyListener
Swing apps should be run from the Event Dispatch Thread (EDT). You can do so by simply wrapping the code in your
main
in aSwingUtilities.invokeLate(..)
. See more at Initial ThreadsAgain, I'll just add, Don't extends
Canvas