我试图在java中绘制图像。在我创建一个单独的类来存储图像之前,它正在工作。但现在我得到空指针异常。
主类:
Image image;
Images images; // class object
public void paint(Graphics G){
image = images.GETImage();
G.drawImage(image, x, y, 20,20,null);
}
protected void paintComponent(Graphics G){
paint(G);
}
用于存储图像的 Image 类:
public Image GETImage(){
int direction = pacman.getDirection();
int newDirection = pacman.getDirection();
int x = pacman.getX();
int y = pacman.getY();
if(direction == Constant.UP){
ImageIcon i = new ImageIcon("src\\images\\pacman up.png");
image = i.getImage();
}
return image;
}