我的图像不动。
这是应用程序的完整代码:
package Game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends JPanel {
private static final long serialVersionUID = 1L;
private BufferedImage image;
public static final int WIDTH = 600;
public static final int HEIGHT = 500;
private int x = 1;
private int y = 1;
private static int dx;
private static int dy;
private boolean leftPressed = false;
private boolean rightPressed = false;
private boolean topPressed = false;
private boolean downPressed = false;
public static void main(String avg[]) throws IOException {
Game abc = new Game();
}
public Game() {
try {
JFrame frame = new JFrame();
image = ImageIO.read(new File(
"C:\\Users\\дНМ\\workspace\\Game\\image\\heroG.png"));
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
frame.getContentPane().add(this);
this.setBackground(Color.BLACK);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Tittle - Game");
addKeyListener(new KeyInputHandler());
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void move() {
x += dx;
y += dy;
if (x < 1) {
x = 1;
}
if (y < 1) {
y = 1;
}
}
public void update(long delta) {
if (leftPressed == true) {
x--;
}
if (rightPressed == true) {
x++;
}
}
public void update2(long delta) {
if (downPressed == true) {
y--;
}
if (topPressed == true) {
y++;
}
}
private class KeyInputHandler extends KeyAdapter {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftPressed = true;
dx = -1;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightPressed = true;
dx = 1;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
topPressed = true;
dy = -1;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
downPressed = true;
dy = 1;
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftPressed = false;
dx = 0;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightPressed = false;
dx = 0;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
topPressed = false;
dy = 0;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
downPressed = false;
dy = 0;
}
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, x, y, null);
}
}
它出什么问题了?
请指出您可能需要添加的错误。