Fern


//--------------------------- Fern Source -------------------------//
import java.awt.*;
import java.applet.*;
import java.io.*;
import java.lang.String;
import java.util.Random;
import java.util.Date;

public class Fern extends Applet implements Runnable 
{
     Thread    runner;
	 int       WIDTH,HEIGHT;
     Image     offscreen;
     Graphics  g,off_g;
     Random    nrand;
     boolean   START;
     double    EE[],FF[],xx,yy,zz,RITU,dx,dy,myxx,myyy;
   	
	public void init() 
	{
	 	Date d=new Date();
		nrand=new Random(d.getTime());
	 	 
		WIDTH=this.size().width;
		HEIGHT=this.size().height;
		
        offscreen = this.createImage(WIDTH, HEIGHT);
        off_g = offscreen.getGraphics();
        off_g.setColor(Color.black);
        off_g.fillRect(0,0,WIDTH,HEIGHT);
		g=this.getGraphics();
		
		setBackground(Color.black);
		setForeground(Color.white);
		
	   	//-------------------------------------//
	    START=false;
	   
	    EE=new double[4];
	    FF=new double[4];
	    
	    EE[0]=(double)0.5*(WIDTH-50);
        EE[1]=(double)0.57*(WIDTH-50);
        EE[2]=(double)0.408*(WIDTH-50);
        EE[3]=(double)0.1075*(WIDTH-50);
        FF[0]=0.0;
        FF[1]=-(double)0.036*HEIGHT;
        FF[2]=(double)0.0893*HEIGHT;
        FF[3]=(double)0.27*HEIGHT;
	     
	    RITU=1.0;
	      
        domyInit();
	}
	
	public void start()
	{
		if (runner == null)
		{
			runner= new Thread(this);
			runner.start();
		}
	}
	
	public void stop()
	{
		if (runner!=null)
		{
			runner.stop();
			runner=null;
		}
	}
	
   
	public void run() //----------------------------------------//
	{
 
		 init(); 
		
		while (true) {
			try { Thread.sleep(10); } catch (InterruptedException e){};
			if ( isVisible()  ) {
		 	
		 	   if(START)   DomyRun();
	  	     	 
			}
		}
		  
	}
	
	public void paint(Graphics G)
	{
		 G.drawImage(offscreen, 0, 0,Color.black,this);
	}
	
	public boolean mouseDown(Event e,int x,int y)
	{
        
	  if(x>WIDTH-45 && x10 && y<35)  START=true;
	  if(x>WIDTH-45 && x45 && y<70)  START=false;
	  if(x>WIDTH-45 && x80 && y<105) { START=false;DoReset();}
	   
	   return true;
	} 
  //-------------------------------------------------//
  
   private void DomyRun()
   {
    int ss,sx,sy,s0,s1;
    
         sx=WIDTH-50;
         sy=HEIGHT;
    
          ss=randomise(1000);
          
          if(ss<20){
              dx=EE[0];
              dy=0.27*yy+FF[0];
          }else if(ss<170){
              dx=-0.139*xx + 0.263*yy + EE[1];
              dy= 0.246*xx + 0.224*yy + FF[1];
          }else if(ss<300){
              dx=  0.17*xx - 0.215*yy + EE[2];
              dy= 0.222*xx + 0.176*yy + FF[2];
          } else {
              dx= 0.781*xx + 0.034*yy + EE[3];
              dy=-0.032*xx + 0.739*yy + FF[3];
          }
           
          xx=dx; yy=dy;
          
          myxx=(double)(dx-sx/2)*RITU +sx/2;
          myyy=(double)(dy-sy/2)*RITU +sy/2;
           
          s0=(int)myxx; s1=(int)myyy; s1= sy - s1;
 
          g.setColor(Color.white);
          g.drawLine(s0,s1,s0,s1);
          
          off_g.setColor(Color.white);
          off_g.drawLine(s0,s1,s0,s1);
        
   
   }
   
   private void domyInit()
   {
    int s0;
    double d0;
    
      
         off_g.setColor(Color.black);
         off_g.fillRect(0,0,WIDTH,HEIGHT);
   
         off_g.setColor(Color.white);
         off_g.drawRect(WIDTH-45,10,40,25);
         off_g.drawString("Start",WIDTH-40,30);
         
         off_g.drawRect(WIDTH-45,45,40,25);
         off_g.drawString("Stop",WIDTH-40,65);
         
         off_g.drawRect(WIDTH-45,80,40,25);
         off_g.drawString("Reset",WIDTH-40,100);
          
        s0=randomise(200);
        d0=(double)s0/200.0;
        xx=(double)(WIDTH-50)*d0;
        
        s0=randomise(200);
        d0=(double)s0/200.0;
        yy=(double)HEIGHT*d0;
   
   }
  
  
  private void DoReset()
  {
       domyInit();
  
       paint(g);
  }
  
    private int randomise(int range)
	 {
	   if(range==0) return 0;
		return(Math.abs(nrand.nextInt()) % range);
	 }

}