/*
 *	Created by	: 	Mr. C. Whittington
 *	Date		:	June 4, 2004
 *	Purpose		:	To demonstrate the use of a GUI with animation.
 */
 
import javax.swing.*;  
import java.awt.*;
import java.awt.event.*;  


class AnCenterGUI extends JPanel implements ActionListener {
	private JTextField tf;
	private JButton b1;
	private JButton b2;
	private JButton b3;
	private JButton b4;
	private JButton b5;
	
	// IAP will be the panel that we use in the center of the layout.
	private ImprovedAnimationPanel IAP;

	public AnCenterGUI()
	{
		// Sets the layout of the JPanel to be a BorderLayout
		super(new BorderLayout());	
		
		// Method of JComponent
		setBackground(Color.white);
		setSize(new Dimension(500,500));

		// Create 4 buttons. What is in quotes appears on the button
		b1=new JButton("Left");
		b2=new JButton("Right");
		b3=new JButton("Up");
		b4=new JButton("Down");
		
		// Create a new ImprovedAnimationPanel
		IAP = new ImprovedAnimationPanel();
		
		// 1. SET ACTION COMMAND
		// This command sets the text string that can be accessed
		// by the action event (see 2.)
		b1.setActionCommand("Left");
		b2.setActionCommand("Right");
		b3.setActionCommand("Up");
		b4.setActionCommand("Down");

		// Adds the buttons to the pane
		add(b1, BorderLayout.WEST); 
		add(b2, BorderLayout.EAST);
		add(b3, BorderLayout.NORTH);
		add(b4, BorderLayout.SOUTH);
		add (IAP, BorderLayout.CENTER);
		
		// Sets an action listener for each button
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		b4.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent e){
		// 2. GET ACTION COMMAND
		// e is the name of the action event
		// Using e you can access the Action Command that we set
		// earlier (see 1).
		
		
		// Depending on which button is pressed ball in the IAP is moved around
		// the screen.
		if (e.getActionCommand().equals("Up"))
		{
			IAP.j=IAP.j-10;
		}
		else if (e.getActionCommand().equals("Down"))
		{
			IAP.j=IAP.j+10;
		}
		else if (e.getActionCommand().equals("Left"))
		{
			IAP.i=IAP.i-10;
		}
	    else if (e.getActionCommand().equals("Right"))
		{
			IAP.i=IAP.i+10;
		}
		
		// We could schedule a repaint of just the IAP with the following line.
		// We do not need to do this in this case because the timer repaints 
		// that component every hundredth of a second anyway.
		//IAP.repaint();
	}
}