import objectdraw.*;

import java.awt.*;


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

    // pause time between leaf creation
    private static final int DELAY_TIME = 900;

    private DrawingCanvas canvas; // the canvas

    private Image fstLeafPic; // pictures of leaves
    private Image sndLeafPic; // pictures of leaves

    // Dimensions of screen
    private int screenHeight;

    // used to pick between the two images
    private RandomIntGenerator leafGen; 

    // used to generate random speeds and positions for leaves
    private RandomIntGenerator leafLocGen; 
    private RandomDoubleGenerator leafSpeedGen; 

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

        leafGen = new RandomIntGenerator(1, 2);
        leafLocGen = new RandomIntGenerator(0, aScreenWidth);
        leafSpeedGen = new RandomDoubleGenerator(2, 4);

        this.start();
    }

    // Successively create leaves which will fall to ground
    public void run() {
        Image nextLeaf; // Image to be used in next leaf
        
        for (int leafCount = 0; 
             leafCount < NUM_LEAVES;
             leafCount++) {

        // pick x location and speed for a new leaf
            double leafX = leafLocGen.nextValue();
            double leafSpeed = leafSpeedGen.nextValue();

            // pick a random leaf picture and then create a leaf
            if (leafGen.nextValue() == 1) {
                nextLeaf = fstLeafPic;
            } else {
                nextLeaf = sndLeafPic;
            }
       
            new FallingLeaf(canvas, 
                            nextLeaf, 
                            leafX,
                            leafSpeed,
                            screenHeight);

            pause(DELAY_TIME);
        }
    }
}
