围绕其轴旋转三角形

时间:2018-09-11 15:21:04

标签: java swing

我里面有一个圆和一个三角形。我需要该图绕其轴旋转,并且每次旋转一定角度时,都应该对其进行绘画,以总体上获得某种“装饰”(在下面的屏幕上)。我一直在尝试使用Graphics2D,但是情况很糟。我该怎么办?

代码:

import org.omg.CORBA.TIMEOUT;
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

public class Main extends JPanel {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Laboratorna 1");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(new Main());
        frame.setSize(1200, 800);
        frame.setVisible(true);
    }

    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);
        g.drawLine(150+500, 0, 20+500, 225);
        g.drawLine(150+500, 0, 280+500, 225);
        g.drawLine(20+500, 225,280+500, 225);
        g.setColor(Color.RED);

    }
}

enter image description here

2 个答案:

答案 0 :(得分:1)

诀窍是对每个角度执行仿射变换。此外,您必须为每个变换定义与圆心相等的枢轴点。以下对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 -------------------------------------------------------------------
   }

输出为:

enter image description here

答案 1 :(得分:0)

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

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

enter image description here