Reputation: 25
I am trying to load an image as part of my platformer, but it cant seam to find the image, I put it into the default package, which is the same as the class
public BufferedImage loadImage() {
try {
BufferedImage img = ImageIO.read(new File("level.jpg"));
System.out.println("Level Found!");
return img;
} catch (IOException e) {
System.out.println("Level Missing!");
return null;
}
}
Upvotes: 1
Views: 3018
Reputation: 8695
If you reference your files like this new File("level.jpg")
you have to put those files into the current directory. In most cases it's not the default package directory.
For example using Eclipse your typical directory structure would be like this:
+ MyProject
|
+ src
|
+ org/mypackage1
+ org/mypackage2
In order to get new File("level.jpg")
to work you have to put level.jpg
into the project root directory, like this
+ MyProject
|
+ src
| |
| + org/mypackage1
| + org/mypackage2
|
+ level.jpg
Upvotes: 3