Ive been following along with a tutorial to make a 2d game. Ive gotten as far as creating the Window and coloring it, setting keyboard inputs, but now im trying to import an image. In the commens of this video, http://www.youtube.com/watch?v=o7pfq0W3e4I , it seems like everyone else is having the same problem.
Line of code in main class:
private SpriteSheet spriteSheet = new SpriteSheet("/spritesheet.png");
code for SpriteSheet class:
package GFX;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SpriteSheet {
public String path;
public int width;
public int height;
public int[] pixels;
public SpriteSheet(String path){
BufferedImage image = null;
try {
image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
} catch (Exception e) {
System.out.println("Cant find image");
}
if(image == null){
return;
}
this.path = path;
this.width = image.getWidth();
this.height = image.getHeight();
pixels = image.getRGB(0,0,width,height,null,0,width); //0xffABCdAs
for(int i = 0;i<pixels.length;i++){
pixels[i] = (pixels[i] & 0xff)/64;
}
for(int i = 0;i<16;i++){
System.out.println(pixels[i]);
}
}
}
I have ried all differnt directories, even the one starting from the harddrvie, but still nothing, its just returning "Cant find image", as indicated in the try-catch. The tutorials had it like
try { image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
} catch (IOException e) {
e.printStackTrace();
}
this did not work and returned this error:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1348)
at GFX.SpriteSheet.<init>(SpriteSheet.java:20)
at inputHandling.Game.<init>(Game.java:23)
at inputHandling.Game.main(Game.java:128)
Many people in the comments also posted this error, and when i comment out the path in the main class, the program runs but without finding the image.
Thanks.