本文介绍了我如何在KeyListener中调用Graphics方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提前,我想说有很多类似这样的帖子,但是,它们都不适用于我,或者它们都没有任何答案,并且已经过时,这意味着可能会有新的java功能,可以帮助我解决我的问题。

无论如何,我想制作一款有网球拍的游戏。当然,他们必须旋转。为了旋转,我必须在我的KeyListener中调用我的Graphics2D方法。我如何做到这一点,而不是在我的键监听器方法中添加一个新的Graphics2D变量?



以下是实现此目标的所有方法: (Graphics2D g2d){
g2d.drawImage(getPaddleImg(),x,y,')

  public void draw空值); 

$ b public static Image getPaddleImg(){
ImageIcon ic = new ImageIcon(C:/ Users / Elliot / Desktop / Eclipse Game Tennis / paddle.png);
return ic.getImage();
}

public void keyPressed(KeyEvent e){
int key = e.getKeyCode();

if(key == KeyEvent.VK_W){
g2d.rotate(Math.toRadians(5));
} else if(key == KeyEvent.VK_W){
g2d.rotate(Math.toRadians(-5));
}

public void keyReleased(KeyEvent e){
int key = e.getKeyCode();

if(key == KeyEvent.VK_W){
g2d.rotate(Math.toRadians(0));
} else if(key == KeyEvent.VK_W){
g2d.rotate(Math.toRadians(0));


我知道,这段代码会给我一个错误,因为在 KeyPressed() KeyReleased() g2d c>方法。我如何在这两种方法中调用 g2d ?或者我该如何在 draw()方法中做同样的事情?解析方案

错误的方法:您可以使用 Component.getGraphics()来获取图形对象。



正确的方法:所有的绘画应该在 paint(Component)方法内完成。这是因为用户可以随时调整/打开/关闭窗口。在你的 KeyListener 中(也研究关键字),你应该更新一个关于如何绘制玩家/球拍的设置,然后调用 repaint() code>


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 outdated, meaning there could be new java features that could help me solve my problem.

Anyway, I wanted to make a game where there is tennis rackets. Of course, they would have to rotate. In order to rotate, I must call my Graphics2D method inside my KeyListener. How would I do this WITHOUT adding a new Graphics2D variable inside my key listener method?

Here is all the methods I have that involve accomplishing this goal:

        public void draw(Graphics2D g2d) {
            g2d.drawImage(getPaddleImg(), x, y, null);
        }

        public static Image getPaddleImg() {
            ImageIcon ic = new ImageIcon("C:/Users/Elliot/Desktop/Eclipse Game Tennis/paddle.png");
            return ic.getImage();
        }

        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();

            if(key==KeyEvent.VK_W){
                g2d.rotate(Math.toRadians(5));
            } else if(key==KeyEvent.VK_W) {
                g2d.rotate(Math.toRadians(-5));
            }

        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();

            if(key==KeyEvent.VK_W){
                g2d.rotate(Math.toRadians(0));
            } else if(key==KeyEvent.VK_W) {
                g2d.rotate(Math.toRadians(0));
            }
        }

I know, this code would give me an error because of the g2d inside of the KeyPressed() and KeyReleased() method. How would I call the g2d inside these two methods? Or how would I do the same thing inside the draw() method?

解决方案

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

这篇关于我如何在KeyListener中调用Graphics方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:45