// The class for drawing arcs on a panel
1 class ArcsPanel extends JPanel {
2 @Override
3 protected void paintComponent(Graphics g) {
4 super.paintComponent(g);
5
6 int xCenter = getWidth() / 2;
7 int yCenter = getHeight() / 2;
8 int radius = (int)(Math.min(getWidth(), getHeight()) * 0.4);
9
10 int x = xCenter - radius;
11 int y = yCenter - radius;
12
13 g.fillArc(x, y, 2 * radius, 2 * radius, 0, 30);
14 g.fillArc(x, y, 2 * radius, 2 * radius, 90, 30);
15 g.fillArc(x, y, 2 * radius, 2 * radius, 180, 30);
16 g.fillArc(x, y, 2 * radius, 2 * radius, 270, 30);
17 }
18}
我不明白第 8 行。获取半径公式的解释。他从哪里得到0.4的半径公式?我没有找到公式。 求圆弧半径是不同的。
我从《Java编程入门,综合版(第9版); list 13.4示例;页码:489(522/1947)》中找到的
最佳答案
0.4
似乎是一个相对值,意思是“角半径是宽度和高度中较小者的 40%”。
编辑:这是一个完美的例子,为什么未注释的代码和魔术常量是不好的。通常,您会使用变量或参数而不是魔术常量。
关于java - java中圆弧的半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29659674/