0

I was doing a tutorial online because I wanted to make a 2d side scroller, and I got this exact error. I have googled it but came up with nothing. I tried looking for a typo and it looks clean, its not giving me an error anywere else in the code. I do not know where to start. If you could explaing to me what the error is and how i fix it then that would be amazing.

    package Main;


import GameState.GameStateManager;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

public class GamePanel extends JPanel implements Runnable, KeyListener{
    public static final int WIDTH = 320;
    public static final int HIGHT = 240;
    public static final int SCALE = 2;

    //game thread

    private Thread thread;
    private boolean running;
    private int FPS = 60;
    private long targetTime = 1000/FPS;
    //image        
    private BufferedImage image;
    private Graphics2D g;

    //game state manager
    private GameStateManager gsm;




    public GamePanel(){
        super();
        setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
        setFocusable(true);
        requestFocus();
    }
    public void addNotify(){
        super.addNotify();
        if (thread == null) {
            thread = new Thread(this);
            addKeyListener(this);
            thread.start();
        }
    }   
    private void init() {
        image = new BufferedImage(WIDTH, HIGHT, BufferedImage.TYPE_INT_RGB);     
        g = (Graphics2D) image.getGraphics();
        running = true;
        gsm = new GameStateManager();

    }

        public void run(){
        init();
        long start, elapsed, wait;

        //game loop
          while(running) {

            start = System.nanoTime();
            update();
            draw();
            drawToScreen();
               elapsed = System.nanoTime() - start;

               wait = targetTime - elapsed / 1000000;

               try
               {
                   Thread.sleep(wait);
               }
               catch(Exception e) 
               {
                   e.printStackTrace();
               }//end of try catch
          }







        }
        private void update()
        {
            gsm.update();
        }
        private void draw()
        {
            gsm.draw(g);
        }
        private void drawToScreen()
        {
            Graphics g2 = getGraphics();
            g2.drawImage(image, 0, 0, null);
            g2.dispose();


        }




        public void KeyPressed(KeyEvent key) 
        {
         gsm.keyPressed(key.getKeyCode());
        }
        public void KeyReleased(KeyEvent key) 
        {
         gsm.keyReleased(key.getKeyCode());
        }




    }
4

2 回答 2

3

编译器错误消息准确地告诉您出了什么问题:您的类实现了 KeyListener 接口,但没有实现该接口的所有必要方法。解决方案:确保按照 KeyListener API 实现所有必要的方法。还要确保使用@Override注释来确保您的覆盖是正确的。

话虽如此,我将建议您不要将 KeyListeners 用于 Swing 应用程序的大多数键盘输入,因为它是一个低级侦听器,应该避免使用更高级别的构造,例如键绑定。此外,Swing GUI 应该避免使用update(...)方法覆盖,因为这更像是一种 AWT 构造。

于 2013-09-03T03:36:03.170 回答
0

您的 KeyReleased(KeyEvent key)方法必须以小写字母 'k' 开头,例如keyReleased(KeyEvent key)。Java 区分大小写。

您可能还需要覆盖 KeyListener 接口的其他方法。

当您想要覆盖超级方法时,还要向该方法添加@Override 注释(如@Hovercraft Full Of Eels 所建议的那样)。这样 IDE 将在编码时为您提供提示。

于 2013-09-03T04:00:45.313 回答