Shape properties from an Image in Swing
I have an Image and I'm able to draw a rectangle on the image like this:
Rectangle rect = new Rectangle(x,y,width,height);
graphics2D.draw(rect);
Then I rotate 开发者_运维问答the Image and the drawn rectangle also rotates as expected. But now how can I get a reference to the newly rotated Rectangle? I need the properties of the rotated Rectangle like Point, width, height....
I don's see that Graphics2D has a method like getShape()? Also, the rectangle passed to the Graphics2D when calling its draw(Rectangle) method doesn't change.
Any ideas?
You can do it using AffineTransform class.
AffineTransform transform = new AffineTransform();
transform.rotate(Math.PI/2);
Shape transformed = transform.createTransformedShape(shape);
But it would yield you only the shape, which you can use for drawing. If you need exact points of rotated rectangle, you'll need to transform each point separately:
transform.transform(point_before, point_after);
精彩评论