Search code examples
javaimageawtinstantiationimageobserver

Java can't instantiate an Image?


So for my 2D game I want to use an image to represent a player but when I try to use an image, it tells me that it couldn't be "instantiated". I don't know what that means:

public class PlayerOne extends Entity{
  private Image img = new Image();
[...]
  @Override
  public void render(Graphics g){
  g.drawImage( img , x, y, Color.BLUE, new ImageObserver());
  }
}

I tried it in another class with BufferedImages but that somehow doesn't work.

So it can't create Objects of neither Image nor the ImageObserver. Does anyone know a fix for this error ?


Solution

  • You should have an image file (*.png for example ) to start with. Then use

    Image  img = new ImageIcon("img.png").getImage();