如何获取JPanel中图像的位置(x,y坐标)
其实我正在做pacman游戏。吃豆子(图像)在Jpanel的迷宫中移动。我想知道pacman(图像)的xy坐标来检查图像的位置。
public class Pacman extends JPanel{
public Image pacLeft ;
int x = 0;
int y = 0;
int xa = 0;
int ya = 0;
private Board board;
Pacman(Board board){
this.board = board;
pacLeft = new ImageIcon("pac.png").getImage();
}
public void move(){
if(x + xa > 0 && x + xa < board.getWidth()-pacLeft.getWidth(board))
x = x+xa;
if(y + ya > 0 && y+ya < board.getHeight()-pacLeft.getHeight(board))
y = y+ya;
}
@Override
public void paint(Graphics g) {
g.drawImage(pacLeft,x,y,this);
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_LEFT){
xa = -1;
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT)
xa = 1;
if(e.getKeyCode() == KeyEvent.VK_UP)
ya = -1;
if(e.getKeyCode() == KeyEvent.VK_DOWN)
ya = 1;
}
public void KeyReleased(KeyEvent e){
xa = 0;
ya = 0;
}
}