//draw a dot and make it move based on button selected
//modified from computer insights Arnold UTM
//mbooth 2005 12

//import section
import javax.swing.*;//gui
import java.awt.*;//aplication windowing toolkit
import java.awt.event.*; //action

//class declaration inherits JPanel and Listener
class ButtonBallPanel extends JPanel implements ActionListener
{
	//fields
    int intX,
        intY;//coordinates
	JTextField tf; //textbox

	//constructor sets up gui and initializes coordinates
	public ButtonBallPanel()
    {
		//variables
        JButton btnUp;//up button
        JButton btnDown;//down button
        JButton btnLeft;//left button
        JButton btnRight;//right button
        Dimension dim;//frame dimension

		intX=50;//initialize coordinates
		intY=50;
		dim = new Dimension(100,100);//set dimension
		btnUp =new JButton("Up"); //construct buttons
		btnDown=new JButton("Down");
        btnLeft=new JButton("Left");
        btnRight=new JButton("Right");
        tf=new JTextField(10);//construct text box


        // component colour and size
		setBackground(Color.white);
		setMinimumSize(dim);

		//add buttons and textbox to window
		add(btnUp);
		add(btnDown);
		add(btnLeft);
		add(btnRight);
		add(tf);

		//add buttons to listeners
		btnUp.addActionListener(this);
		btnDown.addActionListener(this);
		btnRight.addActionListener(this);
		btnLeft.addActionListener(this);
	} //end of constructor

	//paintComponent method overrides JPanel
	public void paintComponent(Graphics g)
    {
		super.paintComponent(g);//paint on graphic g
		g.fillOval(this.intX,this.intY,10,10);//fill circle
	}

    //action method that moves circle and displays message for direction
	public void actionPerformed(ActionEvent e)
    {
		String strDir;//direction message
        strDir=e.getActionCommand();//get direction from button
		tf.setText(strDir);//display direction in textbox

		//change direction of x,y based on direction chosen
		if(strDir.equals("Up"))
        {
            this.intY--; //decrease y
        }
		else if(strDir.equals("Down"))
        {
             this.intY ++;//increase y
        }
		else if(strDir.equals("Left"))
        {
             this.intX--;//decrease X
        }
		else //must be right
        {
             this.intX++;//increase X
        }
		repaint();//redraw
	} //end of action method
} //end of class