import java.awt.*;
import java.applet.*;
import java.awt.event.*;	//  new

public class FollowTheClick
       extends Applet implements MouseListener, KeyListener  // modified
{

	//  Other variables
	private Color purple = new Color(200,20,200);

	//  Declare state variables
	private int x, y;
	private Color current_color = purple;
	private Color mikes_color= Color.white;
	private boolean arrow_size;   // "false" means small, and "true" means big
    private int delta = 0;


	public FollowTheClick( )  //  CONSTRUCTOR - initialize state variables
	{
		x = 50;
		y = 100;
		arrow_size = false;
	}

	public void init ( )  //  NEW METHOD
	{
		addMouseListener( this );
		addKeyListener( this );
		requestFocus( );
	}

	public void paint ( Graphics g )
	{
		paintArrow( g );
		paintWords( g );
		paintButton(g);
		System.out.println( "New location: x = "+x+" and y = "+y );
	}

    private void paintButton(Graphics go)
    {
		Color old_color = go.getColor();
		go.setColor(purple);
        go.fillRect(23,33,80,20);
        go.drawRect(19,29,82,22);
        go.setColor(mikes_color);
		go.fillRect(20,30,80,20);
		go.setColor(Color.orange);
		go.drawString("Arrow Size",23,45);



		go.setColor(old_color);
	}

	private void paintArrow( Graphics g )
	{
		g.setColor(current_color);
		Polygon p = new Polygon( );
		p.addPoint( x, y );
		p.addPoint( x+12+delta, y-12-delta );
		p.addPoint( x+8+delta, y );
		p.addPoint( x+12+delta, y+12+delta );
		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)
		{
			System.out.println("OUCH!!!)");
			}
	public void mouseExited( MouseEvent e) {}
	public void mouseReleased( MouseEvent e)
	{
		mikes_color = Color.white;
		repaint();
	}
	public void mousePressed( MouseEvent mouse_e)
		{
			//  Check if button was pressed,
			//   if so, then change the size of the
			//   arrow.
			int x1 = mouse_e.getX();
			int y1 = mouse_e.getY();

            //  Check x,y of mouse click.  If within the rect.
            //  button area, then...
			if( x1 < 100 && x1> 20 &&
			   y1 < 50 && y1 > 30)
               {
			      delta++;				//b  increment the size of the arrow head
			      mikes_color = Color.yellow;  // and change the color
			      repaint();				//  and repaint the window.
			   }

			   else
			     {                    // Move the arrow head
					x = x1;
					y = y1;
					if ( x < 100 )
						{current_color = Color.red;}
						else
						     if ( x < 200 )
							     current_color = Color.green;
							     else
							     	{
								    	current_color = purple;
								    }
					repaint( );
				  }

	   }
	// Key Events

	public void keyTyped( KeyEvent e ) { }
	public void keyReleased( KeyEvent e ) { }
	public void keyPressed( KeyEvent e ) {
		if(e.getKeyChar() == 'b')
		   delta += 5;
		   else if(e.getKeyChar() == 'B')
		       delta += 10;
		       else if(e.getKeyChar() == 'r')
		            delta = 0;
		            else if(e.getKeyChar() == '+')
		                 delta--;
		repaint();
		}
}













