java画个半径为1地圆_java - 绘制一个半径为圆的圆并围绕边缘指向 - 堆栈内存溢出...

圆上的点可以指定为角度θ的函数:

x = a + r cos(θ)

y = b + r sin(θ)

这里,示出了2π/ 8的增量。

附录:正如@ChristofferHammarström在评论中所建议的那样,这个修改过的例子减少了原始数字中的幻数 。 所需的点数成为构造函数的参数。 它还使渲染适应容器的大小。

aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9hQXlyZy5wbmc=

/** @see https://stackoverflow.com/questions/2508704 */

public class CircleTest extends JPanel {

private static final int SIZE = 256;

private int a = SIZE / 2;

private int b = a;

private int r = 4 * SIZE / 5;

private int n;

/** @param n the desired number of circles. */

public CircleTest(int n) {

super(true);

this.setPreferredSize(new Dimension(SIZE, SIZE));

this.n = n;

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;

g2d.setRenderingHint(

RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g2d.setColor(Color.black);

a = getWidth() / 2;

b = getHeight() / 2;

int m = Math.min(a, b);

r = 4 * m / 5;

int r2 = Math.abs(m - r) / 2;

g2d.drawOval(a - r, b - r, 2 * r, 2 * r);

g2d.setColor(Color.blue);

for (int i = 0; i < n; i++) {

double t = 2 * Math.PI * i / n;

int x = (int) Math.round(a + r * Math.cos(t));

int y = (int) Math.round(b + r * Math.sin(t));

g2d.fillOval(x - r2, y - r2, 2 * r2, 2 * r2);

}

}

private static void create() {

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.add(new CircleTest(9));

f.pack();

f.setVisible(true);

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

create();

}

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值