//**********************************************************************

// bounce.java:	Applet

//**********************************************************************

import java.applet.*;

import java.awt.*;



//========================================================================

// Main Class for applet bounce

//========================================================================

public class bounce extends Applet

{

	// bouncing string, it's height, & speed in the X direction

	static final int	heightChar	= 10;	

	static final int	deltaX		= 2;

	private String		msg = "ball";

	private Font		font;



	// applet dimensions

	private int		width	= 0;

	private int		height	= 0;

	private int[]	XBorder = new int[5];

	private int[]	YBorder = new int[5];



	// position, speed, acceleration of the message

	private int			X, Y;	

	private double		deltaY;

	private int			rolling		= 0;

	private double		accelY		= 1.2;

	private double		elasticity	= 0.8;

	private Choice		lAccel = new Choice();

	private Choice		lElast = new Choice();



	// separate threaded class to control the motion

	private controller	cont;





	// bounce Class Constructor

	//--------------------------------------------------------------------

	public bounce()

	{

		// One time initializations



		// controller needs to be a separate thread.

		cont = new controller();

		cont.setBounce(this);



		font = new Font("Helvetica", 

				Font.BOLD, heightChar);



		lAccel.addItem("moon");

		lAccel.addItem("Mercury");

		lAccel.addItem("Earth");

		lAccel.addItem("Jupiter");

		lAccel.addItem("Sol");

		lAccel.select("Earth");

		add(new Label("Gravity:"));

		add(lAccel);



		lElast.addItem("rock");

		lElast.addItem("rabbit");

		lElast.addItem("ball");

		lElast.addItem("super ball");

		lElast.select("ball");

		add(new Label(" Elasticity:"));

		add(lElast);

		

		RepeatableInitPrivate();

	}





	// APPLET HANDLES

	//--------------------------------------------------------------------

	public void resize(int w, int h)

	{

		width	= w-1;

		height	= h-1;



		XBorder[0] = 0;

		XBorder[1] = w-1;

		XBorder[2] = w-1;

		XBorder[3] = 0;

		XBorder[4] = 0;

		YBorder[0] = 3 * heightChar;

		YBorder[1] = 3 * heightChar;

		YBorder[2] = h-1;

		YBorder[3] = h-1;

		YBorder[4] = 3 * heightChar;

	

		super.resize(w, h);

	}

	

	public String getAppletInfo()

	{

		return "\r\n" +

		       "shite heid applets!\r\n" +

		       "\r\n" +

		       "";

	}





	public void init()

	{

		cont.start();

	}



	public void destroy()

	{

	}



	public void paint(Graphics g)

	{

		g.drawPolygon(XBorder, YBorder, 5);



		g.drawString(msg, X, Y);

		cont.IsPainted= true;

	}



	public void start()

	{

		cont.resume();

	}

	

	public void stop()

	{

		cont.suspend();

	}



	public boolean action(Event event, Object arg)

	{

		if(event.target == lAccel)

		{

			if (arg.equals("moon"))

				accelY = .07;

			else if (arg.equals("Mercury"))

				accelY = .15;

			else if (arg.equals("Earth"))

				accelY = .4;

			else if (arg.equals("Jupiter"))

				accelY = .7;

			else if (arg.equals("Sol"))

				accelY = .95;

			else 

				accelY = .4;



			RepeatableInitPrivate();

			return true;

		}

		else if(event.target == lElast)

		{

			//msg = lElast.

			if (arg.equals("rock"))

				elasticity	= 0;

			else if (arg.equals("rabbit"))

				elasticity	= 0.2;

			else if (arg.equals("ball"))

				elasticity	= 0.6;

			else if (arg.equals("super ball"))

				elasticity	= 0.8;

			else 

				elasticity	= 0.2;		



			RepeatableInitPrivate();

			return true;

		}





		return false;

	}





	// INTERNAL FUNCTIONS

	//--------------------------------------------------------------------

	public void Move()

	{

		// this function causes the unnatural accelleration.

		// it also forces a repaint



		X += deltaX;

		if (X >= width) {

			RepeatableInitPrivate();

		}

			

		if(rolling == 0)

		{

			deltaY = deltaY + accelY;



			if (Y >= height) {

				Y		= height;

				deltaY	= -deltaY * elasticity;

				

				if(	Math.abs(deltaY) < 1)

					rolling = 1;

			}



			Y += deltaY;

		} else {

			rolling ++;

			if (rolling == 10)

			{

				RepeatableInitPrivate();

			}

		}

		

		repaint();

	}



	void RepeatableInitPrivate()

	{

		X = 0;

		Y = 4 * heightChar;

		deltaY = 0;

		rolling = 0;

	}

}


