import java.awt.*;
import java.lang.*;
import java.applet.Applet;

/**
* author: conrad plake
*/

public class StarfieldApplet extends java.applet.Applet implements Runnable{
   //String p_FrameWidth;
   //String p_FrameHeight;
   String p_text;
   String p_Stars;
   int FrameWidthi = 10, FrameHeighti = 10;

  public void init(){
    // Validate input parameters...
    /*if ((p_FrameWidth = getParameter("p_FrameWidth")) == null)
         p_FrameWidth = String.valueOf(this.getSize().width);
    if ((p_FrameHeight = getParameter("p_FrameHeight")) == null)
         p_FrameHeight = String.valueOf(this.getSize().height);
    Integer frameW = new Integer(p_FrameWidth);
    Integer frameH = new Integer(p_FrameHeight);
  	FrameWidthi  = frameW.intValue();
  	FrameHeighti  = frameH.intValue();*/
  	FRAMEX   = this.getSize().width;
	FRAMEY   = this.getSize().height;
	MX       = FRAMEX/2;
    MY       = FRAMEY/2;
    STARS    = 800;
	DELAY    = 50;
	MAXDEPTH = 300;
	STARSIZE = 1;

  	    if ((p_text = getParameter("p_text")) == null)
         p_text = "Default text";
    setBackground(Color.black);
    starfield = new int[STARS][4];
    for(int i=0;i<STARS;i++){
	starfield[i][0] = (int)( Math.random()*FRAMEX ); 		// x
	starfield[i][1] = (int)( Math.random()*FRAMEY );		// y
	starfield[i][2] = (int)( Math.random()*MAXDEPTH );		// z
	starfield[i][3] = (int)( Math.pow( Math.abs(starfield[i][0]-MX), 0.1 ) + 1 );	// speed (empiric formula)
    }
  }





  public void start(){
	if(th == null){
	  th = new Thread(this);
	  th.start();
	}
  }

  public void run(){
	while(th != null){
	  try{
	    Thread.sleep(DELAY);
	  }
	  catch(Exception e){
	  }
	  repaint();
	}
  }

  public void stop(){
  	th = null;
  }
  public void destroy(){
  	th = null;
  }

  public void paint(Graphics g){
    	for(int i=0;i<STARS;i++){
	  int x = starfield[i][0];
	  int y = starfield[i][1];
	  int z = starfield[i][2];
	  // shade color
	  Color c = Color.lightGray;
	  if(z>=MAXDEPTH/2) 	  c = Color.darkGray;
	  else if(z>=MAXDEPTH/3)  c = Color.gray;
	  else if(z>=MAXDEPTH/10) c = Color.lightGray;
	  else if(z<0)  	  c = Color.gray;
	  // paint
	  g.setColor(c);
	  if(z<0){
	    g.fillRect( x, y, STARSIZE, STARSIZE );
	  }
	  else{
	    if(z==0) z++;
	    int x1 = (int)( MX + ((x-MX)*100) / z );
	    int y1 = (int)( MY + ((y-MY)*100) / z );
	    int x2 = (int)( MX + ((x-MX)*100) / (z+7) );
	    int y2 = (int)( MY + ((y-MY)*100) / (z+7) );
  	    g.drawLine( x1, y1, x2, y2 );
   		// Debug: Display the parameters...
      	/*
      	g.drawString("FrameWidth  = " + String.valueOf(p_FrameWidth),10,10);
      	g.drawString("FrameHeight = " + String.valueOf(p_FrameHeight),10,40);
      	g.drawString("FRAMEX   = " + String.valueOf(FRAMEX),230,10);
      	g.drawString("FRAMEY  = " + String.valueOf(FRAMEY),230,40);
      	g.drawString("text  = " + p_text,10,80);
		*/
	}
	  // move star
	  starfield[i][2] -= starfield[i][3]<<1;
	  if(starfield[i][2] < -400){
	    starfield[i][2] = (int)( Math.random()*MAXDEPTH );  // reset star
	  }
    	}
  }

  public void update(Graphics g){
	if (dbImage == null){
  	  dbImage=createImage(this.getSize().width,this.getSize().height);
  	}
	Graphics dbGraphics = dbImage.getGraphics();
	dbGraphics.setColor(getBackground());
	dbGraphics.fillRect(0,0,this.getSize().width,this.getSize().height);
	dbGraphics.setColor(getForeground());
	paint(dbGraphics);
	g.drawImage(dbImage,0,0,this);
  }


  private int FRAMEX   = 10,
		 FRAMEY   = 10,
		 MX       = FRAMEX/2,
		 MY       = FRAMEY/2,
		 STARS    = 800,
		 DELAY    = 50,
		 MAXDEPTH = 900,
		 STARSIZE = 1;

  private int[][] starfield;
  private Thread th;
  private Image    dbImage;
}