简体   繁体   English

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

[英]Work with the keyboard (“AWT-EventQueue-0”)

having a little difficulty. 有一点困难。 I ask you to look at the code: 我请您看一下代码:

1 class (MyCanvas.java) 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);
    }

}

The second class (InputKey.java) 第二类(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
    }
}

First class works perfectly and the picture frame is displayed in it too.But when I press the button (the left arrow or the right), I get an error: 头等舱工作正常,并且相框也显示在其中。但是当我按下按钮(向左箭头或向右箭头)时,出现错误:

 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)

Please tell me what to fix in the code to make it work) 请告诉我要修复的代码以使其正常工作)

Thanks in advance, sorry for the bad design. 在此先感谢您,不好的设计。

UPD UPD

Still have not figured out a bit easy, but I guess not. 还是没有想通一点,但是我想不是。 If someone writes a ready solution to the problem - I will be happy) 如果有人写出解决问题的现成解决方案-我会很高兴)

The problem causing the NullPointerException was that you didn't assigned a value to cv. 导致NullPointerException的问题是您没有为cv分配值。

I changed the code so it works now: 我更改了代码,现在可以使用了:

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

}

There was also a repaint() missing which I added. 我还添加了一个repaint()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM