如何绘制多条线看起来像划分面板的扇区 Java?

How do I draw multiple line looks like dividing the sectors of a panel Java?

我想知道如何绘制看起来像将面板分成扇区的多条线。

This is the example of lines that I wanted to draw

下面是到目前为止我已经弄清楚的代码,但它只能在面板上绘制 "x" 线和一条水平线。我想知道我是怎么画出像上图这样的线条的。

public void paintComponent(Graphics g){
    super.paintComponent(g);

    Graphics2D graphic = (Graphics2D)g;
    Insets insets = getInsets();
    graphic.setStroke(new BasicStroke(5.0f));
    graphic.draw(new Line2D.Double(insets.left, insets.top,getWidth()-insets.right, getHeight()-insets.bottom));
    graphic.draw(new Line2D.Double(insets.left,getHeight()-insets.bottom,getWidth()-insets.right,insets.top));
    graphic.drawLine(0,200,800,200);


}

谢谢。

您可能有几种方法可以做到这一点,但对我来说,这看起来像是轮子上的辐条,而且由于我知道如何计算一个点和一个圆,所以我会回到这里。

我们知道什么:

  • 我们知道面积(面板的大小)
  • 我们要的segments/divisions个数
  • 如何计算圆上的一个点

因此,根据这些基本信息,我们可以设计出围绕中心点均匀绘制分区数所需的角度增量

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        protected Point2D pointAt(double radians, double radius) {
            double x = radius * Math.cos(radians);
            double y = radius * Math.sin(radians);

            return new Point2D.Double(x, y);
        }

        protected Point2D translate(Point2D point, Point2D to) {
            Point2D newPoint = new Point2D.Double(point.getX(), point.getY());
            newPoint.setLocation(point.getX() + to.getX(), point.getY() + to.getY());
            return newPoint;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.BLACK);

            double startAngle = 0;
            double divisions = 12;
            double delta = 360.0 / divisions;

            int centerX = getWidth() / 2;
            int centerY = getHeight() / 2;
            int radius = Math.min(centerX, centerY) * 2; // Overshoot the visible bounds

            Point2D centerPoint = new Point2D.Double(centerX, centerY);
            double angle = startAngle;
            for (int index = 0; index < divisions; index++) {
                Point2D point = pointAt(Math.toRadians(angle), radius);
                point = translate(point, centerPoint);
                g2d.draw(new Line2D.Double(centerPoint, point));
                angle += delta;
            }

            g2d.dispose();
        }

    }

}

现在,如果您不想使用行 "overshoot",请更改

int radius = Math.min(centerX, centerY) * 2; // Overshoot the visible bounds

int radius = Math.min(centerX, centerY);

现在,如果你想让它看起来有点"nicer",你可以考虑添加

RenderingHints hints = new RenderingHints(
        RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHints(hints);

进入 paintComponent 方法,然后再绘制任何东西