/**************************************************************************
* 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.*;
import utilities.AppletShell;

/**
 * Creates a black background on which to draw Swirlies.
 *
 * @author Jim Crossley
 * @version 1.0, 10/17/1996
 */
public class SwirlyLand extends java.applet.Applet {
    int radius;
    boolean single = false;
    boolean swirlOnly = false;
    Vector swirlPool = null;

	/**
	 * Provide standalone functionality.
	 */
	public static void main(String[] args) {
        new AppletShell(new SwirlyLand(), 400, 400);
	}

    /**
	 * Make a black background.
	 */
	public void init() {
        swirlPool = new Vector();
        single = Boolean.valueOf(this.getParameter("single")).booleanValue();
        swirlOnly = Boolean.valueOf(this.getParameter("swirlonly")).booleanValue();
        setBackground(Color.black);
    }

    /**
	 * Determine the maximum dimensions of the Swirlies -- relative to the
	 * applet's size.  Then begin swirling!
	 */
	public void start() {
        Dimension d = this.size();
        radius = Math.min(d.height, d.width)/2;

        if (swirlOnly) {
            Swirl s = new Swirl(this, d.width/2, d.height/2, radius, new Color((int)(Integer.MAX_VALUE * Math.random())), false, 500);
            swirlPool.addElement(s);
            s.start();
        }
        else
        if (single) {
   	        Swirly s = new Swirly(this, d.width/2, d.height/2, radius, new Color((int)(Integer.MAX_VALUE * Math.random())), false, 500);
            swirlPool.addElement(s);
            s.start();
    	}
    	else {
            Swirlies s = new Swirlies(this, d.width/2, d.height/2, false, radius);
            swirlPool.addElement(s);
            s.start();
        }
    }

	/**
	 * Once the initial Swirlies die off (or even before), allow the operator to
	 * manually start them again.
	 * If the 'swirlOnly' parameter is true, only the original, tame swirl will commence.
	 * (One may always see an original swirl by SHIFT-clicking.)
	 * If the 'swirlOnly' parameter is false, and the 'single' parameter is true,
	 * a lone Swirly will be created.  False values for both parameters results
	 * in a herd of Swirlies (the default behavior).
	 *
	 * Clicking on either the right or left mouse button results in either a clockwise
	 * or a counterclockwise rotation, respectively.
	 */
	public boolean mouseDown(Event e, int x, int y) {
	    if (swirlOnly || (e.modifiers & Event.SHIFT_MASK) != 0) {
	        // Show the original Swirl
	        Swirl s = new Swirl(this, x, y, radius, new Color((int)(Integer.MAX_VALUE * Math.random())), (e.modifiers & Event.META_MASK) == 0);
            swirlPool.addElement(s);
            s.start();
	    }
	    else
	    if (single) {
	        // Create The Lone Swirly
   	        Swirly s = new Swirly(this, x, y, radius, new Color((int)(Integer.MAX_VALUE * Math.random())), (e.modifiers & Event.META_MASK) == 0);
            swirlPool.addElement(s);
            s.start();
	    }
	    else {
	        // Create multiple Swirlies
	        Swirlies s = new Swirlies(this, x, y, (e.modifiers & Event.META_MASK) == 0, radius);
            swirlPool.addElement(s);
            s.start();
        }

        return true;
	}

    /**
     * Stop this thread and all the ones we've spawned.
     */
	synchronized public void stop() {
        Enumeration enum = swirlPool.elements();
        while (enum.hasMoreElements()) {
            Swirl s = (Swirl)enum.nextElement();
            s.stop();
        }
        swirlPool.removeAllElements();
	}
}



