使用仿射变换来旋转多边形

时间:2013-04-01 00:42:32

标签: java polygon

我目前正在尝试使用仿射变换类旋转多边形。使用rotate方法,多边形的图形表示会更新,但多边形的边界框不会更新。除了更新它的坐标外,如何旋转多边形?

1 个答案:

答案 0 :(得分:4)

创建一个新的Shape而不是仅仅旋转多边形作为绘制它。例如:

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());