# Drawing Concentric Circles with For Loops Assignment

• 07-12-2013, 11:16 PM
shaeway
Drawing Concentric Circles with For Loops Assignment
Ok, this is for my intro to programming class and we are in the Supplemental 3G chapter of the book: Building Java Programs. I don't really understand how to write a for loop, so using one to draw five concentric circles in a drawing panel is beyond me. The panel is 600 by 600 pixels and the circles are supposed to start from the outside and work in. Here is what I have:

import java.awt.*;//for drawing panel
import java.util.*;//for scanner

public class Hyperbola
{
//declare needed constants
public static final Scanner CONSOLE = new Scanner (System.in);
public static final int WIDTH = 600;
public static final int HEIGHT = 600;
public static final int CIRCLES = 5;

public static void main( String [] args)
{
//drawing panel(canvas)
DrawingPanel dp = new DrawingPanel( WIDTH, HEIGHT );
//graphis (paintbrush)
Graphics g = dp.getGraphics();

drawCircles(g, 0, 0 );

}
//draws the circles
public static void drawCircles(Graphics g, int x, int y)
{
for( int i = 0; i < CIRCLES; i = i + 1)
{
g.drawOval( x * i , y * i , WIDTH , HEIGHT );

}
}
}

I get a circle, but don't have a clue how to get four smaller. I will also have to vary the color of the circles starting from gray and getting closer to white with each circle. I'm also kinda clueless about that. The examples in the book are all squares or pyramids. Nothing about concentric anythings....sigh...I'm a geology major. This is all Greek to me.

The scanner is for the next part of the project-drawing a hyperbola. I will probably be back for that too.

Thanks for ANY help.
• 07-12-2013, 11:34 PM
shaeway
Re: Drawing Concentric Circles with For Loops Assignment
Ok, I have the right number of circles, but they aren't concentric.

//draws the circles
public static void drawCircles(Graphics g, int x, int y)
{
for( int i = 0; i < CIRCLES; i = i + 1)
{
g.drawOval( x + (i * 10) , y + (i * 10) , WIDTH - (i * 100) , HEIGHT - (i * 100) );
• 07-12-2013, 11:37 PM
Toll
Re: Drawing Concentric Circles with For Loops Assignment
Well, let's start by looking at what the for-loop is doing:

Code:

```for( int i = 0; i < CIRCLES; i = i + 1) { g.drawOval( x * i , y * i , WIDTH , HEIGHT ); }```
So it will go through the numbers 0-4 and draw one oval each at x*i, y*i with a width/height of 600. Since you call the method with 0, 0 as the arguments, this means that all five circles will be at the exact same place.

If you haven't already, I'd suggest having a look at Graphics (Java Platform SE 6) which will tell you how to use drawOval. You're off to a good start though, you just need to use the i variable a little better.
• 07-12-2013, 11:39 PM
Toll
Re: Drawing Concentric Circles with For Loops Assignment
Good to see you figured it out while I was writing my post! As to why they're not concentric:

Try going through what the values will be for each value of i. In your example above, it'd be:

g.drawOval(0, 0, 600, 600);
g.drawOval(10, 10, 500, 500);
g.drawOval(20, 20, 400, 400);
g.drawOval(30, 30, 300, 300);
g.drawOval(40, 40, 200, 200);

Can you see the problem there?
• 07-12-2013, 11:52 PM
shaeway
Re: Drawing Concentric Circles with For Loops Assignment
YES! :)

I was just thinking about that. I was trying to draw it out with a pen and paper. My circles are getting exponentially smaller, not linearly. So I need to change to something more like this:

public static void drawCircles(Graphics g, int x, int y)
{
for( int i = 0; i < CIRCLES; i = i + 1)
{
g.drawOval( x + (i * 10) , y + (i * 10) , WIDTH - (i * 10) , HEIGHT - (i * 10) );

This still isn't quite right though....the circles are still off. I'm thinking it is still the sides of the "square". I need to find a better formula for them I think. I'm trying to look at this from a mathematics standpoint so it makes more sense to me. Or perhaps I am confusing myself more. But I think the trouble is in my WIDTH and HEIGHT parameters.
• 07-12-2013, 11:56 PM
shaeway
Re: Drawing Concentric Circles with For Loops Assignment
GOT IT!!!!!!!

I wasn't considering the fact that my "squares" were getting smaller on both sides and therefore needed to double the decrease in length. In other words:

public static void drawCircles(Graphics g, int x, int y)
{
for( int i = 0; i < CIRCLES; i = i + 1)
{
g.drawOval( x + (i * 10) , y + (i * 10) , WIDTH - (i * 20) , HEIGHT - (i * 20) );
• 07-12-2013, 11:56 PM
Toll
Re: Drawing Concentric Circles with For Loops Assignment
Nope, you're definitely on the right path. Hmm... Well, let's use squares as an example instead of circles. If you had a square that was 600 by 600, and you wanted to add a second square inside it that had a distance of 10 to each border, how big would that square be?
• 07-12-2013, 11:57 PM
Toll
Re: Drawing Concentric Circles with For Loops Assignment
Heh, and there we go!

You said you needed colours though. If you follow the link I posted above and scroll to near the top, you'll get a list of methods in the Graphics class. Have a look for the method called setColor, and see if you can figure it out from there.
• 07-13-2013, 12:01 AM
shaeway
Re: Drawing Concentric Circles with For Loops Assignment
Thank you so much!

It feels so good getting this programming stuff to do what I want it to do. Getting the slight push in the right direction is all I need sometimes.

And yes I need colors, so here I go....I'll probably be back.
• 07-13-2013, 12:20 AM
shaeway
Re: Drawing Concentric Circles with For Loops Assignment
Oh. Um....

Ok, I need the colors to be going from dark gray to light gray. So if I set the x, y, WIDTH and HEIGHT as int variables in my loop, then I can use one of them in the RGB color decrease and the drawing of my circles.

I don't think that makes sense outside of my head. Ok, if I do a g.setColor(new Color(#, #, #) then g.drawOval(#, #, #, #), each successive oval will be drawn as a different color if the # I have in the Color is connected with i. Then as i changes, the Color and Oval will change until the loop is done.

Gonna try it.
• 07-13-2013, 12:34 AM
shaeway
Re: Drawing Concentric Circles with For Loops Assignment
Ok, that wasn't quite right, but this is:

public static void drawCircles(Graphics g, Color c, int x, int y)
{
for( int i = 0; i < CIRCLES; i = i + 1)
{
int grays = 128 + i * 20;
Color newg = new Color(grays, grays, grays);
g.setColor(newg);
g.fillOval( x + (i * 10) , y + (i * 10) , WIDTH - (i * 20) , HEIGHT - (i * 20) );

YES!!
• 07-13-2013, 03:06 AM
DarrylBurke
Re: Drawing Concentric Circles with For Loops Assignment
• 07-13-2013, 09:12 AM
JosAH
Re: Drawing Concentric Circles with For Loops Assignment
Quote:

Originally Posted by shaeway
I was trying to draw it out with a pen and paper.

Which is extremely useful; visualizing stuff by simply scribbling and doodling on a piece of paper can be enlightning; I wish more people'd do that before they ask their questions here ...

kind regards,

Jos