绘制圆形

huangapple go评论67阅读模式
英文:

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

所以所有四个都在面板的角落

你是指面板的四个角落吗?

如果是这样,那么你知道:

  1. 你的圆的大小,因为你正在绘制这个圆。
  2. 通过使用面板的 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:

  1. the size of your circle because you are painting the circle
  2. the width/height of the panel by using the getWidth() and getHeight() 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.

huangapple
  • 本文由 发表于 2020年8月18日 22:23:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63470707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定