import java.awt.*;
import java.awt.event.*;
import java.applet.*;

//-----------------------Traffic light class------------------------------
class Light
	{
		int x, y, w, h;
		boolean s;
		Light(int x, int y, int w, int h, boolean s)
		{
			this.x = x;
			this.y = y;
			this.w = w;
			this.h = h;
			this.s = s; //Light status Green/Red (True/False)
		}
		
		public void draw (Graphics g)
		{
			g.setColor(Color.black);
			g.fillRect(x, y, w, h);
			
			//Red
			if (s == false) {
				g.setColor(Color.red);
				g.fillOval(x, y, w, h/2); //Upper
				g.setColor(Color.black);
				g.fillOval(x, y+h/2, w, h/2); //Lower
			//Green
			}else if (s == true) {
				g.setColor(Color.black);
				g.fillOval(x, y, w, h/2); //Upper
				g.setColor(Color.green);
				g.fillOval(x, y+h/2, w, h/2); //Lower
			}			
		}
		
	}
//-----------------------END of Traffic light class------------------------------

//-----------------------Car class------------------------------
class Car
	{
		int x, y, w, h, s;
		Car(int x, int y, int w, int h, int s)
		{
			this.x = x;
			this.y = y;
			this.w = w;
			this.h = h;
			this.s = s; //status up/down/left/right
		}
		
		public void move (int x, int y)
		{
			this.x = x;
			this.y = y;
		}
		
		public void draw (Graphics g)
		{
			g.setColor(Color.black);
			
			//Status 0 - up
			if (s == 0) 
			{
			g.fillRect(x, y, w, h);
			g.fillOval(x, y-20, w, h);
			//Status 1 - down
			}else if (s == 1) 
			{
			g.fillRect(x, y, w, h);
			g.fillOval(x, y+20, w, h);
			//Status 2 - left
			}else if (s == 2)
			{
			g.fillRect(x, y, h, w);
			g.fillOval(x-20, y, h, w);	
			//Status 3 - right
			}else if (s == 3)
			{
			g.fillRect(x, y, h, w);
			g.fillOval(x+20, y, h, w);	
			}
			
			
			
		}
		
	}
//-----------------------END of Car class------------------------------

public class Applet1 extends Applet implements ActionListener
{
	Light L;
	Car C;
	Button ChangeLight;
	Button Up, Down, Left, Right, Garage;
	
	public void init()
	{
		//Button layout 0 if not set (Buttons will not show up, if their cordinates will not be entered with setBounds).
		setLayout(null);
		
		//Creat L - Light & C - Car objects
		L = new Light(100, 50, 60, 100, false);
		C = new Car(400, 100, 60, 50, 0);
				
		//Buttons
		ChangeLight = new Button("Change light");
		Up = new Button("Up");
		Down = new Button("Down");
		Left = new Button("Left");
		Right = new Button("Right");
		Garage = new Button("Garage");
		
		//Set button bounds
		Up.setBounds(240,180,40,20);
		Down.setBounds(240,220,40,20);
		Left.setBounds(200,200,40,20);
		Right.setBounds(280,200,40,20);
		Garage.setBounds(240,200,40,20);
		ChangeLight.setBounds(215,250,90,20);
		
		//Add buttons 
		add(ChangeLight);add(Up);add(Down);add(Left);add(Right);add(Garage);
		
		//Add listener to buttons
		ChangeLight.addActionListener(this);
		Up.addActionListener(this);
		Down.addActionListener(this);
		Left.addActionListener(this);
		Right.addActionListener(this);
		Garage.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent ae) 
	{
			String str = ae.getActionCommand();
			if (str == "Change light") 
			{
				if (L.s == false) 
				{
					L.s = true;
				}else{
					L.s = false;	
				}
			}else if (str == "Up" && L.s == true) 
			{
				C.s = 0;
				C.move(C.x, C.y-10);
			}else if (str == "Down" && L.s == true) 
			{	
				C.s = 1;
				C.move(C.x, C.y+10);
			}else if (str == "Left" && L.s == false)
			{
				C.s = 2;
				C.move(C.x-10, C.y);
			}else if (str == "Right" && L.s == false)
			{
				C.s = 3;
				C.move(C.x+10, C.y);
			}else if (str == "Garage")
			{
				C.s = 0;
				C.move(400, 100);
			}
			repaint();
	}
	
	public void paint(Graphics g) 
	{
		L.draw(g);
		C.draw(g);
	}

}

