//show use of keyboard to move a dot
//modified from computer insights Arnold UTM
//mbooth 2005 12

//import section
import javax.swing.*;//gui
import java.awt.*;   //events
import java.awt.image.*;
import java.awt.event.*;

//class declaration inherits panel and keylistener
class KeyPressPanel extends JPanel implements KeyListener
{
    //field declarations
	private int intX,
        intY; // The current position of the ball

    //constructor-set up panel
	public KeyPressPanel()
    {
		setBackground(Color.black);//panel background
		intX=100;//initialize coordintates
        intY=100;
		addKeyListener(this);//attach keylistener to panel code
		                     //-keyPressed, keyTyped, keyReleased
	}//end of constructor

    //draw on panel method
	public void paintComponent(Graphics g)
    {
		super.paintComponent(g);//override Jpanel method
		g.setColor(Color.white);//set dot colour
		g.fillOval(intX,intY, 10, 10);//make dot
		requestFocus();//give this dot keyboard focus
	}//end of paintComponent

	//keyPressed method (keylistener requires this)
	//we don't want to use this since the dot won't move
	//if we hold the keydown
    public void keyPressed(KeyEvent e)
    {
        //what key was pressed? testing only
        //System.out.println("Just pressed" + e.getKeyChar());
    }

    //keyReleased method(keylistener requires this)
	public void keyReleased(KeyEvent e)
    {
    }

    //keyboard code repeats code sent from keylistener
	public void keyTyped(KeyEvent e)
    {
        //constants
        final int intStep = 4; //number of pixels to move

		//decide the location of the dot
        switch(e.getKeyChar())
        {
			case 'o': //o chosen
                 intY=intY-intStep;//move up
                 break;
			case 'k': //k chosen
                 intX=intX-intStep;//move left
                 break;
			case ';': //; chosen
                 intX=intX+intStep;//move right
                 break;
			case '.': //. chosen
                 intY=intY+intStep;//move down
                 break;
			default:
		}
		repaint(); // redraw the dot
	} //end of keyboard code
}//end of panel class
