Sign in to follow this  

[java] Image not drawing?

This topic is 2670 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I"m making a simple game with Java's Graphics2D api, and everything was working fine until I started cleaning up my code lol. I have this art loader class which loads sprites into a map.

public class ArtLoader {
private BufferedImage sprites;
private TreeMap<Integer, BufferedImage> spriteCache;
private static ArtLoader instance = null;

protected ArtLoader() {
try {
sprites = ImageIO.read(new File("sprites.png"));
spriteCache = new TreeMap<Integer, BufferedImage>();
} catch(Exception e) {
System.out.println("Failed to load sprite sheet");
}
}

public BufferedImage getSprite(int id) {
try {
BufferedImage r = spriteCache.get(id);
} catch (Exception e) {
spriteCache.put(id, sprites.getSubimage(id*16, 0, 16, 16));
}

return spriteCache.get(id);
}

public static ArtLoader getInstance() {
if(instance == null) {
instance = new ArtLoader();
}

return instance;
}
}


Then in my Player class I have something like


BufferedImage sprite = artLoader.getSprite(2);
setSprite(sprite);


However, when it comes time to draw the sprite, nothing is drawn. No runtime errors, no compile errors, nothing. I have tested the x,y coordinates to see if I was drawing off screen, and it doesn't look like that's the problem. Unless AffineTransform.translate() translates relatively and not absolutely, since I have one instance for the life of the class.

I just tried messing around with the AffineTransform, and it doesn't look like that's the problem. Something else?



Share this post


Link to post
Share on other sites
So it looks like it's a problem with my art loader. When I try to print the sprite.getHight() I get a null pointer exception. I just don't know what's causing it.

After some more poking around the error is happening when I call spriteCache.get(), it's returning null. Any idea why?

Share this post


Link to post
Share on other sites
Is that the full code listing for the ArtLoader class?

Based on the code posted; nothing is ever added to the TreeMap.
So "spriteCache.get()" will always return null.

Share this post


Link to post
Share on other sites
Ohh, I see. Thanks, got it working.

	public BufferedImage getSprite(int id) {
BufferedImage s = spriteCache.get(id);

if(s != null) {
return s;
} else {
s = sprites.getSubimage(id*16, 0, 16, 16);
spriteCache.put(id, s);
return s;
}
}

Share this post


Link to post
Share on other sites

This topic is 2670 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this