How do I re run the paint method so the JPanel is animated?

前端 未结 2 520
孤独总比滥情好
孤独总比滥情好 2021-01-14 06:07

I think I need to put some code where the comment is (or maybe use non static method but I am not sure). The main method creates the window and then starts the graphics meth

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-14 06:34

    your issues are

    1) by calling Thread.sleep(int) in the Swing related code, never do that, for delaying in Swing (there are lots of topics about why not use sleep in programing languages ...) use Swing Timer

    2) your JPanel doesn't returns any XxxSize

    3) for Swing use paintComponent(), only if you have got really important reasons then use method paint() more about repaint and animating Graphics in the 2D Graphics tutorial

    4) Swing GUI should be built in the Event Dispatch Thread

提交回复
热议问题