Java 围绕其轴旋转三角形

Java 围绕其轴旋转三角形,java,swing,Java,Swing,我有一个圆和一个三角形在里面。我需要这个图形绕着它的轴旋转,每次它以一定的角度旋转时,它都应该被画出来,整体上得到某种“装饰”(在下面的屏幕上)。我一直在尝试使用Graphics2D,但效果不好。我该怎么做 代码: 诀窍是对每个角度执行仿射变换。此外,还必须为每个变换定义轴点,该轴点等于圆的中心。paintComponent方法的以下修改可能会完成这项工作 public void paintComponent(Graphics g){ int num_2 = 8;

我有一个圆和一个三角形在里面。我需要这个图形绕着它的轴旋转,每次它以一定的角度旋转时,它都应该被画出来,整体上得到某种“装饰”(在下面的屏幕上)。我一直在尝试使用
Graphics2D
,但效果不好。我该怎么做

代码:


诀窍是对每个角度执行仿射变换。此外,还必须为每个变换定义轴点,该轴点等于圆的中心。paintComponent方法的以下修改可能会完成这项工作

    public void paintComponent(Graphics g){

       int num_2 = 8;
       int bigOval_h = 300, bigOval_w = 300;

       g.setColor(Color.BLUE);
       g.drawOval(0 + 500, 0, bigOval_h, bigOval_w);
       // REMOVE -------------------------------------------
       // g.drawLine(150+500, 0, 20+500, 225);
       // g.drawLine(150+500, 0, 280+500, 225);
       // g.drawLine(20+500, 225,280+500, 225);
       // REMOVE -------------------------------------------
       g.setColor(Color.RED);

       // ADD -------------------------------------------------------------------
       // Create, transform and draw the lines
       Line2D lin1 = new Line2D.Float(150f + 500f, 0f, 20f + 500f, 225f);
       Line2D lin2 = new Line2D.Float(150f + 500f, 0f, 280f + 500f, 225f);
       Line2D lin3 = new Line2D.Float(20f + 500f, 225f, 280f + 500f, 225f);
       double pivotX = 500.0 + bigOval_w / 2.0; // center of the circle (x)
       double pivotY = 0.0 + bigOval_h / 2.0;   // center of the circle (y)
       for (int i = 0; i < num_2; i++) {
          AffineTransform affineTransform = AffineTransform.getRotateInstance(Math.toRadians(360.0 / num_2 * i), pivotX, pivotY);
          ((Graphics2D)g).draw(affineTransform.createTransformedShape(lin1));
          ((Graphics2D)g).draw(affineTransform.createTransformedShape(lin2));
          ((Graphics2D)g).draw(affineTransform.createTransformedShape(lin3));
       }
       // ADD -------------------------------------------------------------------
   }
公共组件(图形g){
int num_2=8;
int bigOval_h=300,bigOval_w=300;
g、 setColor(Color.BLUE);
g、 drawOval(0+500,0,bigOval_h,bigOval_w);
//除去-------------------------------------------
//g.拉线(150+500,0,20+500,225);
//g.拉线(150+500,0280+500,225);
//g.拉线(20+500225280+500225);
//除去-------------------------------------------
g、 setColor(Color.RED);
//加-------------------------------------------------------------------
//创建、变换和绘制线条
Line2D lin1=新的Line2D.Float(150f+500f、0f、20f+500f、225f);
Line2D lin2=新的Line2D.Float(150f+500f、0f、280f+500f、225f);
Line2D lin3=新的Line2D.Float(20f+500f、225f、280f+500f、225f);
双枢轴x=500.0+bigOval_w/2.0;//圆心(x)
双枢轴=0.0+双椭圆h/2.0;//圆心(y)
对于(int i=0;i
输出为:


定义多边形的一种方法是半径、旋转和边数。三角形是一个三边多边形。因此,三角形类可以简单地保存半径和旋转,然后计算多边形的三个顶点,以便围绕中心点绘制

public class Triangle {
    private final int radius;
    private final double rotation;

    public Triangle(int radius, double rotation) {
        this.radius = radius;
        this.rotation = rotation;
    }

    public void paintComponent(Graphics2D g, Point center) {    
        int[] xVertices = new int[3];
        int[] yVertices = new int[3];

        xVertices[0] = (int) (center.getX() - (Math.cos(rotation) * radius));
        yVertices[0] = (int) (center.getY() - (Math.sin(rotation) * radius));
        xVertices[1] = (int) (center.getX() - (Math.cos(Math.PI * 0.66667 + rotation) * radius));
        yVertices[1] = (int) (center.getY() - (Math.sin(Math.PI * 0.66667 + rotation) * radius));
        xVertices[2] = (int) (center.getX() - (Math.cos(Math.PI * 1.33333 + rotation) * radius));
        yVertices[2] = (int) (center.getY() - (Math.sin(Math.PI * 1.33333 + rotation) * radius));

        Polygon polygon = new Polygon(xVertices, yVertices, 3);
        g.setColor(Color.RED);
        g.drawPolygon(polygon);
    }
}
这将给出两个旋转PI弧度的三角形

Triangle t = new Triangle(100, 0.0);
t.paintComponent((Graphics2D)g, new Point(250,250));
Triangle t2 = new Triangle(100, Math.PI);
t2.paintComponent((Graphics2D)g, new Point(250,250));

听起来像是一道数学题。但我不清楚你在问什么。你想解决的确切问题是什么?“它坏了”并不是一个很好的解释。请阅读并告诉我们“它坏了”(你做了什么,你期望什么,你得到了什么)。通常这是一个线性代数问题,你旋转三角形顶点的x,y坐标。。
Triangle t = new Triangle(100, 0.0);
t.paintComponent((Graphics2D)g, new Point(250,250));
Triangle t2 = new Triangle(100, Math.PI);
t2.paintComponent((Graphics2D)g, new Point(250,250));