简体   繁体   English

画圈

[英]Drawing circles

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.如何在 java 中画四个圆,使四个圆都在面板的角内?我有半径 a,但我的主要问题是找到矩形左上角的坐标。 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?有最简单的方法吗?

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.使用面板的getWidth()getHeight()方法设置面板的宽度/高度。

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).绘制左上角很容易,因为圆总是从 (0,0) 开始。

So in the paintComponent(...) method the code would be:所以在paintComponent(...)方法中的代码是:

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:要在底部/左侧绘制圆圈,您还知道 x 值将为 0,因此您只需要计算 y 值即可:

int y = getHeight() - circleHeight;
g.fillOval(0, y, circleWidth, circleHeight);

The same basic logic will apply for the top/right and bottom/right.相同的基本逻辑将适用于顶部/右侧和底部/右侧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM