0
votes

I'm currently trying to rotate a polygon using the Affine Transform class. Using the rotate method, the polygon's graphical representation updates, but the bounding box of the polygon doesn't update. How can I rotate the polygon in addition to updating it's coordinates?

1
What do you mean by the polygon's "bounding box"? Are you storing this as part of the polygon? - Code-Apprentice

1 Answers

4
votes

Create a new Shape instead of just rotating the polygon as your paint it. For example:

Polygon shape = new Polygon();
shape.addPoint(...);
....
Rectangle bounds = shape.getBounds();
AffineTransform transform = new AffineTransform();
transform.rotate(Math.toRadians(angle), bounds.width / 2, bounds.height / 2);

Path2D path = (shape instanceof Path2D) ? (Path2D)shape : new GeneralPath(shape);
Shape rotated = path.createTransformedShape( transform );
System.out.println(rotated.getBounds());