我正在使用Java Gui制作游戏。 我想让角色在键盘上移动。 所以我训练这个例子,但它没有用。
我在Board.java中创建了子类TAdapter。 但是当我按下键盘时,actionPerformed(ActionEvent e)不起作用。
以下是我的代码。
[MovingSpriteEx.java]
import java.awt.EventQueue;
import javax.swing.JFrame;
public class MovingSpriteEx extends JFrame {
public static final int SCREEN_WIDTH = 900;
public static final int SCREEN_HEIGHT = 720;
public MovingSpriteEx() {
initUI();
}
private void initUI() {
add(new Board()); // ADD THE PANEL
setSize(900, 720);
setResizable(false);
setTitle("Moving sprite");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO : WHAT IS IT?
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MovingSpriteEx ex = new MovingSpriteEx();
ex.setVisible(true);
}
});
}
}
[Board.java]
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Timer;
import javax.swing.JPanel;
public class Board extends JPanel implements ActionListener {
// private Timer timer;
private Craft craft;
private final int DELAY = 10;
public Board() {
initBoard();
}
private void initBoard() {
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.BLACK);
craft = new Craft();
//
// timer = new Timer(DELAY, this);
// timer.start();
}
@Override
// TODO what is is?
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
Toolkit.getDefaultToolkit().sync();
}
//In the doDrawing() method, we draw the spacecraft with the drawImage() method.
// We get the image and the coordinates from the sprite class.
private void doDrawing(Graphics g) {
//TODO : what is it?
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(), this);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("call the move");
craft.move();
repaint();
}
private class TAdapter extends KeyAdapter {
@Override
public void keyReleased(KeyEvent e) {
craft.keyReleased(e);
}
@Override
public void keyPressed(KeyEvent e) {
craft.keyPressed(e);
}
}
}
[Craft.java]
import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
public class Craft {
private int dx;
private int dy;
private int x;
private int y;
private Image image;
public Craft() {
initCraft();
}
private void initCraft() {
ImageIcon ii = new ImageIcon(MovingSpriteEx.class.getResource("/images/dino_front.png"));
image = ii.getImage();
x = 40;
y = 60;
}
public void move() {
System.out.println("move");
x += dx;
y += dy;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Image getImage() {
return image;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
System.out.println("key pressed");
dx = -1;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 1;
}
if (key == KeyEvent.VK_UP) {
dy = -1;
}
if (key == KeyEvent.VK_DOWN) {
dy = 1;
}
}
public void keyReleased(KeyEvent e) {
System.out.println("key released");
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = 0;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 0;
}
if (key == KeyEvent.VK_UP) {
dy = 0;
}
if (key == KeyEvent.VK_DOWN) {
dy = 0;
}
}
}
问题是什么?