import java.applet.*;
import java.awt.*;

public class RingZ extends Applet implements Runnable
{
	// Var werk
	int x_pos = 30;			// Ringi x-koordinaat
	int y_pos = 100;		// Ringi y-koordinaat
	int x_speed = 1;		// Ringi kiirus suunas x
	int radius = 20;		// Ringi raadius
	int appletsize_x = 300; // Appleti akna suurus x-teljel
	int appletsize_y = 300;	// Appleti akna suurus y-teljel

	// Var dopelt-puhverdamise jaoks
	private Image dbImage;
	private Graphics dbg;

	public void init()
	{
		setBackground (Color.blue);
	}

	public void start ()
	{
		// Tehakse ks tore uus Thread, milles tegevus toimuma hakkab
		Thread th = new Thread (this);
		// Thread'i alustamine
		th.start ();
	}

	public void stop()
	{

	}

	public void destroy()
	{

	}

	/* See meetod pstab valla reaktsiooni nupuvajutusele */
	public boolean keyDown (Event e, int key)
	{
		// Vasem kursori-nupp
		if (key == Event.LEFT)
		{
			// Ring liigub vasemale
			x_speed = -1;
		}
		// les kursori-nupp
		if (key == Event.UP)
		{
			// Ring liigub kiiremalt
			x_speed = x_speed+1;
		}
		// Parem kursori-nupp
		else if (key == Event.RIGHT)
		{
			// Ring liigub paremale
			x_speed = 1;
		}
		// Alla kursori-nupp
				if (key == Event.DOWN)
		{
			// Ring liigub aeglasemalt
			x_speed = x_speed-1;
		}
		// Space omab vrtust 32
		else if (key == 32)
		{
			x_speed = 0;
		}
		else
		{
			// Vajutatud nuppude this ja vrtus standardvljundile
			System.out.println ("Charakter: " + (char)key + " Integer Value: " + key);
		}

		return true;
	}

	public void run ()
	{
		// ThreadPriority vhendamine, et joonitamist kergendada
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

		// Kuni on "true", jookseb thread edasi
		while (true)
		{
			// Ring prkub, puutudes kokku parema appleti rega
			if (x_pos > appletsize_x - radius)
			{
				// Ringi suunamuutus
				x_speed = -1;
			}
			// Ring prkub, puutudes kokku vasema appleti rega
			else if (x_pos < radius)
			{
				// Ringi suunamuutus
				x_speed = +1;
			}

			// x-koordinaatide muutmine
			x_pos += x_speed;

			// Applet joonistatakse le
			repaint();

			try
			{
				// Threadi peatamine sulgudes etteantud millisekundiks
				Thread.sleep (20);
			}
			catch (InterruptedException ex)
			{
				// Ei tee midagi
			}

			// ThreadPriority tagasimramine maksimaalvrtusele
			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
		}
	}

	/** Update - meetod, dopeltpuhvri realiseerimine (pildi vbelemise rahoidmiseks) */
	public void update (Graphics g)
	{
		// Dopelt-puhvri initsialiseerimine
		if (dbImage == null)
		{
			dbImage = createImage (this.getSize().width, this.getSize().height);
			dbg = dbImage.getGraphics ();
		}

		// Ekraan kustutatakse (tagataustal)
		dbg.setColor (getBackground ());
		dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

		// Kustutatud taustale joonistatakse esipind
		dbg.setColor (getForeground());
		paint (dbg);

		// Valmisjoonistatud pilt igele ekraanile
		g.drawImage (dbImage, 0, 0, this);
	}

	public void paint (Graphics g)
	{
		g.setColor  (Color.red);

		g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
	}
}

