//------------------------//
//   stars.java applet    //
//------------------------//
// by Craig Comstock 1998 //
//------------------------//

import java.applet.*;

import java.awt.*;



class star extends Object {
	public int x,y,xv,yv,z,count,i;
	public void sInit(Applet a) {
		xv=(int)(16-Math.random()*32);
		yv=(int)(16-Math.random()*32);
		if (xv==0) {sInit(a);}
		if (yv==0) {sInit(a);}
		x=(a.size().width/2)+xv*4;
		y=(a.size().height/2)+yv*4;
		count=10;i=1;
		z=1;
	}
}


class starField implements Runnable {
	private int num_stars=500;
    private star s[];
	private int xo,yo,newXo,newYo;
	private int speed;
	private int wait;
	private int waitIndex=0;
	private Graphics field;
	private Applet a;
	
	public void run() {
	while (true) {
		waitIndex++;
		if (waitIndex>wait) {
			waitIndex=0;
			newXo=(int)(4*(4-Math.random()*8));
			newYo=(int)(4*(4-Math.random()*8));
		}
		if (xo<newXo) {xo++;}
		if (xo>newXo) {xo--;}
		if (yo<newYo) {yo++;}
		if (yo>newYo) {yo--;}
		if (wait<0) {xo=0;yo=0;}
		for (int j=0;j<num_stars;j++) {
			field.setColor(Color.white);
			field.fillRect(s[j].x,s[j].y,s[j].z,s[j].z);
			s[j].x=xo+s[j].x+s[j].xv*s[j].z;
			s[j].y=yo+s[j].y+s[j].yv*s[j].z;
			s[j].i++;if (s[j].i>s[j].count) {s[j].i=1;s[j].z++;}
			if ( (s[j].x<0) || (s[j].x>a.size().width) ) {s[j].sInit(a);}
			if ( (s[j].y<0) || (s[j].y>a.size().height) ) {s[j].sInit(a);}
			if (s[j].z>6) {s[j].sInit(a);}
		//System.out.println("x="+x+"y="+y+"z="+z+"xv="+xv+"yv="+yv+"i="+i+"count="+count);
	    field.setColor(Color.black);
	    field.fillRect(s[j].x,s[j].y,s[j].z,s[j].z);
		}
		try {
		Thread.sleep(speed);
	    } catch (InterruptedException e) {
		 //ignore
	    }
	}
    }
	starField (Applet a,int num_stars,int speed,int wait) {
		this.a = a;
		this.num_stars = num_stars;
		this.speed = speed;
		this.wait = wait;// the number of frames to wait before changing direction
		s = new star[num_stars];
		field = a.getGraphics();
		xo=0;yo=0;
		for (int j=0;j<num_stars;j++) {
			s[j] = new star();
			s[j].sInit(a);
		}
	}
	}

public class stars extends Applet {

	public void init() {
		int num_stars;
		try {
			num_stars = Integer.parseInt(getParameter("num_stars"));
		} catch (Exception e) {
			num_stars = 50;
		}
		int speed;
		try {
			speed = Integer.parseInt(getParameter("speed"));
		} catch (Exception e) {
			speed=1;
		}
		int wait;
		try {
			wait = Integer.parseInt(getParameter("sway_wait"));
		} catch (Exception e) {
			wait=70;
		}
		Thread t= new Thread(new starField(this,num_stars,speed,wait),"star field");
		t.start();		
	}
	public void update(Graphics g) {
		repaint();
	}
	public void paint(Graphics g) {
		g.setColor(Color.white);
		g.fillRect(0,0,this.size().width,this.size().height);
	}
	public String[][] getParameterInfo() {
		String  info[][]  =  {
			{"num_stars",        "integer",        "number of stars (default=50)"},
			{"speed",  "integer",  "delay between animation frames in milliseconds (default=1)"},
			{"sway_wait",      "integer","number of frames to wait before changing direction (-1 causes no change, default=70)"} 
		};
		return info;
	}
	public String getAppletInfo() {
		String s = "stars.class applet written by Craig Comstock, beta 2 - 6/3/97 - freeware, url -- http://users.solve.net/~comet/stars.html, email -- comet@solve.net";
		return s;
	}
}