import java.awt.*;
import java.applet.*;
import java.awt.event.*;	//  new

public class SimpleFollowTheClick extends Applet implements MouseListener, KeyListener  // modified
{
	//  Declare state variables
	private int x, y;
	private Color c;

	public SimpleFollowTheClick( )  //  CONSTRUCTOR - initialize state variables
	{
		x = 50;
		y = 100;
	}

	public void init ( )  //  NEW METHOD
	{
		addMouseListener( this );
		addKeyListener( this );
		requestFocus( );
	}

	public void paint ( Graphics g )
	{
		paintArrow( g );
		paintWords( g );
		System.out.println( "New location: x = "+x+" and y = "+y );
	}

	private void paintArrow( Graphics g )
	{

		g.setColor(c);


        //  Paint an arrowhead

		Polygon p = new Polygon( );
		p.addPoint( x, y );
		p.addPoint( x+12, y-12 );
		p.addPoint( x+8, y );
		p.addPoint( x+12, y+12 );
		g.fillPolygon( p );
	}

	private void paintWords( Graphics g )
	{
		g.setColor( Color.black );
		g.setFont( new Font( "SansSerif", Font.BOLD, 12 ) );
		g.drawString( "You clicked here", x+15, y+5 );
	}

	// Mouse Events

	public void mouseClicked( MouseEvent e) {}
	public void mouseEntered( MouseEvent e) {}
	public void mouseExited( MouseEvent e) {}
	public void mouseReleased( MouseEvent e) {}
	public void mousePressed( MouseEvent e)
	{
		x = e.getX( );
		y = e.getY( );
		repaint( );
	}

	// Key Events

	public void keyTyped( KeyEvent e ) { }
	public void keyReleased( KeyEvent e ) { }
	public void keyPressed( KeyEvent ke )
	         {
				 char new_char;
				 new_char = ke.getKeyChar();
				 if(new_char == 'O' || new_char == 'o')
				    c = Color.orange;
				    else if(new_char == 'Y' || new_char == 'y')
				         c = Color.yellow;
				  repaint();
		     }

}













