如何划分多行看起来像划分面板Java的扇区?

时间:2017-03-02 17:42:41

标签: java jpanel graphics2d

我想知道如何绘制多条线,看起来像是将面板划分为扇区。

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


}
谢谢。

1 个答案:

答案 0 :(得分:1)

你可能有几种方法可以做到这一点,但对我来说,这看起来就像一个轮子上的辐条,而且因为我知道如何计算一个圆点,这就是我回归的地方。

我们知道什么:

  • 我们知道面积(面板的大小)
  • 我们想要的细分/细分数
  • 如何计算圆上的点

因此,利用这些基本信息,我们可以设计出在中心点周围均匀绘制分割数所需的角度增量

Overshoot

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

    }

}

现在,如果你不想让这些行“过冲”,那就改变

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

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

Undershoot

现在,如果你希望它看起来有点“更好”,你可以考虑添加

RenderingHints hints = new RenderingHints(
        RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHints(hints);
在绘制任何内容之前

进入paintComponent方法