英文:
Drawing circles
问题
在Java中如何绘制四个圆,使得这四个圆恰好位于面板的四个角?我有半径a,但我的主要问题是找到矩形左上角的坐标。我已经找到了左下角的情况:
g.fillOval(0 - (2 * a / 2 - ((int)(2 * a / 2 * Math.sqrt(2) / 2))), 0 - (2 * a / 2 - ((int)(2 * a / 2 * Math.sqrt(2) / 2))), 2 * a, 2 * a).
有更简单的方法吗?
英文:
How to draw four circles in java, so all four are in the angles of panel?I have radius a, but my main problem is to find coordinates of upper left point of rectangle. I found this for the down left corner:
g.fillOval(0-(2*a/2-((int)(2*a/2*Math.sqrt(2)/2))),0-(2*a/2-((int)(2*a/2*Math.sqrt(2)/2))), 2*a, 2*a).
Is there easiest way?
答案1
得分: 1
所以所有四个都在面板的角落
你是指面板的四个角落吗?
如果是这样,那么你知道:
- 你的圆的大小,因为你正在绘制这个圆。
- 通过使用面板的
getWidth()
和getHeight()
方法,你知道面板的宽度和高度。
但我的主要问题是找到矩形左上角的坐标
绘制左上角很简单,因为圆总是从 (0,0) 开始。
所以在 paintComponent(...)
方法中,代码将会是:
g.FillOval(0, 0, circleWidth, circleHeight);
要在底部/左侧绘制圆,你也知道 x 值将为 0,因此你只需要计算 y 值,它将是:
int y = getHeight() - circleHeight;
g.fillOval(0, y, circleWidth, circleHeight);
同样的基本逻辑也适用于右上角和右下角。
英文:
> so all four are in the angles of panel
Do you mean the 4 corners of the panel?
If so, then you know:
- the size of your circle because you are painting the circle
- the width/height of the panel by using the
getWidth()
andgetHeight()
methods of the panel.
> but my main problem is to find coordinates of upper left point of rectangle
Painting the upper left is easy since the circle will always start at (0,0).
So in the paintComponent(...)
method the code would be:
g.FillOval(0, 0, circleWidth, circleHeight);
To paint the circle at the bottom/left you also know the x value will be 0, so you only need to calculate the y value which will be:
int y = getHeight() - circleHeight;
g.fillOval(0, y, circleWidth, circleHeight);
The same basic logic will apply for the top/right and bottom/right.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论