有可能是你能做到这一点的几种方式,但对我来说,这看起来像一个轮辐,因为我知道如何计算一个点一个圆圈,这就是我会回落到。
什么做的,我们知道:
- 我们了解该地区(面板的尺寸)
- 段的数量,我们希望
- 如何在一个圆圈计算点/司
因此,通过这些基本信息,我们可以设计出需要在中心点周围均匀划分分割数所需的角度增量
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);
现在,如果你希望它看起来有点“更好”,你会发现ð考虑加入
RenderingHints hints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHints(hints);
到paintComponent
方法,你画什么之前
我首先想到的是,基本上是计算各地根据给定角圆点,并在二者之间画线 - 听起来很蠢,但我会给你更好的基本控制过程。从概念上讲,像[此为示例](http://stackoverflow.com/questions/30228146/drawing-a-line-maximum-point/30228270#30228270) – MadProgrammer