import java.awt.*;
import java.applet.*;
import java.util.*;

public class Stars extends Applet implements Runnable
{
	Thread thread;
	Image offi;
	Graphics offg;
	
	public static final int STARS = 256;
	
	float starx[] 	= new float[STARS];
	float stary[]	= new float[STARS];
	int starplane[] = new int[STARS];
	
	Color colors[]	= new Color[4];
	
	boolean running = true;
	
	public void init()
	{
		Random r = new Random();
		for (int i = 0; i < STARS; i++)
		{
			starx[i]		= Math.abs(r.nextInt()) % width;
			stary[i]		= Math.abs(r.nextInt()) % height;
			starplane[i]	= Math.abs(r.nextInt()) % 4;
		}
		colors[0]	= new Color(0, 0, 0);
		colors[1] 	= new Color(112, 112, 112);
		colors[2]	= new Color(180, 180, 180);
		colors[3]	= new Color(255, 255, 255);
		
		setBackground(Color.black);
		setFont(new Font("SansSerif", Font.PLAIN, 10));
		offi = createImage(320, 200);
		offg = offi.getGraphics();
	}
	
	public void start()
	{
		if (thread==null)
		{
			thread = new Thread(this);
			running = true;
			thread.start();
		}
	}
	
	public void stop()
	{
		if (thread!=null)
		{
			running = false;
		}
	}
	
	public void paint(Graphics g)
	{
		g.drawImage(offi, 0, 0, this);
	}
	
	public void update(Graphics g)
	{
		paint(g);
	}
	
	public void run()
	{
		long frames = 0;
		long starttime = System.currentTimeMillis();
		long endtime = System.currentTimeMillis();
		long time = 0;
		long fps = 0;
		while (true)
		{
			try
			{
				Random r = new Random();
				offg.setColor(Color.black);
				offg.fillRect(0, 0, 320, 200);
				for (int i = 0; i < STARS; i++)
				{
					stary[i] += (float)(1f+starplane[i])*0.15f;
					if (stary[i] > height)
					{
						stary[i] = 0;
						starx[i] = Math.abs(r.nextInt()) % width;
					}
					offg.setColor(colors[starplane[i]]);
					offg.drawLine((int)starx[i], (int)stary[i], (int)starx[i], (int)stary[i]);
				}
				frames++;
				endtime = System.currentTimeMillis();
				time = (endtime - starttime)/1000;
				if (time > 0)
					fps = frames / time;
				offg.setColor(Color.white);
				offg.drawString(fps + " FPS", 10, 200-10);
				repaint();
				Thread.sleep(5);
				//showStatus(fps + " FPS");
			}
			catch (Exception ex)
			{
				ex.printStackTrace();
				break;
			}
		}
	}
}