title: Multiple bouncing balls triggered by MouseListener
body: <p>I want to do a bouncing balls application in java. Each ball should take place by mouse clicking and each of them should have random speed, color, radius and starting position. I managed to do everything except the part where mouse listener takes place. Whatever i do in the mousePressed method didn't work. What should i do to make user create a random ball when he presses the mouse?</p>
  
  <p><strong>EDIT:</strong> This is the last version of my code. Now the problem is that i can't create more than one ball. When i click on the screen same ball is just keeps speeding. </p>
  
  <p><em>BouncingBalls Class</em> </p>
  
  <pre><code>public class BouncingBalls extends JPanel implements MouseListener{
  
  private Ball ball;
  protected List&lt;Ball&gt; balls = new ArrayList&lt;Ball&gt;(20);
  private Container container;
  private DrawCanvas canvas;
  private int canvasWidth;
  private int canvasHeight;
  public static final int UPDATE_RATE = 30;
  
  int x = random(480);
  int y = random(480);
  int speedX = random(30);
  int speedY = random(30);
  int radius = random(20);
  int red = random(255);
  int green = random(255);
  int blue = random(255);
  int count = 0;
  
  public static int random(int maxRange) {
      return (int) Math.round((Math.random() * maxRange));
  }
  
  public BouncingBalls(int width, int height){
  
      canvasWidth = width;
      canvasHeight = height;
  
      ball = new Ball(x, y, speedX, speedY, radius, red, green, blue);
      container = new Container();
  
      canvas = new DrawCanvas();
      this.setLayout(new BorderLayout());
      this.add(canvas, BorderLayout.CENTER);
      this.addMouseListener(this);
  
  }
  
  public void start(){
  
      Thread t = new Thread(){
  
          public void run(){
  
              while(true){
  
                  update();
                  repaint();
                  try {
                      Thread.sleep(1000 / UPDATE_RATE);
                  } catch (InterruptedException e) {}
              }
          }
      };
      t.start();
  }
  
  public void update(){
  
      ball.move(container);
  }
  
  class DrawCanvas extends JPanel{
  
      public void paintComponent(Graphics g){
  
          super.paintComponent(g);
          container.draw(g);
          ball.draw(g);
      }
  
      public Dimension getPreferredSize(){
  
          return(new Dimension(canvasWidth, canvasHeight));
      }
  }
  
  public static void main(String[] args){
  
      javax.swing.SwingUtilities.invokeLater(new Runnable(){
  
          public void run(){
  
              JFrame f = new JFrame("Bouncing Balls");
              f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
              f.setContentPane(new BouncingBalls(500, 500));
              f.pack();
              f.setVisible(true);
          }
      });
  }
  
  @Override
  public void mouseClicked(MouseEvent e) {
      // TODO Auto-generated method stub
  
  }
  
  @Override
  public void mouseEntered(MouseEvent e) {
      // TODO Auto-generated method stub
  
  }
  
  @Override
  public void mouseExited(MouseEvent e) {
      // TODO Auto-generated method stub
  
  }
  
  @Override
  public void mousePressed(MouseEvent e) {
  
      balls.add(new Ball(x, y, speedX, speedY, radius, red, green, blue));
      start();
  }
  
  @Override
  public void mouseReleased(MouseEvent e) {
      // TODO Auto-generated method stub
  
  }
  }
  </code></pre>
  
  <p><em>Ball Class</em></p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics;
  
  public class Ball{
  
  public static int random(int maxRange) {
      return (int) Math.round((Math.random() * maxRange));
  }
  
  private BouncingBalls balls;
  int x = random(480);
  int y = random(480);
  int speedX = random(30);
  int speedY = random(30);
  int radius = random(20);
  int red = random(255);
  int green = random(255);
  int blue = random(255);
  int i = 0;
  
  public Ball(int x, int y, int speedX, int speedY, int radius, int red, int green, int blue){
  
      this.x = x;
      this.y = y;
      this.speedX = speedX;
      this.speedY = speedY;
      this.radius = radius;
      this.red = red;
      this.green = green;
      this.blue = blue;
  }
  
  public void draw(Graphics g){
  
      for(Ball ball : balls){
  
          g.setColor(new Color(red, green, blue));
          g.fillOval((int)(x - radius), (int)(y - radius), (int)(2 * radius), (int)(2 * radius));
      }
  }
  
  public void move(Container container){
  
      x += speedX;
      y += speedY;
  
      if(x - radius &lt; 0){
  
          speedX = -speedX;
          x = radius;
      }
      else if(x + radius &gt; 500){
  
          speedX = -speedX;
          x = 500 - radius;
      }
  
      if(y - radius &lt; 0){
  
          speedY = -speedY;
          y = radius;
      }
      else if(y + radius &gt; 500){
  
          speedY = -speedY;
          y = 500 - radius;
      }
  }
  }
  </code></pre>
  
  <p><em>Container Class</em></p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics;
  
  public class Container {
  
  private static final int HEIGHT = 500;
  private static final int WIDTH = 500;
  private static final Color COLOR = Color.WHITE;
  
  public void draw(Graphics g){
  
      g.setColor(COLOR);
      g.fillRect(0, 0, WIDTH, HEIGHT);
  }
   }
  </code></pre>
  
  <p><strong>ERROR:</strong> I get "Can only iterate over an array or an instance of java.lang.Iterable" error in this part of code:</p>
  
  <pre><code>public void draw(Graphics g){
  
      for(Ball ball : balls){
  
          g.setColor(new Color(red, green, blue));
          g.fillOval((int)(x - radius), (int)(y - radius), (int)(2 * radius), (int)(2 * radius));
      }
  }
  </code></pre>