Java - 如何填充三角形并旋转它? (多边形)

时间:2023-02-09 15:42:42

So I got this which is a triangle, but how do I color it in? I am new to Java so bear with me. I also want later to copy it and paste it next to it and then rotate it on it's head. The figure I am trying to build is a Kite (as you can see it's mainly build in different parts of traingles).

所以我得到的这是一个三角形,但我该如何着色呢?我是Java的新手,所以请耐心等待。我也想稍后将其复制并粘贴在它旁边,然后将它旋转到它的头上。我想要建立的图是一个风筝(你可以看到它主要构建在traingles的不同部分)。

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    this.setBackground(Color.yellow);

    //this is where the triangle starts
    g.setColor(Color.RED);
    g.drawPolygon(new int[] {40, 80, 120}, new int[] {100, 20, 100}, 3);

Thanks in advance!!!

提前致谢!!!

1 个解决方案

#1


1  

What you can do is cast the graphics object g to a 2D grphics object. For example:

你可以做的是将图形对象g转换为2D grphics对象。例如:

Graphics2D g2 = (Graphics2D) g;

will allow you to use the tools included in the Graphics2D package, which you will need to import. Then, you can create the triangle:

将允许您使用Graphics2D包中包含的工具,您需要将其导入。然后,您可以创建三角形:

Polygon tri = new Polygon(new int[] {40, 20, 80}, new int[] {100, 20, 100}, 3);

Next, you need to rotate the triangle THETA degrees clockwise arround it's center and create it, filled:

接下来,您需要将三角形THETA度旋转到它的中心并创建它,填充:

g2.rotate(Math.toRadians(THETA), (140 / 3), (220 / 3));
g2.fill(tri);

