/**************************************************************************
* Copyright (c) 1996 Jim Crossley
*
* Although it's unlikely that anyone would actually want it,
* permission is hereby granted, without written agreement and
* without license or royalty fees, to use, copy, modify, and
* distribute this software and its documentation for any purpose,
* provided that the above copyright notice and the following three
* paragraphs appear in all copies of this software, its documentation
* and any derivative work.
*
* In no event shall Jim Crossley or Automated Design Systems, Inc. be
* liable to any party for direct, indirect, special, incidental, or
* consequential damages arising out of the use of this software and
* its documentation.
*
* The software provided hereunder is on an "as is" basis, and neither
* Jim Crossley nor Automated Design Systems, Inc. has any obligation
* to provide maintenance, support, updates, enhancements, or modifications.
*
* Derived or altered versions must be plainly marked as such, and
* must not be misrepresented as being the original software.
***************************************************************************/
package swirly;

import java.awt.*;
import java.util.*;
/**
 * A quick and dirty way of creating Swirly objects, of random diameter and
 * color, all with the same center.
 *
 * @author Jim Crossley
 * @version 1.0, 10/17/1996
 */
class Swirlies extends Swirl implements Runnable {
    Vector swirlPool;

    /**
     * The constructor sets member variables.
     */
    Swirlies(SwirlyLand c, int x, int y, boolean direction, int max, int reps) {
        super(c,x,y,max,null,direction,reps);
        swirlPool = new Vector();
    }

    /**
     * The default number of Swirlies is a tenth of the number of rotations.
     */
    Swirlies(SwirlyLand c, int x, int y, boolean direction, int max) {
        this(c,x,y,direction,max,DEFAULT_REPS/10);
    }

    /**
     * Every so often we start up a Swirly!
     */
    public void run() {
        for (int i=0; i++ < reps && Thread.currentThread() == swirler;) {
            try {
    	        Swirly s = new Swirly(box, x, y, (int)(r * Math.random()), new Color((int)(Integer.MAX_VALUE * Math.random())), clockwise);
                s.start();
                swirlPool.addElement(s);

                Thread.sleep(500 + (int)(500 * Math.random()));
            }
            catch (Exception e) {
                break;
            }
        }
        swirler = null;
    }

    /**
     * Stop this thread and all the ones we've spawned.
     */
	synchronized public void stop() {
	    swirler = null;
        Enumeration enum = swirlPool.elements();
        while (enum.hasMoreElements()) {
            Swirl s = (Swirl)enum.nextElement();
            s.stop();
        }
        swirlPool.removeAllElements();
	}

}

