gpt4 book ai didi

java - 使用键盘 ("AWT-EventQueue-0")

转载 作者:行者123 更新时间:2023-12-01 04:18:42 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

我的 Canvas

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

关于java - 使用键盘 ("AWT-EventQueue-0"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19197170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com