# [java] rotating image with AffineTransform

This topic is 4844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to rotate an image using AffineTransform, but it always gets clipped to the dimensions of the original image. I've tried using AffineTransformOp and that works if I rotate an image past the right or bottom bound, but the image still gets clipped if its rotated past the left or top bound. Here's the rotate method:
    private Animation getRotatedImage(Animation anim, float deg) {
Image image = anim.getImage();
// set up the transform
AffineTransform transform = new AffineTransform();

// create a transparent (not translucent) image
BufferedImage newImage = gc.createCompatibleImage(
image.getWidth(null),
image.getHeight(null),

AffineTransformOp transformOp = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
Rectangle2D bounds = transformOp.getBounds2D(newImage);
newImage = transformOp.filter(newImage, null);
// draw the transformed image
Graphics2D g = (Graphics2D)newImage.getGraphics();
g.drawImage(image, transform, null);
g.dispose();
Animation newAnim = new Animation();
return newAnim;
}

Thanks for any help [Edited by - nataku92 on August 27, 2005 2:22:06 AM]

##### Share on other sites
the dimensions of your image that you are rendering to are too small.

take two playing cards, one stacked on top of the other. Rotate the top card. Notice how the corners stick out over the edges? Your image is using the bounds of the bottom card while trying to render the top card.

You need to be able to figure out how large to make your image in order to fit the rotated image inside. It's pretty simple trigonometry, actually.

edit: oh, hell, I'll just give it to you

w' = w*cos(A)+h*sin(A)
h' = w*sin(A)+h*cos(A)

where w, h are the dimensions of the original image, A is the angle of rotation, and w', h' are the dimensions of the rotated image.

edit2: don't forget to transform the center of the original image to the origin of the image, and then transform the center of the rotated image to the center of the new image. If you want a complete transform matrix:

c = cos(A)s = sin(A)u = w*c + h*sv = w*s + h*c| c s (u*c+v*s-w)/2 || s c (u*s+v*c-h)/2 || 0 0       1       |

You only have to set the top two rows in the The AffineTransform class. The bottom row never changes.

[Edited by - capn_midnight on August 12, 2005 9:05:05 AM]

##### Share on other sites
Quote:
 Original post by capn_midnightedit2: don't forget to transform the center of the original image to the origin of the image, and then transform the center of the rotated image to the center of the new image. If you want a complete transform matrix:c = cos(A)s = sin(A)u = w*c + h*sv = w*s + h*c| c s (u*c+v*s-w)/2 || s c (u*s+v*c-h)/2 || 0 0 1 |You only have to set the top two rows in the The AffineTransform class. The bottom row never changes.

um...could you please explain how that works and how implement it into code? I'm still in eighth grade so I can't understand that yet -.-

Thanks

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 15
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633352
• Total Posts
3011483
• ### Who's Online (See full list)

There are no registered users currently online

×