How do I call Graphics Method inside KeyListener?

懵懂的女人 提交于 2019-12-02 05:46:20
ControlAltDel

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()

OK! I actually figured it out by myself! What I did is called the g2d.rotate(.....) inside my draw() method. What I did is created variables that check for rotation, and set them true or false inside of my KeyPressed() method. Here is the code I used inside the draw() method:

 if(isRotatingPositive == true) {
    g2d.rotate(Math.toRadians(speed));
 } else if(isRotatingNegative == true) {
    g2d.rotate(Math.toRadians(-speed));
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!