R04_T.java_



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Trafico extends Canvas implements KeyListener
{
	boolean Corriendo = false;	// EstA corriendo el hilo?
	Anima_Num NUMERO[] = new Anima_Num[4];
	int x_a, x_n, y;		// x_anterior, x_nueva, y
	String NUM = "";
	
	public Trafico()
	{
        	addKeyListener(this);
		//Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
		this.requestFocusInWindow();
	}

	public  void paint(Graphics G)	// synchronized
	{
		if (!Corriendo)
		{
			G.setColor(Color.black);  G.fillRect(0, 0, 570, 310);
			G.setColor(Color.yellow); PintaManzanas(G);
		}
		else
		{
			G.setColor(Color.black);  G.drawString(NUM, x_a, y);
			G.setColor(Color.yellow); G.drawString(NUM, x_n, y);
		}
	}
	
	public void update(Graphics G) { paint(G); }
	
	void PintaManzanas(Graphics G)
	{
		int w = 40, h = 30;	// w = width, h= hight
		int x = 10, y = 10;

		for (int R = 0; R < 5; R++)
		{
			x = 10;
			for (int C = 0; C < 8; C++)
			{
				G.drawRect(x, y, w, h);
				x += 70;
			}
			y += 60;
		}
	}
	
	public void Imprime(int x_a, int x_n, int y, String NUM)
	{
		this.x_a = x_a;
		this.x_n = x_n;
		this.y   = y;
		this.NUM = NUM;
		repaint();
	}
	
	public void keyTyped(KeyEvent e)  {}	// No me interesa
	public void keyPressed(KeyEvent e){}	// No me interesa
	public void keyReleased(KeyEvent e)
	{
		String Tecla = e.getKeyText(e.getKeyCode());
		int Numeros = 0;
		if (Tecla.equals("F1") || Tecla.equals("F2") || Tecla.equals("F3") || Tecla.equals("F4"))
		{
			if (!Corriendo)
			{
				Corriendo = true;
				Numeros = e.getKeyCode() - 111;		// N nUmeros
				y = 60;
				for (int n = 0; n < Numeros; n++)
				{
					NUMERO[n] = new Anima_Num(y, n+1, this);
					new Thread(NUMERO[n]).start();
					y += 60;
				}
			}
			else
			{
				switch (e.getKeyCode() - 111)
				{
					case 1:		// F1
						if (NUMERO[0].CORRIENDO) NUMERO[0].CORRIENDO = false;
						else { NUMERO[0].CORRIENDO = true; new Thread(NUMERO[0]).start(); }
						break;
					case 2:		// F2
						if (NUMERO[1].CORRIENDO) NUMERO[1].CORRIENDO = false;
						else { NUMERO[1].CORRIENDO = true; new Thread(NUMERO[1]).start(); }
						break;
					case 3: 	// F3
						if (NUMERO[2].CORRIENDO) NUMERO[2].CORRIENDO = false;
						else { NUMERO[2].CORRIENDO = true; new Thread(NUMERO[2]).start(); }
						break;
					case 4:		// F4
						if (NUMERO[3].CORRIENDO) NUMERO[3].CORRIENDO = false;
						else { NUMERO[3].CORRIENDO = true; new Thread(NUMERO[3]).start(); }
				}
			}
		}
		else if (Tecla.equals("Introduzca"))	// Es el enter
			ALTO();
	}
	
	void ALTO()
	{
		for (int k = 0; k < 4; k++)
			if (NUMERO[k] != null)
			{
				NUMERO[k].CORRIENDO = false;
				NUMERO[k] = null;
			}
		this.setVisible(false);
		Corriendo = false;
	}
}

class Anima_Num extends Thread
{
	int x_a, x_n, y, SLEEP;
	String NUM = "", TECLA = "";
	Trafico TRAFICO;
	boolean CORRIENDO = true;

	public Anima_Num(int y, int numero, Trafico TRAFICO)
	{
		this.y = y;
		this.NUM = "" + numero;
		this.TRAFICO = TRAFICO;
		this.setPriority(5);	// PRIORIDAD NORMAL
	}

	public void run( )
	{
		while(CORRIENDO)
		{
			try { Thread.sleep(50); }
			catch(InterruptedException r){}
			x_n =  (x_n == 570) ? 0 : x_n+10;
			TRAFICO.Imprime(x_a, x_n, y, NUM);
			x_a = x_n;
		}
	}
}