THREAD_CaritaAnimada.java



// SE MUESTRA UNA CARITA HACIENDO GUINIOS
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;


class CaritaAnimada extends JFrame
{
	Grafico G = new Grafico();
	
	public static void main(String R[]) { new CaritaAnimada(); }

	public CaritaAnimada()
	{
		super("Animacion 2.0");
		setBounds(0, 0, 800, 500);
		setVisible(true);

		add(G); G.setBounds(0, 0, 800, 500);
	}
	

	protected void processWindowEvent(WindowEvent e)
        { if (e.getID() == WindowEvent.WINDOW_CLOSING) System.exit(0); }

}


class Grafico extends JPanel implements Runnable
{
	Thread H = null;
	int Objeto = 1;			// 1 Izq, 2 Der, 3 Boca
	int Cerrado = 2, Abierto = 40;

	public Grafico()
	{
		H = new Thread(this);
		H.start();
	}

	public void paint(Graphics G)
	{
		G.setColor(Color.blue); G.fillRect(0, 0, 800, 500);
		G.setColor(Color.white);
		G.drawOval(50, 50, 300, 300);

		Ovalo(100, 120, 80,  40, G, 1);
		Ovalo(220, 120, 80,  40, G, 2);
		Ovalo(120, 250, 160, 60, G, 3);

		G.drawRect(200, 150, 10, 80);
	}

	void Ovalo(int x, int y, int w, int h, Graphics G, int Elem)
	{
		if (Elem == Objeto) h = Cerrado;
		G.drawOval(x, y, w, h);
	}

	public void run()
	{
		while(true)
		{
			repaint();
			try { H.sleep(1000); } catch (InterruptedException e) {}
			Objeto++;
			if (Objeto == 4) Objeto = 1;
		}
	}
}