/*********************************************************************************************/
/*                                                                                           */
/*  Java Image Demo - Animated Spiral Pattern                                                */
/*  Auth: bkenwright@xbdev.net                                                               */
/*  URL: www.xbdev.net                                                                       */
/*                                                                                           */
/*********************************************************************************************/

import java.awt.image.*;
import java.awt.*;
import java.applet.*;

public class spiral extends Applet implements Runnable
{
	private Thread m_thread        = null;
	// Screen width and height

	int w, h;

  int NN = 30; // how many points are in a full rotation
  int WW = 5;  // number of circles (or windings)


	public synchronized void update(Graphics g)
	{
	    w=getSize().width;    h=getSize().height;
			drawSpiral(g);
	}// End update(..)


  int s = 1;
  double inc=0;
	void drawSpiral(Graphics g)
	{
	   //s+=1;
	   inc+= 0.1;
	   g.setColor(Color.white);
	   g.fillRect(0,0,w,h);
		 g.setColor(Color.blue);
	   
	   int x1 = 0, y1 = 0, x2, y2;
     for ( int i = s; i <= WW*NN; ++i ) 
		 {
         double angle = 2*Math.PI*i/(double)NN;
         double radius = i/(double)NN * w/2 / (WW+1);
         x2 = (int)( radius*Math.cos(angle+inc) );
         y2 = -(int)( radius*Math.sin(angle+inc) );
         g.drawLine( w/2+x1, h/2+y1, w/2+x2, h/2+y2 );
         x1 = x2;
         y1 = y2;
      }// End for loop

	}// End drawSpiral(..)

	
  
  public void run()
	{
			// Remember the starting time
			long thenTime = System.currentTimeMillis();
			while (true) 
			{
			    long nowTime = System.currentTimeMillis();
			    long difTime = nowTime - thenTime;
			    
			    // I've set the delay to 0, so its pushing for the MAX fps, you would
			    // set a value in there to limit for example to 25-50 fps refresh rate
			    if( difTime > 25 )
			    {
			       thenTime = nowTime;
			       // This method provides better updates, instead of filling the
			       // message queue up
			       Graphics g=this.getGraphics();
			       update(g);
			    }
			}// end while loop
	}// End of run() method
	
	
	public void start()
	{
		if(m_thread == null)
    {
			m_thread = new Thread(this);
			m_thread.setPriority(Thread.MAX_PRIORITY);
			m_thread.start();
		}
		System.out.println( "Starting Spiral Graphics Demo\n" );
	}// End of start()
	
}// End of Applet



