
import java.awt.*;
import java.applet.*;
import java.awt.event.*;	//  new

public class trisha extends Applet implements MouseListener, KeyListener  // modified
{
	//  Declare state variables
	int x, y;
	int size;
	Color mycolor;

	public trisha()  //  CONSTRUCTOR - initialize state variables
	{
		x=200;
		y=100;
		size = 50;
		mycolor=Color.green;
	}

	public void init ( )  //  NEW METHOD
	{
		addMouseListener( this );
		addKeyListener( this );
		requestFocus( );
	}

	public void paint ( Graphics g )
	{
		paintBox(g);
		moveUp(g);
		yellow(g);
	}

	private void paintBox (Graphics g)
	{
		g.setColor (mycolor);
		g.fillRect(x, y, size, size);
	}
	private void moveUp (Graphics g)
	{
		g.drawRect(300, 20, 80, 20);
		g.drawString("Move Up", 315, 35);
	}
	private boolean moveUpContains(int mx, int my)
	{
		if (300 < mx && mx < 380 && 20 < my && my < 40)
			return true;
		return false;
	}
	private void yellow (Graphics g)
	{
		g.drawRect(300, 50, 80, 20);
		g.drawString("Yellow", 315, 65);
	}
	private boolean yellowContains(int mx, int my)
	{
		if (300 < mx && mx < 380 && 50 < my && my < 70)
			return true;
		return false;
	}



	// 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)
	{
		int mx = e.getX();
		int my = e.getY();

		if (moveUpContains(mx,my) && y>0)
			y-=20;
		if (yellowContains(mx, my))
			mycolor=Color.yellow;
		repaint();
	}

	// Key Events

	public void keyTyped( KeyEvent e ) { }
	public void keyReleased( KeyEvent e ) { }
	public void keyPressed( KeyEvent e )
	{
		char key = e.getKeyChar();

		if (key == 'u' || key == 'U')
		{
			if(y>0)
				y-=20;
		}
		if (key == 'y' || key == 'Y')
			mycolor = Color.yellow;
		repaint();
	}
}



