/*
 * Maze
 * Copyright (C) 2000  Paul Davis, pdavis@lpccomp.bc.ca
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 */

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/**
 * The Maze Applet.
 * Known Applet parameters:
 * <UL>
 * <LI>x - The X (width) size of the maze - defaults to 5.
 * <LI>y - The Y (height) size of the maze - defaults to 5.
 * <LI>z - The Z (levels) size of the maze - defaults to 1.
 * <LI>WIDTH - The Window display width, defaults to 300.
 * <LI>HEIGHT - The Window display height, defaults to 300.
 * </UL>
 */
public class MazeApplet extends Applet {

	Maze model;
	Maze3D disp;
	Coordinate3D size;
	int dispWidth,dispHeight;
	public MazeApplet() {
	}

	/**
	 * Initialize the Applet.
	 * Create the Maze, set a random finish
	 * and display in a Maze3D component.
	 */
	public void init() {
		size = new Coordinate3D(
			getNumericParameter("x",5),
			getNumericParameter("y",5),
			getNumericParameter("z",1));
		model = new Maze(
			getNumericParameter("x",5),
			getNumericParameter("y",5),
			getNumericParameter("z",1));
		model.setRandomFinish();
		//MazeTextDisplay text = new MazeTextDisplay(m);
		//text.display(null,null);
		dispWidth = getNumericParameter("WIDTH",300);
		dispHeight = getNumericParameter("HEIGHT",330)-30;
		disp = new Maze3D(model, dispWidth, dispHeight);
		//add(disp);
		setLayout(new BorderLayout());
		add(disp,BorderLayout.NORTH);

		Button restartButton = new Button("New Maze");
		restartButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				remove(disp);
				Maze m2 = new Maze(size);
				m2.setRandomFinish();
				disp = new Maze3D(m2,dispWidth,dispHeight);
				add(disp,BorderLayout.NORTH);
				validate();
				repaint();
			}
		});

		Panel buttonPanel = new Panel();
		buttonPanel.add(restartButton);

		add(buttonPanel,BorderLayout.SOUTH);
		//pack();
		//app.show();
	}

	/**
	 * Get a numeric Applet parameter.
	 * @param name The parameter name.
	 * @param defaultValue A default value to use if the parameter
	 *	is not set.
	 * @return The parameter value if set, otherwise the default value.
	 */
	private int getNumericParameter(String name, int defaultValue) {
		String p = getParameter(name);
		if ( p == null )
			return defaultValue;
		try {
			return Integer.parseInt(p);
		}
		catch ( NumberFormatException e ) {
			return defaultValue;
		}
	}
}