/*
 * File: RotaryDemoApplet.java
 *
 * Demonstration of animated applet with a Wankel Rotary engine.
*/

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

/* This is the Applet class. It is also a thread that runs the animation.
*/
public class RotaryDemoApplet extends Applet implements Runnable
{
	int			xSize, ySize;
	long			milliPause = 25,
				ts1, ts2;
	volatile boolean	isRunning;
	RotaryAnimation		rAnim;
	Thread			animTh;

	public void init()
	{
		xSize = getSize().width;
		ySize = getSize().height;
		rAnim = new RotaryAnimation(xSize, ySize);
		rAnim.init();
	}

	public void paint(Graphics g)
	{
		rAnim.draw(g);
	}

	public void start()
	{
		isRunning = true;
		animTh = new Thread(this);
		animTh.start();
	}

	public void stop()
	{
		isRunning = false;
	}

	public void run()
	{
		while(isRunning)
		{
			// Get the current timestamp
			ts1 = System.currentTimeMillis();

			// Advance the animation
			rAnim.inc(4);
			repaint();

			// Wait for next time chunk (on real time clock)
			ts2 = System.currentTimeMillis();
			if (ts2 - ts1 < milliPause)
			{
				try
				{
					Thread.sleep(milliPause - (ts2 - ts1));
				}
				catch (InterruptedException e)
				{
					;
				}
			}
		}
	}
}
