BufferedImage.getSubImage() ?

I’m using getSubImage() to get a BufferedImage area of an image and it draws it very slowly, here’s what I’m doing:


// initialize.
		BufferedImage sprites = null;
		try {
			sprites = ImageIO.read(getClass().getResource("sprites.gif"));
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		BufferedImage badGuy = sprites.getSubimage(82,0,19,25);
		BufferedImage goodGuy = sprites.getSubimage(56,0,26,13);
		BufferedImage bullet = sprites.getSubimage(41,25,16,5);
		BufferedImage explosion = sprites.getSubimage(0,0,32,31);

...

// start game loop
...
// update world
...
// draw world
g.drawImage(...); // for each of those 4 images.



pretty straight forward, but the framerate drops down to 1 fps, how can I speed things up, or accomplish what I’m trying to do in a different way?

The getSubImage() call disables the ability to hardware accelerate the image.

You have a choice: either create a bunch of images (one per character) or
just leave the single image with all sprites and use one of the drawImage calls
which allows you to specify a source (and destination if needed) rectangle.

So basically instead of a subimage you’ll have a Rectangle (representing
the sprite coordinates in the one large sprite image) associated
with a character.

Hope this helps…

Thanks,
Dmitri
Java2D Team

Alternatively you could create another image and copy the pixels from the subbed image into the new image and return the new image.
You get the sub image and still have acceleration.

Yes, both good ideas, have opened up many possibilites for me. Thanks for the help :slight_smile: