[英]How to draw a random circle in center using the center as reference with Graphics#fillOval
Currently when a button is clicked, the circle will draw at g.fillOval(getWidth()/2 - 50, getHeight()/2 - 50, radius, radius);
当前,当单击按钮时,圆将在g.fillOval(getWidth()/2 - 50, getHeight()/2 - 50, radius, radius);
处绘制g.fillOval(getWidth()/2 - 50, getHeight()/2 - 50, radius, radius);
Here I have: 我在这里:
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);
}
Your question is not very clear, and you posted a bit too much code so let me generalize. 您的问题不是很清楚,您发布了太多代码,让我概括一下。
When you draw a circle with Graphics#fillOval
, the point of reference if the upper left corner of the square that the Oval is inscribed into. 当您使用Graphics#fillOval
圆时,作为参考点,如果椭圆的内接在正方形的左上角。
So if you want to use the center as reference, given a radius r you should draw in: 因此,如果您想将中心用作参考,则在给定半径r的情况下,应绘制出:
(xCenter - r, yCenter - r, r*2, r*2)
Also since it's a circle, consider using Graphics2D
with Antialising on. 另外,由于它是一个圆圈,因此请考虑在启用了Antialising的情况下使用Graphics2D
。 Here the docs to do that: 这里的文档可以做到这一点:
https://docs.oracle.com/javase/tutorial/2d/advanced/quality.html https://docs.oracle.com/javase/tutorial/2d/advanced/quality.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.