Fast sprite rotation

Hi,
Which is the fastest way to rotate an image in Java2D?
I want to make a game if full screen exclusive mode where I will use heavily sprite rotation and I need to do it fast.I dont want to create pre-rotated images but to rotate the images in real time using code and with any angle.
I know about the Affine Trasformation,bt is it hardware accelerated?Or is it to slow and is better to use JOGL instead?
Thank u

The only thing I need to know if is using the class AffineTransform in java the jvm will make the transformation using the hardware acceleration or not.Reading the java docs i didn’t find this information.So if Java rotates the image without using the hardware acceleration it means that is slow in this case is better to use JOGL since JOGL is using OpenGL anc in this case I can rotate objects using the hardware acceleration.

Ok I foudn the answer by myself:
avoid using AffineTrasnform and rotate the coordinates before drawing and the rotate back, this is faster



g.rotate(theta, x, y);
g.drawImage(x,y);
g.rotate(-theta,x,y);



Graphics2D.rotate() just invokes AffineTransform.rotate() on the currently active/set AffineTransform object stored in the Graphics2D(SunGraphics2D). The Graphics2D also provides a drawImage() taking an AffineTransform. Maybe use that. Avoids having to compute sin/cos twice when reverting the rotation, like you did.


AffineTransform rotation = new AffineTransform(); // <- store this as a field somewhere
...
// now in the render method do this:
rotation.setToIdentity();
rotation.rotate(theta, x, y);
g.drawImage(img, rotation, (ImageObserver) null);

@KaiHH: text book case of premature optimisation :point:

It makes the code less simple (simplicity is important) and won’t have significant impact on performance.

When I do this in my game I think I also had to use translate() to translate the x and y positions so it rotated around the correct coordinates.

Scratch that, I didn’t use translate…here’s what I did: This will make the object spin 45 degrees/frame

init:
AffineTransform saveAT = ((Graphics2D) g).getTransform();
rotateAngle = 0;

in draw:
((Graphics2D) g).rotate(Math.toRadians(rotateAngle), x + width / 2, y + height / 2);
g.fillRect(x, y, width, height);
((Graphics2D) g).setTransform(saveAT);

in update:
if(rotateAngle < 360)
rotateAngle += 45;
else rotateAngle = 0;