Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 814
нαƒєєz Avatar asked Oct 15 '13 17:10

нαƒєєz


People also ask

How do I draw a circle in a JPanel?

Solution: Write a method, drawCircle() , which has parameters for the coordinates of the center point and a radius. It then calls drawOval with transformed parameters. To make a drawing, define a new component by subclassing JPanel and overriding the paintComponent() method.

How do I draw a circle in eclipse?

9 Answers. Show activity on this post. 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.

How do you draw a circle in Java applet?

In Java, the Graphics class doesn't have any method for circles or ellipses. However, the drawOval() method can be used to draw a circle or an ellipse. Ovals are just like a rectangle with overly rounded corners.


2 Answers

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

like image 125
arynaq Avatar answered Sep 22 '22 14:09

arynaq


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);   } 
like image 30
Justin Avatar answered Sep 20 '22 14:09

Justin