/*

Bounce.java

The Bounce class is the applet.  It does initialization, sets up the
user interface, handles user events, and lets the Stage class do
everything else.

*/

import java.awt.*;
import java.lang.*;
import java.applet.*;
import java.net.*;

public class Bounce extends Applet {

    public Stage      stage;

    public AudioClip  sound;
    public URL        url;
    
    GridBagLayout     layout;
    Button            addButton  = new Button( "add ball" );
    Button            fastButton = new Button( "faster" );
    Button            slowButton = new Button( "slower" );


    public void init() {
	setBackground( Color.white );
	layout = new GridBagLayout();
        setLayout( layout );
	int width = Integer.parseInt( getParameter( "width" ).trim() );
	int height = Integer.parseInt( getParameter( "height" ).trim() );
        stage = new Stage( this,width-100,height-50 );
	constrain( stage,this,layout,1,0,1,3,
		   GridBagConstraints.NONE,
		   GridBagConstraints.CENTER,
		   0,0 );
	constrain( addButton,this,layout,0,0,1,1,
		   GridBagConstraints.BOTH,
		   GridBagConstraints.CENTER,
		   0.3,0 );
	constrain( fastButton,this,layout,0,1,1,1,
		   GridBagConstraints.BOTH,
		   GridBagConstraints.CENTER,
		   0.3,0 );
	constrain( slowButton,this,layout,0,2,1,1,
		   GridBagConstraints.BOTH,
		   GridBagConstraints.CENTER,
		   0.3,0 );
	try {
	    url = new URL( "http://www.columbia.edu/~cs3157/examples/bounce/ding.au" );
	    sound = getAudioClip( url );
	    sound.play();
	}
	catch( MalformedURLException murlx ) {
	    System.out.println( "error: " + murlx );
	}
    } // end of init()


    public void start() {
        stage.start();
    } // end of start()


    public void stop() {
        stage.stop();
    } // end of stop()


    public void destroy() {
    } // end of destroy()

    
    public boolean action( Event evt, Object arg ) {
	if ( evt.id == Event.ACTION_EVENT ) {
	    if ( evt.target == addButton ) {
		stage.addBall();
		return true;
	    }
	    else if ( evt.target == fastButton ) {
		stage.faster();
		return true;
	    }
	    else if ( evt.target == slowButton ) {
		stage.slower();
		return true;
	    }
	}
	return false;
    } // end of action()

    
    /*----- constrain --------------------------------------*/
    /**
     * This method adds <code>Component component</code> to
     * <code>Container container</code> with a 
     * <code>GridBagLayout layout </code> manager,
     * according to the constraint values specified by the
     * method arguments.
     * 
     * @param	component	component to add
     *
     * @param	container	container to which 
     *				"component" is added
     *
     * @param	layout		grid bag layout manager 
     *				for "container"
     *
     * @param     gridx		the left-most cell of the
     *				component's display area
     *
     * @param     gridy		the top-most cell of the
     *				component's display area
     *
     * @param     gridwidth	number of cells in a row
     *				for component's display area
     *
     * @param     gridheight	number of cells in a column
     *				for component's display area
     *
     * @param     fill		indicates, when the 
     *				component's display area is
     *				larger than the component's
     *				requested size, whether/how
     *				to resize the component
     *
     * @param     anchor	indicates, when the 
     *				component is smaller than 
     *                          its display area, where to
     *				place the component within
     *				the area
     *
     * @param     weightx	how to distribute extra
     *				vertical space
     *
     * @param     weighty	how to distribute extra
     *				horizontal space
     *
     */
    public static void constrain(
				 Component     component,
				 Container     container,
				 GridBagLayout layout,
				 int           gridx,
				 int           gridy,
				 int           gridwidth,
				 int           gridheight,
				 int           fill,
				 int           anchor,
				 double        weightx,
				 double        weighty
				 ) {
	
	GridBagConstraints gbc = new GridBagConstraints();
	
	gbc.gridx      = gridx; 
	gbc.gridy      = gridy;
	gbc.gridwidth  = gridwidth; 
	gbc.gridheight = gridheight;
	gbc.fill       = fill; 
	gbc.anchor     = anchor;
	gbc.weightx    = weightx;
	gbc.weighty    = weighty;
	
	layout.setConstraints( component,gbc );
	
	container.add( component );
	
    } // end of constrain()



} // end of Bounce class
