尝试绘制多个未填充的三角形,在Java中旋转中心点。绘制一个三角形,将点旋转一定的半径,然后绘制另一个三角形。
int rad = 10 //Radius between the triangles
int num = 20 //Number of triangles
for (int i = 0; i < num; i++){
// (250,250) would be the center
int[] xPoints = (250,175,325) //X points of the first triangle
int[] yPoints = (250,100,100) //Y points of the first triangle
g.drawPolygon(xPoints,yPoints,3); //Paint the shape
}
当然我的代码只打印第一个三角形,因为我不确定如何旋转这些点。我已经四处寻找并找到了一些触发器,但我并不是真的理解它。是否有一种简单的方法来旋转每个点?感谢。
答案 0 :(得分:5)
有一种简单的方法可以旋转每个点吗?
使用为您做几何图形的AffineTranform
。
在标有affinetransform的帖子中可以看到一些示例。特别是mine,Trashgod,MadProgrammer&amp; HovercraftFullOfEels(如果我忘记了一些做过一些不错的例子,我很抱歉。)
答案 1 :(得分:1)
Graphics2d
对象包含AffineTransform
,并且有一个调用将其直接设置为关于给定点的旋转。
使用它时,您经常(并非总是)想先保存变换的副本然后将其恢复,以便g
的下一次使用具有原始变换而不是预乘或后乘版本:
AffineTransform savedTransform = g.getTransform();
g.rotate(theta, x_center_of_rotation, y_center_of_rotation);
g.setTransform(savedTransform);