Jump to content
  • Advertisement
Sign in to follow this  
CirdanValen

[java] Image not drawing?

This topic is 2976 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!