/**
* @author Amanda Gorski
* @date July 7, 2002
* Bounces a Ball around the screen
*
* Requires Ball.java, run it here
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Bouncer extends JPanel implements ActionListener
{
    JButton bounceBall;
    JButton reset;
    Ball bounce;
    public static final int sizex = 350;
    public static final int sizey = 400;

    public Bouncer ()
    {
	super (new BorderLayout ());
	bounce = new Ball (sizex, sizey); //a new selection bounceBall object (other file)
	setBackground (Color.white);
	//bounceBall button stuff
	bounceBall = new JButton ("Start");
	bounceBall.setActionCommand ("Start");
	bounceBall.addActionListener (this);

	//reset button stuff
	reset = new JButton ("Stop");
	reset.setActionCommand ("Stop");
	reset.addActionListener (this);

	//panel & interface stuff
	JPanel p = new JPanel (new FlowLayout ());
	p.setBackground (Color.white);
	p.add (bounceBall);
	p.add (reset);
	add (p, "North");
	add (bounce, "Center");
    }


    public void actionPerformed (ActionEvent e)
    { //if bounceBall button is presed
	if (e.getActionCommand ().equals ("Start"))
	{ //start a new thread and start bouncing the ball
	    Thread bounceBallingThread = new Thread (bounce, "Ballthread");
	    bounceBallingThread.start ();
	}
	else if (e.getActionCommand ().equals ("Stop"))
	{ //stop the thread 
	    bounce.stopBall ();
	}
    }


    /*Again, not mine: from Sun Systems Java Tutorials
     */
    public static void main (String [] args)
    {
	//Make sure we have nice window decorations.
	JFrame.setDefaultLookAndFeelDecorated (true);

	//Create and set up the window.
	JFrame frame = new JFrame ("Bouncing Ball Demo");
	frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

	//Create and set up the content pane.
	JComponent newContentPane = new Bouncer ();
	newContentPane.setOpaque (true); //content panes must be opaque
	frame.setContentPane (newContentPane);
	frame.setSize (sizex, sizey+65);

	//Display the window.
	frame.setVisible (true);
    }
}
