1

I'm working on a type of image editor which includes snapping to other elements as a feature. The snapping works by using the image width and height.

However, after an AffineTransform.rotate() the image width and height are no longer the actual bounds of the rendered image and thus the snapping fails.

What's the best way of determining the width and height of the rectangular area that encompasses the rotated image?

Jargon
  • 420
  • 4
  • 14
  • This question seems like it was asked in the last day or so. Was that you? – Andrew Thompson Jun 12 '13 at 11:06
  • ... sorry, meant [this](http://stackoverflow.com/questions/4156518/rotate-an-image-in-java) (3rd time lucky...where's bed again? Better check google-maps...) – MadProgrammer Jun 12 '13 at 11:26
  • 1
    Try `affineTransform.createTransformedShape(new Rectangle(width, height)).getBounds()` – johnchen902 Jun 12 '13 at 11:32
  • @AndrewThompson This is the first time I've asked a question on stackoverflow :) I did do a search for similar questions but couldn't find an appropriate one. – Jargon Jun 12 '13 at 11:36
  • @johnchen902 Ok, that suggestion does work but I need to maintain a `float` level of accuracy and `Rectangle` only accepts `int`s. Any ideas? – Jargon Jun 12 '13 at 11:56
  • 1
    `affineTransform.createTransformedShape(new Rectangle2D.Double(width, height)).getBounds2D()` – johnchen902 Jun 12 '13 at 11:59
  • @johnchen902 Ok, that works perfectly :) If you submit it as an answer I'll accept it. – Jargon Jun 12 '13 at 12:34

1 Answers1

0

For int:

affineTransform.createTransformedShape(new Rectangle(width, height))
        .getBounds();

For double:

affineTransform.createTransformedShape(
        new Rectangle2D.Double(width, height)).getBounds2D();
johnchen902
  • 9,531
  • 1
  • 27
  • 69