How do I call Graphics Method inside KeyListener?

前端 未结 2 569
被撕碎了的回忆
被撕碎了的回忆 2021-01-24 07:41

Ahead of time, I would like to say there have been many posts similar to this, however, none of them apply to me or none of them actually have any answer at all, and are outdate

2条回答
  •  一整个雨季
    2021-01-24 08:13

    The wrong way: You can use Component.getGraphics() to get the graphics object.

    The right way: All painting should be done inside the paint(Component) method. This is because a user could resize/open/close the window at any time. In your KeyListener (also research keybindings) you should update a setting on how the player/ racket should be drawn, and then call repaint()

提交回复
热议问题