A noobish question about loading BufferedImage

Hi all just a quick questiong :slight_smile:

I understood that we can only load BufferedImages with ImageIO.read( url ) ; , right?

Thing is I have no idea about URL so I took only the example from the Planetalua space invaders tutorial and made the URL like this :
url = getClass().getClassLoader().getResource(filename);

Thins is , ok it works … but what If I wan tto have my images in another place and not in the classloader?How do I form my own URL to tell it where to load the images from?

I tried URL url = new URL( String string) ;

But it said something about malfunctioned URL and about a protocol.Please excuse my noobiness :frowning: , and any help is appreciated ! :smiley:

ImageIO’s read() method is also available as a version which takes an InputStream.

but what If I wan tto have my images in another place and not in the classloader?

Why?

hmm don’t know why , I just want toexplore all my options I guess.Hm did not know it can take InputStream too … need to studdy the API’s better I guess :-X

EDIT: Another question arose … I reread the tutorial to help me divide the code in classes and I made the SpriteCache class like in the tutorial :

package Version01;

import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.net.URL;
import javax.imageio.ImageIO;

public class SpriteCache
{
    private HashMap sprites;
    private URL url;
    
    public SpriteCache()
    {
        sprites = new HashMap();
    }
    
    private  BufferedImage loadImage(String filename)
        {
           
            try
            {
                url = getClass().getClassLoader().getResource(filename);
                return ImageIO.read(url);
            }
             catch(Exception e)
             {
                 e.printStackTrace();
                 return null;
             }
        }
    
    
    public BufferedImage getSprite(String name)
    {
        BufferedImage img = (BufferedImage)sprites.get(name);
        if(img == null)
        {
            img = loadImage("images/"+name);
            sprites.put(name , img);
        }
        return img;
    }
}

Thing is in my main class … in the paintWorld() method I try to load an image in a random position of the screen


public void paintWorld()
    {
        Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
        g.setColor(getBackground());
        testImage = spriteCache.getSprite("test.gif");
        g.drawImage(testImage , 80 , 90 , this);
        strategy.show();
    }

When I run it it throws a NullPointerException … :frowning: seems it can not find the image … I am sure the image has the right name though and is at the right place.So what is wrong with the code?

Sorry about double posting , but I bump my thread cause I edited the above reply of mine with another question

Uhm yea… use leading slashing in the path for making it relative to the roots in the classpath.

say your jar contains:
omg/wtf/Foo.class
omg/Bar.class
gfx/foobar.png

and another jar in your cp contains:
gfx/barfoo.png

Then you can access the foobar.png with “/gfx/foobar.png” and barfoo.png with “/gfx/barfoo.png” from Foo.class or Bar.class.

Thats the whole magic :slight_smile:

edit: See details here:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getResource(java.lang.String)

Hmmmm If I understood correctly all I had to do was add a / at the “images/”+name ?

I did that but it did not work :frowning:

Or again I might have completely misunderstood :stuck_out_tongue: , it is 2:00 here in Greece and I am kind of sleepy ; (

Well, whats the location of that test.gif? (Note that the whole thingy inclusive the extension is case sensitive)

C:\Java2\Game\build\classes\images\test.gif

That is where it is … but I don’t think it is that kind of mistake because I move the images folder and recompiled and ran many many times.

http://kaioa.com/k/jarimage.jar

Comes with source. Its double clickable and it also works in extracted form.

Thanks for even going through the trouble of doing that ;D

I have no idea what iw wrong with my code so … all I can do is rewrite it now that it is morning … the sun is shining outside … the coffe is plenty e.t.c. :wink:

Also thanks again again and again for striving to answer every single question :smiley: