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 …
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?