I need to know how to flip a series of PNG images horizontally only when a certain condition is met. To be more eleborate, I have the images and code for a fighting game and it uses 60 or so sprites which need to be flipped horizontally when the two fighters switch sides (for example: one character jumps over the other, they will then need to be flipped so they still face each other). Any insight that can be provided would be greatly appreciated.
-RT
Assuming that you’re using Java2D, you should be able to set a scale of -1 on the axis you want to flip on to do this (either via an AffineTransform, or a Graphics.drawImage with a negative width/height).
However I’m not sure how fast that’ll be compared to normal drawing, it might be faster to create mirrored sprites at load time and instead of doing it on the fly.
affine tranforamtion disables image accelaration as I know, maybe it’s better to store fliped images as Tang said
edit: btw. read your inbox, sent you a pm
The easiest way I think is to use drawImage and swap x coordinates, like this:
g.drawImage (im, im.getWidth(this), 0, 0, im.getHeight(this), 0, 0, im.getWidth(this), im.getHeight(this), this);
If you want more performance, do this for all your images first and save them on BufferedImages, so to have them all already flipped in memory.
More info on http://www.javaworld.com/javaworld/javatips/jw-javatip32.html