25

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
нαƒєєz
  • 1,239
  • 4
  • 17
  • 27

9 Answers9

47

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

arynaq
  • 6,710
  • 9
  • 44
  • 74
  • 8
    +1 for noticing the OP was using `r` (radius) as a pseudo `d` (diameter). The parameters are labelled `w` and `h` in the docs. – Andrew Thompson Oct 15 '13 at 17:24
  • This answer is wrong. It does paint an oval at (x,y) as center, but the radius is wrong. Please use Justin's answer! – UNIQUEorn Dec 22 '22 at 09:30
14

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

}
Justin
  • 149
  • 1
  • 2
  • 1
    ' *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. – AirlineDog Jan 13 '21 at 20:11
6

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.

qaphla
  • 4,707
  • 3
  • 20
  • 31
3
drawCircle(int X, int Y, int Radius, ColorFill, Graphics gObj) 
xlm
  • 6,854
  • 14
  • 53
  • 55
  • 5
    Welcome to Stack Overflow! I recommend you [take the tour](http://stackoverflow.com/tour). When giving an answer it is preferable to give [some explanation as to WHY your answer](http://stackoverflow.com/help/how-to-answer) is the one. – Stephen Rauch Jan 31 '17 at 00:09
2
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.

Udeesha Induwara
  • 605
  • 1
  • 10
  • 20
1

both answers are is incorrect. it should read:

x-=r;
y-=r;


drawOval(x,y,r*2,r*2);
johnny
  • 258
  • 2
  • 12
1
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);    
    }

}
Farhad
  • 57
  • 1
1

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)
anydoby
  • 408
  • 4
  • 9
0

The only thing that worked for me:

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