public void paintComponent(Graphics g) {
g.setColor(Color.red);
g.fillRect(ball.getX(), ball.getY(), ball.getWidth(), ball.getHeight());
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_DOWN) {
System.out.println("down");
ball.moveY(5);
}
if(keyCode == KeyEvent.VK_UP) {
System.out.println("up");
ball.moveY(-5);
}
if(keyCode == KeyEvent.VK_LEFT) {
System.out.println("left");
ball.moveX(-5);
}
if(keyCode == KeyEvent.VK_RIGHT) {
System.out.println("right");
ball.moveX(5);
}
System.out.println("X: " +ball.getX() +", Y: " +ball.getY());
repaint();
}
當我按下箭頭鍵移動ball
,爲什麼不repaint()
方法從之前抹去球的位置?這是創造一個尾巴的事情。
感謝
你應該叫'super.paintComponent方法(G)'和使用的按鍵組合在keylisteners – nachokk