1

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;
  }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
DerekMallon
  • 25
  • 2
  • 3

2 Answers2

3

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 
Boris Brodski
  • 8,425
  • 4
  • 40
  • 55
0

Use get resource method of the class loader

user3041058
  • 1,520
  • 2
  • 12
  • 16