1

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

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

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

4

1 回答 1

0

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

我更改了代码,所以它现在可以工作了:

输入键

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
    }
}

我的画布

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);
    }

}

我还添加了一个 repaint() 。

于 2014-01-31T15:41:55.663 回答