Sunday, April 19, 2009

How to program concentric circles using Java and for loops?

Ok, I don%26#039;t exactly know what you mean by programming concentric circles.





If you%26#039;re trying to display circles of progressively changing radius in a GUI (here I%26#039;m assuming you%26#039;re using a basic Swing GUI setup with the default Graphics module for graphics), then you could just use a %26#039;for%26#039; ranging from your smallest radius to your largest radius inside your applet%26#039;s graphics method:





for (double r=1; r%26lt;20; r++){...}





Then, inside your loop, you would create Ellipse2D objects. g is the Graphics2D instance, x and y are doubles predefined to be the center of your concentric circles.





g.draw(new Ellipse2D.Double(x, y, r*2, r*2))





If you need to use the concetric circles for mathematical application though, you could use a user-defined class Circle which includes double X, double Y, and double R, being x coordinate, y coordinate and radius. If you needed a set of concentric circles, then you could define a ConcentricCircles class which holds an ArrayList of Circles and dynamically populates the ArrayList with circles.

How to program concentric circles using Java and for loops?
The website (link below) will show you loops and concentric circles. Happy Programming



myspace

No comments:

Post a Comment