This will create the triangle specified by coordinates {40, 20, 80} and {100, 20, 100}, rotated by THETA degrees. The reason I specified the 2 extra parameters of rotate are because it would rotate around (0, 0) if they were not specified. 140 / 3 is just the average of the x coordinates, and 220 / 3 is the average of the y coordinates. If you know your geometry well, you will recognize this as the centroid of a triangle. If you are rotating by the same amount each time, you could just convert THETA to radians by hand. There is, however, another way to do this. You can rotate each point around the centroid of the triangle and then draw the polygon defined by these points. To do this, it is easiest to derive a formula from the general rotation matrix. Because this is Stack Overflow, I cannot very easily show you the derivation of the formula without LaTex support. In the end, the point (x, y) can be rotated around the center of the triangle (xc, yc) , by THETA degrees. using the formula rX = ((x - xc) * cos(THETA)) - ((y - yc) * sin(THETA) + xc to get the x coordinate of the rotated point, and rY = ((x - xc) * sin(THETA)) + ((y - yc) * cos(THETA)) + yc. I trust that you can adapt this to java, but if you need help, tell me. Remember, Math.cos() and Math.sin() take their parameter in radians, not degrees. You will also need to round it, because the Math.toRadians() will not return an exact value, so most values supplied to Math.cos() or Math.sin() will evaluate to 0.

这将创建由坐标{40,20,80}和{100,20,100}指定的三角形,旋转THETA度。我指定旋转的2个额外参数的原因是因为如果未指定旋转,它将围绕(0,0)旋转。 140/3只是x坐标的平均值,220/3是y坐标的平均值。如果你很好地了解几何体,你会发现它是三角形的质心。如果每次旋转的数量相同,则可以手动将THETA转换为弧度。但是,有另一种方法可以做到这一点。您可以围绕三角形的质心旋转每个点,然后绘制由这些点定义的多边形。要做到这一点,最简单的方法是从一般旋转矩阵中导出一个公式。因为这是Stack Overflow,所以我不能很容易地向您展示没有LaTex支持的公式的推导。最后,点(x,y)可以围绕三角形的中心(xc,yc)旋转THETA度。使用公式rX =((x - xc)* cos(THETA)) - ((y - yc)* sin(THETA)+ xc得到旋转点的x坐标,rY =((x - xc) * sin(THETA))+((y - yc)* cos(THETA))+ yc。我相信你可以将它改编为java,但是如果你需要帮助,请告诉我。记住,Math.cos()和Math .sin()以弧度为单位获取参数,而不是度数。您还需要对其进行舍入,因为Math.toRadians()不会返回精确值,因此大多数值都提供给Math.cos()或Math.sin( )将评估为0。

#1


1  

What you can do is cast the graphics object g to a 2D grphics object. For example:

你可以做的是将图形对象g转换为2D grphics对象。例如:

Graphics2D g2 = (Graphics2D) g;

will allow you to use the tools included in the Graphics2D package, which you will need to import. Then, you can create the triangle:

将允许您使用Graphics2D包中包含的工具,您需要将其导入。然后,您可以创建三角形:

Polygon tri = new Polygon(new int[] {40, 20, 80}, new int[] {100, 20, 100}, 3);

Next, you need to rotate the triangle THETA degrees clockwise arround it's center and create it, filled:

接下来,您需要将三角形THETA度旋转到它的中心并创建它,填充:

g2.rotate(Math.toRadians(THETA), (140 / 3), (220 / 3));
g2.fill(tri);

This will create the triangle specified by coordinates {40, 20, 80} and {100, 20, 100}, rotated by THETA degrees. The reason I specified the 2 extra parameters of rotate are because it would rotate around (0, 0) if they were not specified. 140 / 3 is just the average of the x coordinates, and 220 / 3 is the average of the y coordinates. If you know your geometry well, you will recognize this as the centroid of a triangle. If you are rotating by the same amount each time, you could just convert THETA to radians by hand. There is, however, another way to do this. You can rotate each point around the centroid of the triangle and then draw the polygon defined by these points. To do this, it is easiest to derive a formula from the general rotation matrix. Because this is Stack Overflow, I cannot very easily show you the derivation of the formula without LaTex support. In the end, the point (x, y) can be rotated around the center of the triangle (xc, yc) , by THETA degrees. using the formula rX = ((x - xc) * cos(THETA)) - ((y - yc) * sin(THETA) + xc to get the x coordinate of the rotated point, and rY = ((x - xc) * sin(THETA)) + ((y - yc) * cos(THETA)) + yc. I trust that you can adapt this to java, but if you need help, tell me. Remember, Math.cos() and Math.sin() take their parameter in radians, not degrees. You will also need to round it, because the Math.toRadians() will not return an exact value, so most values supplied to Math.cos() or Math.sin() will evaluate to 0.

这将创建由坐标{40,20,80}和{100,20,100}指定的三角形,旋转THETA度。我指定旋转的2个额外参数的原因是因为如果未指定旋转,它将围绕(0,0)旋转。 140/3只是x坐标的平均值,220/3是y坐标的平均值。如果你很好地了解几何体,你会发现它是三角形的质心。如果每次旋转的数量相同,则可以手动将THETA转换为弧度。但是,有另一种方法可以做到这一点。您可以围绕三角形的质心旋转每个点,然后绘制由这些点定义的多边形。要做到这一点,最简单的方法是从一般旋转矩阵中导出一个公式。因为这是Stack Overflow,所以我不能很容易地向您展示没有LaTex支持的公式的推导。最后,点(x,y)可以围绕三角形的中心(xc,yc)旋转THETA度。使用公式rX =((x - xc)* cos(THETA)) - ((y - yc)* sin(THETA)+ xc得到旋转点的x坐标,rY =((x - xc) * sin(THETA))+((y - yc)* cos(THETA))+ yc。我相信你可以将它改编为java,但是如果你需要帮助,请告诉我。记住,Math.cos()和Math .sin()以弧度为单位获取参数,而不是度数。您还需要对其进行舍入,因为Math.toRadians()不会返回精确值,因此大多数值都提供给Math.cos()或Math.sin( )将评估为0。