ns have developed a telecommunication application for locating signal strengths from the towers. I have actually used java swing and also I"m having actually a problem when drawing the circle roughly the given suggest of the mobile signal transmitter tower location. I have currently calculated the X, Y collaborates and additionally the radius value.

Please discover the listed below code which I"ve used to attract the circle and also it is having actually issues.

You are watching: How to draw a circle in java

JPanel panelBgImg = new JPanel() public void paintComponent(Graphics g) g.drawOval(X, Y, r, r); The problem is, it create the circle yet it didn"t take the X and also Y collaborates as the facility point. It take it the X and also Y works with as the top left point of the circle.

Could anyone please help me to attract the circle by having actually the offered X and also Y coordinates as the center allude of the circle.


*

*

The fillOval fits one oval inside a rectangle, v width=r, height = r you gain a circle.If you want fillOval(x,y,r,r) to attract a circle v the facility at (x,y) you will need to displace the rectangle by half its broad 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 attract a circle with center at x,y


*

So we space all doing the same home work?

Strange how the many up-voted prize is wrong. Remember, draw/fillOval take height and width together parameters, no the radius. So to effectively draw and also center a circle v user-provided x, y, and also radius worths you would carry out something choose this:

public static void drawCircle(Graphics g, int x, int y, int radius) int diameter = radius * 2; //shift x and y through the radius the the circle in order come correctly center it g.fillOval(x - radius, y - radius, diameter, diameter);

*

Replace your attract line with

g.drawOval(X - r, Y - r, r, r)This have to make the top-left of her circle the right location to make the center be (X,Y),at least as lengthy as the point (X - r,Y - r) has both components in range.


*

JPanel pnlCircle = brand-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 friend want.


import java.awt.Color;import java.awt.Graphics2D;import java.awt.Graphics;import javax.swing.JFrame;public class Graphiic windy Graphics GClass; windy Graphics2D G2D; publicly 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);
This paint, etc an arc v 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)
thanks for contributing an answer to ridge Overflow!

Please be certain to answer the question. Provide details and also share your research!

But avoid

Asking because that help, clarification, or responding to various other answers.Making statements based upon opinion; ago them up with recommendations or personal experience.

See more: How Can You Reduce The Stress Of Completing A Project? ? How To Reduce Project Stress & Resolve Issues

To find out more, check out our advice on writing an excellent answers.


write-up Your answer Discard

By clicking “Post your Answer”, friend agree come our terms of service, privacy policy and cookie plan


Not the answer you're looking for? Browse various other questions tagged java waver jpanel java-2d paintcomponent or asking your own question.


How have the right to I find the works with of the one of a circle that rotates approximately the one of another circle?
site architecture / logo design © 2021 stack Exchange Inc; user contributions licensed under cc by-sa. Rev2021.11.12.40742


her privacy

By click “Accept every cookies”, friend agree stack Exchange deserve to store cookie on your device and disclose information in accordance with our Cookie Policy.