使用键盘(“AWT-EventQueue-0”)

时间:2013-10-05 11:16:28

标签: java swing nullpointerexception awt keylistener

有点困难。我请你看一下代码:

1班(MyCanvas.java)

package Game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JComponent;
import javax.swing.JFrame;

class MyCanvas extends JComponent {

    private static final long serialVersionUID = 1L;

    private static final int WIDTH = 490;
    private static final int HEIGHT = 470;
    public static InputKey input = new InputKey();
    private int x = 10;
    private int y = 10;

    // public MyCanvas() {
    // addKeyListener(input);
    // }

    public void move() {
        if (x == 0) {
            x = 10;
        }
        if (y == 0) {
            y = 10;
        }

        if (input.left) {
            x--;
        }
        if (input.right) {
            x++;
        }
    }

    public void paint(Graphics g) {
        Image img1 = Toolkit.getDefaultToolkit().getImage(
                "C:\\Users\\дНМ\\workspace\\Game\\image\\Peopl.png");

        int width = img1.getWidth(this);
        int height = img1.getHeight(this);

        int scale = 4;
        int w = scale * width;
        int h = scale * height;
        g.drawImage(img1, x, y, (int) w, (int) h, this);

    }

    public static void main(String[] a) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH, HEIGHT);
        frame.getContentPane().add(new MyCanvas());
        frame.getContentPane().setBackground(Color.BLACK);
        frame.setVisible(true);
        frame.setFocusable(true);
        frame.requestFocusInWindow();
        frame.addKeyListener(input);
    }

}

第二个类(InputKey.java)

package Game;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JComponent;

public class InputKey extends JComponent implements KeyListener {

    private static final long serialVersionUID = 1L;

    public boolean left;
    public boolean right;

    public  MyCanvas cv;

    void FBool() {
        left = right = false;
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            left = true;
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            right = true;
        }
        cv.move();
        repaint();
    }

    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            left = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            right = false;
        }
        //cv.move();
        //repaint();
    }

    public void keyTyped(KeyEvent e) {
        // bla...bla..bla
    }
}

第一堂课完美运作,画框也显示在里面。但是当我按下按钮(左箭头或右边)时,我收到一个错误:

 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Game.InputKey.keyPressed(InputKey.java:28)
    at java.awt.Component.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Window.processEvent(Unknown Source)

请告诉我在代码中要修复的内容以使其正常工作)

提前致谢,对不好的设计感到抱歉。

UPD

仍然没有想到有点容易,但我猜不是。如果有人为问题写了一个现成的解决方案 - 我会很高兴的)

1 个答案:

答案 0 :(得分:0)

导致NullPointerException的问题是你没有为cv赋值。

我更改了代码,现在可以使用了:

InputKey

package help.stackoverflow.keyboard_awt;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JComponent;

public class InputKey extends JComponent implements KeyListener {

    private static final long serialVersionUID = 1L;

    public boolean left;
    public boolean right;

    private MyCanvas cv;

    public InputKey(MyCanvas cv){
        this.cv = cv;
    }

    void FBool() {
        left = right = false;
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            left = true;
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            right = true;
        }
        cv.move();
        repaint();
    }

    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            left = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            right = false;
        }
        //cv.move();
        //repaint();
    }

    public void keyTyped(KeyEvent e) {
        // bla...bla..bla
    }
}

MyCanvas

package help.stackoverflow.keyboard_awt;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JComponent;
import javax.swing.JFrame;

class MyCanvas extends JComponent {

    private static final long serialVersionUID = 1L;

    private static final int WIDTH = 490;
    private static final int HEIGHT = 470;
    private InputKey input = new InputKey(this);
    private int x = 10;
    private int y = 10;

    // public MyCanvas() {
    // addKeyListener(input);
    // }

    public void move() {
        if (x == 0) {
            x = 10;
        }
        if (y == 0) {
            y = 10;
        }

        if (input.left) {
            x--;
        }
        if (input.right) {
            x++;
        }
        repaint();
    }

    public void paint(Graphics g) {
        Image img1 = Toolkit.getDefaultToolkit().getImage(
                "C:\\Users\\дНМ\\workspace\\Game\\image\\Peopl.png");

        int width = img1.getWidth(this);
        int height = img1.getHeight(this);

        int scale = 4;
        int w = scale * width;
        int h = scale * height;
        g.drawImage(img1, x, y, (int) w, (int) h, this);

    }

    public static void main(String[] a) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH, HEIGHT);
        MyCanvas myCanvas = new MyCanvas();
        frame.getContentPane().add(myCanvas);
        frame.getContentPane().setBackground(Color.BLACK);
        frame.setVisible(true);
        frame.setFocusable(true);
        frame.requestFocusInWindow();
        frame.addKeyListener(myCanvas.input);
    }

}

我还添加了一个重绘()缺失。