如何使用 Graphics#fillOval 以中心为参考在中心绘制一个随机圆
How to draw a random circle in center using the center as reference with Graphics#fillOval
目前点击按钮时,圆会在g.fillOval(getWidth()/2 - 50, getHeight()/2 - 50, radius, radius);
处绘制
我这里有:
private class DrawPanel extends JPanel {
private int radius;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(getWidth()/2 - 50, getHeight()/2 - 50, radius, radius);
}
你的问题不是很清楚,你贴的代码有点多,我概括一下吧。
当你用Graphics#fillOval
画圆时,参考点是椭圆所刻入的正方形的左上角。
因此,如果您想使用中心作为参考,给定半径 r,您应该绘制:
(xCenter - r, yCenter - r, r*2, r*2)
此外,由于它是一个圆圈,请考虑使用 Graphics2D
并启用 Antialising。
这是执行此操作的文档:
https://docs.oracle.com/javase/tutorial/2d/advanced/quality.html
目前点击按钮时,圆会在g.fillOval(getWidth()/2 - 50, getHeight()/2 - 50, radius, radius);
我这里有:
private class DrawPanel extends JPanel {
private int radius;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(getWidth()/2 - 50, getHeight()/2 - 50, radius, radius);
}
你的问题不是很清楚,你贴的代码有点多,我概括一下吧。
当你用Graphics#fillOval
画圆时,参考点是椭圆所刻入的正方形的左上角。
因此,如果您想使用中心作为参考,给定半径 r,您应该绘制:
(xCenter - r, yCenter - r, r*2, r*2)
此外,由于它是一个圆圈,请考虑使用 Graphics2D
并启用 Antialising。
这是执行此操作的文档:
https://docs.oracle.com/javase/tutorial/2d/advanced/quality.html