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

215,593

Solution 1

The fillOval fits an oval inside a rectangle, with width=r, height = r you 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.

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

Solution 2

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:

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); 

}

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
215,593

Related videos on Youtube

нαƒєєz
Author by

нαƒєєz

Updated on July 11, 2022

Comments

  • нαƒєєz
    нαƒєєz almost 2 years

    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.

    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.

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

  • Cruncher
    Cruncher over 10 years
    I'm pretty sure it works even if the top-left corner is out of range
  • qaphla
    qaphla over 10 years
    I've never tried drawing things offscreen, so I'll take your word for that. Certainly, however, it does work if the corner is in range.
  • Andrew Thompson
    Andrew Thompson over 10 years
    +1 for noticing the OP was using r (radius) as a pseudo d (diameter). The parameters are labelled w and h in the docs.
  • An SO User
    An SO User over 10 years
    I agree with @Cruncher
  • Stephen Rauch
    Stephen Rauch over 7 years
    Welcome to Stack Overflow! I recommend you take the tour. When giving an answer it is preferable to give some explanation as to WHY your answer is the one.
  • AirlineDog
    AirlineDog over 3 years
    ' Strange how the most up-voted answer is wrong '. It is not wrong, he is just using the letter r as diameter which is the height and width. That's how the OP is naming his variable.