Jump to content
  • Advertisement
Sign in to follow this  
nataku92

[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.

If you intended to correct an error in the post then please contact us.

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();
	    transform.rotate(Math.toRadians(deg),image.getWidth(null) / 2,0);

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

        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();
        newAnim.addFrame(newImage, 1000);
        return newAnim;
    }
Thanks for any help [Edited by - nataku92 on August 27, 2005 2:22:06 AM]

Share this post


Link to post
Share on other sites
Advertisement
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*s
v = 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 this post


Link to post
Share on other sites
Quote:
Original post by capn_midnight
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*s
v = 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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!