/** 
 *  BlackHole Source File
 *
 *  GNU Copyright (C) 2008  Gaspar Sinai gaspar(at)adys.org 
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License, version 2,
 *  dated June 1991. See file COPYYING for details.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package sinai.gaspar.blackhole;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

/**
 * This is an animated version of Stanislaw Ulam's spiral prime drawing. 
 * The black dots are the prime numbers and the starting point
 * is incremented on each animation iteration.
 *
 * @author Gaspar Sinai
 * @version 2008-04-30
 */
public class BlackHole extends Applet {

    private BoardPanel boardPanel = null;

    /**
     * Start animation.
     */
    public void start () {
        boardPanel.start ();
    }

    /**
     * Stop animation.
     */
    public void stop () {
        boardPanel.stop ();
    }

    /**
     * Destroy the applet.
     */
    public void destroy () {
        boardPanel.destroy ();
    }

    /**
     * Initialize the applet. Create a BoardPanel and set options.
     */
    public void init () {
        // Get applet parameters
        boardPanel = new BoardPanel ();
        // Initialize applet
        setLayout (new BorderLayout());
        add (boardPanel, BorderLayout.CENTER);
        OptionPanel optionPanel = new OptionPanel (boardPanel);
        add (optionPanel, BorderLayout.EAST);
        boardPanel.setChangeListener (optionPanel);
        try {
            if (getParameter("cells") != null) {
                boardPanel.setCells (Integer.parseInt (getParameter("cells")));
            }
            if (getParameter("cellSize") != null) {
                boardPanel.setCellSize (Integer.parseInt (getParameter("cellSize")));
            }
            if (getParameter("delay") != null) {
                boardPanel.setDelay (Long.parseLong (getParameter("delay")));
            }
            if (getParameter("start") != null) {
                boardPanel.setStart (Long.parseLong (getParameter("start")));
            }
            if (getParameter("background") != null) {
                boardPanel.setBackground (BoardPanel.parseColor (getParameter("background")));
                optionPanel.setBackground (BoardPanel.parseColor (getParameter("background")));
                setBackground (BoardPanel.parseColor (getParameter("background")));
            }
            if (getParameter("foreground") != null) {
                boardPanel.setForeground (BoardPanel.parseColor (getParameter("foreground")));
            }
            if (getParameter("showCounter") != null) {
                boardPanel.setShowCounter ((new Boolean (getParameter("showCounter"))).booleanValue());
            }
            if (getParameter("type") != null) {
                int type = Integer.parseInt (getParameter("type"));
                optionPanel.setType (type);
            }
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }
}
