import objectdraw.*;
import java.awt.*;

public class Tree extends ActiveObject
{
   // # of leaves to generate
   private static final int NUM_LEAVES = 10;

   private DrawingCanvas aCanvas;      // the canvas

   private Image fstLeafPic, sndLeafPic;  // pictures of leaves

   // Dimensions of screen
   private int aScreenWidth, aScreenHeight;

   // used to generate random speeds and positions for leaves
   private RandomIntGenerator leafGen,		// determines which leaf image used
   leafLocGen; 	// determines x coord of leaf

   // Remember parameters and create random number generator
   // in order to create leaves in execute method
   public Tree( Image leafpic1, Image leafpic2,
                int screenWidth, int screenHeight,
                DrawingCanvas canvas)
   {
      // save the parameters for the "execute" method
      aCanvas = canvas;
      fstLeafPic = leafpic1;
      sndLeafPic = leafpic2;
      aScreenWidth = screenWidth;
      aScreenHeight = screenHeight;

      leafGen = new RandomIntGenerator(1,2);
      leafLocGen = new RandomIntGenerator( 0, screenWidth);

      start();
   }

   // Successively create leaves which will fall to ground
   public void run()
   {
      Image nextLeaf;		// Image to be used in next leaf

      int treeCount = 0;	// # of leaves generated so far

      while (treeCount < NUM_LEAVES )
      {
         // pick a random leaf picture and then create a leaf
         if ( leafGen.nextValue() == 1)
         {
            nextLeaf = fstLeafPic;
         }
         else
         {
            nextLeaf = sndLeafPic;
         }

         new FallingLeaf( aCanvas, nextLeaf,
                          leafLocGen.nextValue(),   // x coordinate
                          leafLocGen.nextValue()*2/aScreenWidth+2, // y speed
                          aScreenHeight);

         treeCount = treeCount + 1;
         pause( 900);

      }
   }

}