in

We will also look at one slight modification to the bouncing ball example. Instead of one bouncing ball there will be several. The program is shown in Figure 3.4.

 Several bouncing balls

Figure 3.4: Several bouncing balls

You can see this program running in Applet 3.2.




Applet 3.2: Several bouncing balls

You can see the source code for this example in Listing 3.5.

Listing 3.5: Several bouncing balls (BouncingBalls.java)

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class BouncingBalls extends BasicAnimate
{
  /**
   * When this class is ran as an application, how wide should it be.
   */     
  public static final int IDEAL_WIDTH = 640;

  /**
   * When this class is ran as an application, how tall should it be.
   */   
  public static final int IDEAL_HEIGHT = 480;

  /**
   * What is the size of each bouncing ball.
   */   
  public static final int SIZE = 32;

  /**
   * The number of balls to animate.
   */   
  public final int BALL_COUNT = 20;

  /**
   * What is the current x-coordinate of the ball.
   */ 
  private int x[];

  /**
   * What is the current y coordinate of the ball. 
   */  
  private int y[];

  /**
   * What is the current x-direction of each ball, 1=right, -1=left
   */  
  private int dx[];

  /**
   * What is the current y-direction of each ball, 1=down, -1=Up
   */   
  private int dy[];

  /**
   * What is the width of the page that we should check, takes into
   * account how wide the ball is.
   */ 
  private int checkWidth;

  /**
   * What is the height of the page that we should check, takes into
   * account how tall the ball is.
   */ 
  private int checkHeight;

  /**
   * This method sets up the bouncing balls example.
   *
   */  
  public void init()
  {
    super.init();
    checkWidth = getWidth() - SIZE;
    checkHeight = getHeight() - SIZE;

    // create the "balls"
    x = new int[BALL_COUNT];
    y = new int[BALL_COUNT];
    dx = new int[BALL_COUNT];
    dy = new int[BALL_COUNT];

    for (int i = 0; i < BALL_COUNT; i++)
    {
      x[i] = (int) (Math.random() * checkWidth);
      y[i] = (int) (Math.random() * checkHeight);
      int tdx = (int) (Math.random() * 2);
      int tdy = (int) (Math.random() * 2);

      if (tdx == 1)
        dx[i] = 1;
      else
        dx[i] = -1;

      if (tdy == 1)
        dy[i] = 1;
      else
        dy[i] = -1;
    }

    setPulseLength(10);
  }


  /**
   * This method renders each frame of the bouncing balls.
   *
   * @param g The off-screen graphics object to paint to.
   */ 
  public void paintOffscreen(Graphics g)
  {
    super.paintOffscreen(g);

    for (int i = 0; i < BALL_COUNT; i++)
    {
      x[i] += dx[i];
      y[i] += dy[i];
      g.setColor(Color.WHITE);
      g.fillOval(x[i], y[i], SIZE, SIZE);

      // bounce if needed
      if ((x[i] > checkWidth) || (x[i] < 0))
        dx[i] = -dx[i];
      if ((y[i] > checkHeight) || (y[i] < 0))
        dy[i] = -dy[i];
    }

  }

  /**
   * The main method is called when the class is to be ran
   * as a Java application. This method creates a frame and
   * attaches the applet to the frame. 
   *
   * @param args Not used.
   */  
  public static void main(String args[])
  {
    BouncingBalls applet = new BouncingBalls();
    Frame frame = new Frame();
    frame.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        System.exit(0);
      }
    });

    frame.add(applet);
    frame.setSize(IDEAL_WIDTH, IDEAL_HEIGHT);
    frame.show();
    applet.init();
  }

}

This program is very similar to the previous example. However, the variables "x", "y", "dx" and "dy" are all now arrays. The "paintOffscreen" method now loops over all of the balls. Yet the same operation is performed as in the previous example.

As you can see from the following lines, the loop begins, and then starts just as the pervious example did.

for (int i = 0; i < BALL_COUNT; i++)
{
   x[i] += dx[i];
   y[i] += dy[i];

The first step, after the loop, is to modify the "x" and "y" coordinates. However, unlike last time, they are arrays.


Copyright 2005 - 2010 by Heaton Research, Inc.. Heaton Research™ and Encog™ are trademarks of Heaton Research. Click here for copyright and trademark information.