Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By Toll

Thread: Drawing Concentric Circles with For Loops Assignment

  1. #1
    shaeway is offline Member
    Join Date
    Jul 2013
    Location
    Texas
    Posts
    13
    Rep Power
    0

    Exclamation 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.

  2. #2
    shaeway is offline Member
    Join Date
    Jul 2013
    Location
    Texas
    Posts
    13
    Rep Power
    0

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

  3. #3
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: Drawing Concentric Circles with For Loops Assignment

    Well, let's start by looking at what the for-loop is doing:

    Java 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.

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default 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?
    shaeway likes this.

  5. #5
    shaeway is offline Member
    Join Date
    Jul 2013
    Location
    Texas
    Posts
    13
    Rep Power
    0

    Default 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.

  6. #6
    shaeway is offline Member
    Join Date
    Jul 2013
    Location
    Texas
    Posts
    13
    Rep Power
    0

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

  7. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default 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?

  8. #8
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default 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.

  9. #9
    shaeway is offline Member
    Join Date
    Jul 2013
    Location
    Texas
    Posts
    13
    Rep Power
    0

    Default 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.

  10. #10
    shaeway is offline Member
    Join Date
    Jul 2013
    Location
    Texas
    Posts
    13
    Rep Power
    0

    Default 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.

  11. #11
    shaeway is offline Member
    Join Date
    Jul 2013
    Location
    Texas
    Posts
    13
    Rep Power
    0

    Default 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!!

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Drawing Concentric Circles with For Loops Assignment

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default Re: Drawing Concentric Circles with For Loops Assignment

    Quote Originally Posted by shaeway View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Drawing concentric circles
    By jazzo in forum New To Java
    Replies: 7
    Last Post: 02-07-2013, 03:01 PM
  2. Using nested loops to repeat circles
    By Jakey in forum New To Java
    Replies: 3
    Last Post: 09-30-2012, 07:08 PM
  3. newbie drawing two circles simultaneously
    By nadeemshafi9 in forum Threads and Synchronization
    Replies: 7
    Last Post: 01-09-2011, 01:53 PM
  4. Replies: 4
    Last Post: 09-13-2008, 11:07 AM
  5. Circles (Loops)
    By Zebra in forum New To Java
    Replies: 1
    Last Post: 05-29-2008, 06:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •