Java 如何以给定的 X 和 Y 坐标绘制一个圆作为圆的中点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19386951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:41:44  来源:igfitidea点击:

How to draw a circle with given X and Y coordinates as the middle spot of the circle?

javaswingjpaneljava-2dpaintcomponent

提问by нα???z

I have developed a telecommunication application for locating signal strengths from the towers. I have used java swing and I'm having a problem when drawing the circle around the given point of the mobile signal transmitter tower location. I have already calculated the X, Y coordinates and also the radius value.

我开发了一个电信应用程序,用于定位信号塔的信号强度。我使用过 java swing 并且在围绕移动信号发射塔位置的给定点绘制圆圈时遇到问题。我已经计算了 X、Y 坐标以及半径值。

Please find the below code which I've used to draw the circle and it is having issues.

请找到我用来绘制圆圈的以下代码,但它有问题。

JPanel panelBgImg = new JPanel() {
    public void paintComponent(Graphics g) {
        g.drawOval(X, Y, r, r);
    }
}

The issue is, it creates the circle but it didn't take the X and Y coordinates as the center point. It took the X and Y coordinates as the top left point of the circle.

问题是,它创建了圆,但没有以 X 和 Y 坐标为中心点。它将 X 和 Y 坐标作为圆的左上点。

Could anyone please help me to draw the circle by having the given X and Y coordinates as the center point of the circle.

任何人都可以通过将给定的 X 和 Y 坐标作为圆的中心点来帮助我绘制圆。

采纳答案by arynaq

The fillOvalfits an oval inside a rectangle, with width=r, height = ryou get a circle. If you want fillOval(x,y,r,r)to draw a circle with the center at (x,y) you will have to displace the rectangle by half its width and half its height.

fillOval一个椭圆形的矩形内的配合,with width=r, height = r你得到一个圆。如果你想fillOval(x,y,r,r)画一个以 (x,y) 为中心的圆,你必须将矩形移动一半的宽度和一半的高度。

public void drawCenteredCircle(Graphics2D g, int x, int y, int r) {
  x = x-(r/2);
  y = y-(r/2);
  g.fillOval(x,y,r,r);
}

This will draw a circle with center at x,y

这将绘制一个圆心在 x,y

回答by qaphla

Replace your draw line with

g.drawOval(X - r, Y - r, r, r)

This should make the top-left of your circle the right place to make the center be (X,Y), at least as long as the point (X - r,Y - r)has both components in range.

这应该使您的圆的左上角成为使中心成为 的正确位置(X,Y),至少只要该点(X - r,Y - r)具有范围内的两个分量。

回答by johnny

both answers are is incorrect. it should read:

两个答案都不正确。它应该是:

x-=r;
y-=r;


drawOval(x,y,r*2,r*2);

回答by Kordian

The only thing that worked for me:

唯一对我有用的东西:

g.drawOval((getWidth()-200)/2,(getHeight()-200)/2, 200, 200);    

回答by Justin

So we are all doing the same home work?

所以我们都做同样的家庭作业?

Strange how the most up-voted answer is wrong. Remember, draw/fillOval take height and width as parameters, not the radius. So to correctly draw and center a circle with user-provided x, y, and radius values you would do something like this:

奇怪的是,投票最多的答案是错误的。请记住,draw/fillOval 将高度和宽度作为参数,而不是半径。因此,要使用用户提供的 x、y 和半径值正确绘制圆并使其居中,您可以执行以下操作:

public static void drawCircle(Graphics g, int x, int y, int radius) {

  int diameter = radius * 2;

  //shift x and y by the radius of the circle in order to correctly center it
  g.fillOval(x - radius, y - radius, diameter, diameter); 

}

回答by Andres Mitchell

drawCircle(int X, int Y, int Radius, ColorFill, Graphics gObj) 

回答by Farhad

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Graphics;
import javax.swing.JFrame;

public class Graphiic
{   
    public Graphics GClass;
    public Graphics2D G2D;
    public  void Draw_Circle(JFrame jf,int radius , int  xLocation, int yLocation)
    {
        GClass = jf.getGraphics();
        GClass.setPaintMode();
        GClass.setColor(Color.MAGENTA);
        GClass.fillArc(xLocation, yLocation, radius, radius, 0, 360);
        GClass.drawLine(100, 100, 200, 200);    
    }

}

回答by Udeesha Induwara

JPanel pnlCircle = new JPanel() {
        public void paintComponent(Graphics g) {
            int X=100;
            int Y=100;
            int d=200;
            g.drawOval(X, Y, d, d);
        }
};

you can change X,Y coordinates and radius what you want.

您可以根据需要更改 X、Y 坐标和半径。

回答by anydoby

This draws an arc with the center in the specified rectangle. You can draw, half-circles, quarter-circles, etc.

这将在指定的矩形中绘制一条圆弧。您可以绘制、半圆、四分之一圆等。

g.drawArc(x - r, y - r, r * 2, r * 2, 0, 360)