Swing Shape Properties

I have an image and I can draw a rectangle on the image as follows:

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 link to a newly rotated rectangle? I need the properties of a rotating rectangle, such as Point, Width, Height ....

I do not see that Graphics2D has a getShape () method? In addition, the rectangle passed to Graphics2D when invoking the draw (Rectangle) method does not change.

Any ideas?

+1
source share
1 answer

You can do this using the AffineTransform class.

AffineTransform transform = new AffineTransform();
transform.rotate(Math.PI/2);
Shape transformed = transform.createTransformedShape(shape);

, . , :

transform.transform(point_before, point_after);
+1

Source: https://habr.com/ru/post/1792098/


All Articles