Loading a BufferedImage [solved]

I’m using the LWJGL for my sprites and stuff and I have no problem with that. But because of the getRGB() method in the BufferedImage class (I want to read level maps from pngs) I have to load a BufferedImage

So… What’s wrong with this code? From everything I’ve read it should work

BufferedImage img = ImageIO.read(this.getClass().getResource("res/testMap.png"));

Here’s how my hierarchy is structures:
In my project I have these folders: src, res, lib
In my res folder, I have “testMap.png”

Hopefully it’s just something silly I don’t understand
Thanks

split this statment in two, so that you can see if the image is found or not.
If not the method returns null, what it will be in your case.

The the thing what you are doing wrong is, that you are searching relatively to your class.

Say you have a class named “org.nhmllr.MyApp”, your code tries to load “./org/nhmllr/res/testMap.png”.
If you want to load relatively to your App the path you enter has to start with a “/” ->


BufferedImage img = ImageIO.read(this.getClass().getResource("/res/testMap.png"));

Besides, you only need to use this getClass().getResource() stuff if you want to read files from within your JAR(yes you can store your images inside your JAR)
When you just want to read an imagefile you can use simply this


BufferedImage img = ImageIO.read("res/testMap.png");

I thought I was already storing images in my JAR
The “res” folder is in the same folder as my “src” folder, so isn’t that what you mean?
Also,

ImageIO.read("res/testMap.png");

won’t even compile because read() doesn’t take strings (says eclipse)

Also, when I put the “/” before the “res” like you said it still doesn’t work. Maybe I’m not storing it properly?

(also I’m using the default package just because this is a little test app)

I couldn’t figure out how to post an image from my hard drive so I’ll just “draw” it out as it’s seen in eclipse

Image Reader
_>src
__>(default package)
___>ImageReaderClass.java
___>ImageReaderRunner.java
_>JRE System Library [JavaSE-1.6]
_>Referenced Libraries
_>lib
_>res
__>testMap.png

mm sry it seems as you have to use the File class so ->
ImageIO.read(new File(“pathtofile/image.png”));

let me ask something quick? how do you compile and run your programm?
It seems to me you are doing that per console?

What you want normally is to get a extecutable Jar file.
compile somehow so you get something like this

->MyAppFolder
–>libs/
–>myapp.jar(the following is inside the jar, which is a simple zip file)
—>MANIFEST/
---->MANIFEST.??
—>com/
---->nhmllr/
----->Main.class
—>resources/
---->mySuperCoolImage.png

If your images are stored outside your Jar file, you have to navigate to it with a relative path starting from the folder you are starting your App in.

That looks alright.

Have you added the res folder in your build path?

Project -> properties -> Java Build Path -> Source -> Add folder

The src folder that eclipse automatically makes is by default in your build path, so if you save the images under the src folder you should be able to see them. I recommend just adding the res folder in your build path.

In an older thread it’s been mentioned you can use this to gather resources.

[quote]URL url = this.getClass().getClassLoader().getResource(“resources/Ball.png”);
[/quote]
source: http://www.java-gaming.org/topics/file-path-that-works-in-both-applets-and-applications/26357/msg/230741/view.html#msg230741

Yeah currently I’m doing with within Eclipse. But I mean, if I were to make it into a JAR later it should still work

As for putting the res in the src, I don’t want to do that. This is a test app such that when I get it all up and running I’ll paste the classes into my larger project. There it already works with the res out of the src. (I load those images as LWGGL textures, as I explain in the original post)

Eclipse won’t even let me declare “URL url = …” It changes URL to “java.net.URL url = …” and neither work

yyyarrrrrg this is frustrating.

I don’t recommend it either. But what about

Are you using java7??

http://www.java-gaming.org/topics/applet-and-app-walk-into-a-jar/26301/msg/229965/view.html

ooooooohhhh I conflated the issue. I thought that putting it in the build path meant putting it in src. I see.
(I don’t deny my title of “Java n00b”)

Also I’m using Java 6
I’ll edit back here in a few minutes and tell you how it turned out

EDIT: It works! It’s glorious!
Thank you everybody!