Loading Images

I’m having some trouble loading images into my game. The images are currently saved in the directory where my class files are located. I’m using Windows 2000. I call the ClassLoader.getResource() method and it returns a URL that looks something like this: “file:C:/_Work/…/image.gif”. Is this a valid URL to pass to the javax.imageio.ImageIO.read function or does the URL have to be an actual http address. I’m not sure that the question makes sense, but can anyone explain how to load images in Java.

Regards,
Igor

Yes, you use getResource… paths with ‘/’ instead of ‘’ and lowercase extensions and pass that over to ImageIO. And yes it works even if you put it into a jar file (it works in both cases image inside the jar and image outside).

What I don’t understand. Why do you ask? You could have just tried it for yourself in the same time.

It still doesn’t work. When I attempt to load an image that I find on google images it works fine. But when I try to load them from my disk it (ImageIO) returns a null image. I use JBuilder so I put the image in the \classes directory. It (getResource) finds the image but when I pass the URL to ImageIO it still returns a null.

Regards,
Igor

OK, I made some progress. The problem arises when ImageIO calls the getImageReaders() method. I looked inside it but didn’t understand what the problem was. Anyway, when it calls getImageReader() the method returns an empty Iterator. This is why it keeps returning null. What file formats does ImageIO support. I was using .gif and thought that it was supported. Can anyone help?

Regards,
Igor

Can anyone help?

As long as you don’t provide additional information - no.

try
{
//image loading
}
catch(Exception e)
{
e.printStackTrace();
}

Show us the stacktrace and print the url (wich you’ve created) and check it and so on.

Also have you already tried to run the programm without using JBuilder? (because JBuilder sucks big time and I won’t be surprised if it breaks everything)

OK :D,

I’m very sorry to have been wasting your time. oNyx you were absolutely right and your advice is worth gold. I can’t stop smiling. :-D. I just spent the last 3 days of my summer break on a simple mistake.

When I was saving the images using Microsoft Paint, I would write the following into the name edit box: image.gif. However, I would forget to change the format to gif. Assuming that Microsoft would do so automatically. This was the problem.

Again I apologize for my last 3 posts. Hey at least I learned something in the process.

Regards,
Igor

PS: I’m really not stupid, just 15. So please don’t hold my age and inattention to detail against me.

:smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley:

Hehe. Well, that’s just usual. You’ll see that you waste the most time with really silly mistakes.

Just some days ago I wasted alot of time hunting a bug in my “snap to grid” method. Actually… everything was perfect there it was my text printing routine(!) wich reseted that opengl stuff and I had simply forgotten that - doh :slight_smile:

Remember that the PC in 99.9999% of the cases exactly does that what you (or 3rd party apis) tell him to do. If there’s a bug and the obvious places aren’t the reason, check every single step carefully.

Oh and enable file extensions for the explorer - that avoids alot of confusion. It should be somewere under view->directory options (or so) and it’s called “hide file extensions of known file types” (or so)… disable that and use the “details” view. It’s much nicer then.

15 is a good age to start this stuff imo oh and excuse my manners - welcome to the board :wink: