title: Iterating through a Java collection to make these balls bounce, any hints?
body: <p>Apologies if the question isn't clear but I couldn't think of another way to phrase it.</p>
  
  <p>This is for a class assignment which I've been working at in BlueJ all weekend. I have to change a method (bounce) to let a user choose how many balls should be bouncing.</p>
  
  <p>Other requirements are: the balls should be of different sizes and should be displayed in a row along the top of the screen BEFORE they bounce.</p>
  
  <p>In order to do this I have to use a collection (ArrayList, HashMap, HashSet). So far I've used HashMap and have been able to have the user choose a number of "balls" of random sizes which place themselves in random positions in the top half of the screen. </p>
  
  <p>When I try to have each ball bounce from its position at the top of the screen, ending at the right hand side I come up stuck. I can make the code draw one ball, bounce it then draw another ball, bounce it etc until the user selected number of balls has looped round.</p>
  
  <p>There are two other classes, one to draw the canvas and one to draw the balls and move them. Both of which I'm not allowed to touch. </p>
  
  <p>What I'm doing the wrong way is probably right in front of me but i've been staring at this code so long I thought I'd ask.</p>
  
  <p>My current version of the code looks like this:</p>
  
  <pre><code>import java.awt.Color;
  import java.util.HashMap;
  import java.util.Random;
  import java.util.Iterator;
  
  public class BallDemo   
  {
      private Canvas myCanvas;
  
      private HashMap&lt;Integer, BouncingBall&gt; ballMap;
  
      private int n;
  
      private int j;
  
      private BouncingBall ball;
  
      /**
       * Create a BallDemo object. Creates a fresh canvas and makes it visible.
       */
      public BallDemo()
      {
          myCanvas = new Canvas("Ball Demo", 600, 500);
      }
  </code></pre>
  
  <p>And the method I have to edit to bounce the balls:</p>
  
  <pre><code> public void bounce(int numBalls)
      {
          ballMap = new HashMap&lt;Integer, BouncingBall&gt;();
          int ground = 400;   // position of the ground line
          Random randomD1 = new Random();
          Random xpos = new Random();
  
  
          myCanvas.setVisible(true);
  
          // draw the ground
          myCanvas.drawLine(50, ground, 550, ground);
  
          // add balls to HashMap
             for(n = 0; n &lt; numBalls; n++) {
           ballMap.put(numBalls, (ball = new BouncingBall(xpos.nextInt(300), 50, randomD1.nextInt(200), Color.BLUE, ground, myCanvas)));
           //
              for(j= 0; j &lt; ballMap.size(); j++) {
                  ball.draw();
                  boolean finished =  false;
                     while(!finished) {
                       myCanvas.wait(50);           // small delay
                       ball.move(); // bounce the ball
  
                          // stop once ball has travelled a certain distance on x axis
                            if(ball.getXPosition() &gt;= 550) {
                                      finished = true;
                                  }
  
  
  
                          }
              }
  
  
  
  
  
  
          }
         }
  }
  </code></pre>
  
  <p>Am I even on the right lines using a HashMap? The combination of keys, values seemed the best way to go. I think I need to somehow iterate through the items placed in the collection to make them bounce using the move() method. But first I need the balls to stay in a row at the top of the screen, no matter how many the user defines. </p>
  
  <p>I'm new to programming and I'm just coming up stumped.</p>
  
  <p>Thanks for any help!</p>