java - 绕其轴旋转三角形

标签 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

最佳答案

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

关于java - 绕其轴旋转三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52279289/

相关文章:

java - 如何人为触发操作命令?

java - 摆脱潜在的僵局

java - 在 ScrollPane 中显示表单

java - 如何将返回 null 的 JTextField 转换为不同的字符串值?

java - 自定义异常(exception)或空列表

java - 通过避免创建对象进行优化

java - 如何在Opengl ES中获得一个像素的透明颜色?

java - 有没有办法只在tomcat中部署一个项目而保留其他项目

java - 如何检测ThreadPool中是否有空闲线程?

java - Swing 工作线程 : Inner class vs passing parameter