我的圖片不動。
這裏是應用程序的整個代碼:圖片不動
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);
}
}
這有什麼錯呢?
請指出您可能需要添加的錯誤。
所有的節目,但是當你按下畫面不動 –
你爲什麼認爲它應該移動? –
那麼,我寫了一個運動的方法,但他們顯然不工作...你能告訴什麼是錯的? –