Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2012
    Posts
    33
    Rep Power
    0

    Default for loop not working?

    Hi

    I'm doing the Stanford cs 106a course and am stuck on what must be a very simple oversight.

    The exercise I'm doing requires a simple program to create random circles on a canvas with random radius, color etc. 10 circles. I've started the program just to test it, without worrying about getting all 10 precisely within the canvas. But there's an unexpected hitch- it only creates one circle. As far as I can see the "for" loop I've written should make 10 method calls and get ten returns- unless I've misunderstood something. I've gone back through the book etc but can't find anything that contradicts my thought process. Here's what I've written;

    import acm.program.*;
    import acm.graphics.*;
    import acm.util.*;
    import java.awt.*;

    public class RandomCircles extends GraphicsProgram {

    private RandomGenerator rgen = RandomGenerator.getInstance();


    public void run() {

    int radius = rgen.nextInt( 25, 250);
    int positionx = rgen.nextInt(getWidth());
    int positiony = rgen.nextInt(getHeight());
    Color nextcolor = rgen.nextColor();
    for (int i = 0; i < 10; i++) {
    add(createCircle(positionx, positiony, radius, nextcolor));
    }

    }

    private GOval createCircle(int x, int y, int r, Color color) {
    GOval circle = new GOval(x, y, r, r);
    circle.setColor(color);
    circle.setFilled(true);
    return circle;

    }
    }

    Could someone let me know what I've done wrong? Thanks!

    Robbie

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: for loop not working?

    Many classes and methods which I dont know, but you never change the parameter for the createCircle method or?
    (positionx, positiony, radius, nextcolor) are still the same!
    You have to change these in your loop! Otherwise you draw ten times the same circle -> looks like one :D

  3. #3
    Join Date
    Jul 2012
    Posts
    33
    Rep Power
    0

    Default Re: for loop not working?

    Of course! So instead of defining variables which use the random generator, I put the random generator into the brackets after the method call? Makes sense I guess, ta, I'll give it a pop...

  4. #4
    Join Date
    Jul 2012
    Posts
    33
    Rep Power
    0

Similar Threads

  1. for loop stops working
    By name in forum New To Java
    Replies: 5
    Last Post: 04-17-2012, 05:53 PM
  2. Loop not working
    By swilliams236 in forum New To Java
    Replies: 2
    Last Post: 11-07-2011, 11:36 PM
  3. for loop not working properly
    By lbgladson in forum New To Java
    Replies: 8
    Last Post: 10-15-2011, 01:33 AM
  4. Why isn't this while loop code working
    By GreenTea in forum New To Java
    Replies: 16
    Last Post: 11-10-2010, 04:14 AM
  5. while loop not working
    By RBNSN83 in forum New To Java
    Replies: 6
    Last Post: 06-21-2010, 08:29 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
  •