When calling rotate()...

What is rotate doing here?


Graphics2D g = (Graphics2D)strategy.getDrawGraphics();

// paint background and other images
...

// Now paint a rotated image.
g.rotate(...);
g.drawImage(...);
g.rotate(...);


// Display results
strategy.show();

Is the first rotate() call rotating the whole screen that has been draw, then draw the image I want rotated, and then rotate the whole screen back?
Isn’t this a big overhead? What’s the best way just to rotate one picture?

Bump.

Found it out.

Would you like to explain what you found out, it might help others eventually :slight_smile:

Kev

I’m not an expert here, but I believe it works as follows:

whenever an image is drawn, that drawing operation will take considerably more time because of the rotation. Setting the transform poses no considerable impact on rendering performance AFAIK. It is, however, wrong to rotate back after the image has been drawn. You should store the transform of the Graphics object before rotating, THEN rotate, draw, and SET the transform back to its initial state. Otherwise there may be floating point stuff which makes the whole screen SLIGHTLY rotated, and although that’s probably not visible, it IS an error, and could degrade rendering performance considerably (since everything would still have to be rotated by a very small amount).

There’s always a better way to do things, I guess :slight_smile:

But, the rotate/draw/rotate works as follows:

rotate(Math.PI); // this sets the "rotation mode", that is, anything that is drawn
                   // after this will be drawn according to that rotation. It doesn't rotate the whole canvas as I thought.

drawImage(...); // this simply draws the image on the canvas, but since you've
              // set the rotation angle then it will be drawn according to that.

rotate(-Math.PI); // this is a negative operation of the first rotate() call, it cancels out whatever you defined previously,
               //and images drawn after this will be drawn with no rotation.

Personally I don’t like this method. I’d like to apply some rotation-object when I draw a image, like: drawImage(image, angle);

You could use AffineTransform if you need to rotate the image around a specified point.

You could cast the Graphics object to java.awt.Graphics2D, then use


drawImage(Image img, AffineTransform xform, ImageObserver obs)

Then you will just have to specify the desired position of the image through an affine transform, i.e. transform.translate(xPos, yPos);

I believe that the procedure described above would be equivalent in terms of efficiency. Like I said, rotating takes a lot of time while manipulating the AffineTransform (involving, like, 6 numbers) is negligible in comparison.