//show use of keyboard to move a dot
//show another dot moving on the screen also
//modified from computer insights Arnold UTM
//mbooth 2005 12

//import section
import javax.swing.*;//gui
import java.awt.*;   //events
import java.awt.event.*;
import java.util.Random;//random numbers
import javax.swing.Timer;//time moving dot


//class declaration inherits panel and keylistener, actionlistener
class KeyDotsPanel extends JPanel
                   implements KeyListener,ActionListener
{
    //field declarations
	private int intX, // The current position of
                intY, //keyboard controlled ball
                intMX,
                intMY,//current position of moving ball
                intDX,
                intDY,//current direction of moving ball
                intTickCount;//ball speed

    //constructor-set up panel
	public KeyDotsPanel()
    {
        //variable declaration
        Timer timer = new Timer(10, this);//with delay,listener
        Random r=new Random(); //random moving position, direction

        setBackground(Color.black);//panel background
		intX=100;//initialize coordintates
        intY=100;
		addKeyListener(this);//attach keylistener to panel code
		                     //-keyPressed, keyTyped, keyReleased
		intMX=Math.abs(r.nextInt())%(600/2); //random set position
        intMX=intMX+600/4;                   //of moving ball
		intMY=Math.abs(r.nextInt())%(600/2);
        intMY=intMY+600/4;
        intDX=(Math.abs(r.nextInt())%3)-1;//direction of moving
		intDY=(Math.abs(r.nextInt())%3)-1;//ball
		timer.start(); //start timer
	}//end of constructor


    //moving the ball in constant direction
    public void move()
    {
		intMX=intMX+intDX;//x,y coordinate and direction
        intMY=intMY+intDY;
    }//end of move

    //tracks when to move
    public void tick()
    {
		intTickCount=(intTickCount+1)%2;//try different #'s
		if(intTickCount==0)
        {
            move(); make a move
        }//done if mod 0 check
	}//end of tick

    // Each time a timer event fires, this method is called
	public void actionPerformed(ActionEvent e)
    {
		tick(); //track if move should happen
		repaint();//redraw moving dot
	}

    //draw on panel method
	public void paintComponent(Graphics g)
    {
		super.paintComponent(g);//override Jpanel method
		g.setColor(Color.red); //moving ball
		g.fillOval(intMX,intMY,10,10);
		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
