/**zapdot(number).java by gregvan 
 * RECOMMENDED SIZE: height 400, width 600
 **/

import java.applet.*;
import java.awt.*;

public class zapdot11 extends Applet implements Runnable
{
int na;//size of dots
int nap;
   
int rr,gg,bb;//color variables 
int bw;//black and white 
int bwq=1;      
int rq=1; // color up-down factors
int gq=1;
int bq=1;

int w,ww;
int wwq,wq;
int x, y; //endpoint locations
int z;
int a;
int zz=1; //endpoint up-down counters
int xx=2;
int yy=3;
int aa=4;
int cx; //center of screen x
int cy; //center of screen y

        Thread    myRunner;
        Image     buffer;         
        Dimension appletSize;     
        Graphics  bufferGraphics; 

        public void init()
        {    
        setBackground(Color.black);
        appletSize = this.getSize();     
        buffer = this.createImage(appletSize.width, appletSize.height);
        bufferGraphics = buffer.getGraphics();
	cx=appletSize.width/2;
        cy=appletSize.height/2;

        }

        public void start()
        {
             if (myRunner == null)
             {
                 myRunner = new Thread(this);
                 myRunner.start();
             }
        }

        public void run()
        {
            Thread  executingThread;                   
            executingThread = Thread.currentThread();

            while (myRunner == executingThread)
            {
               //new random numbers to start animation
	       x=(int)(Math.random() *400);
		x=x-200;

               z=(int)(Math.random() *500);
		z=z-250;

//random starting colors
rr=(int)(Math.random() *255);
gg=(int)(Math.random() *255);
bb=(int)(Math.random() *255);
bw=(int)(Math.random() *255);
               repaint();
               try
               {Thread.sleep(1000);}
               catch(InterruptedException e) {} 

               while (myRunner != null)
               {
                  repaint(); 
                  try
                  {Thread.sleep(100); }
                  catch (InterruptedException e) {}
               }  
           } 
        }

       // make sure the animation really stops when exiting webpage
        public void stop()
        { 
          if (myRunner != null)
             { myRunner = null; }
        }

        //used to prevent blinking graphics....
        public void update(Graphics g)
        {paint(g);}

        public void paint(Graphics g)
        {
w=(int)(Math.random() *9);
wq=(int)(Math.random() *25);
wwq=(int)(Math.random() *26);

  
             //for loop to paint many images before sleeping .1 second     
        for (int i=0;i<2;i++)
          {
na=na-1;
if (na<5){na=15;}
nap=na+na;
w=w+1;
if (w>9){w=1;}
wq=wq+1;
if (wq>25){wq=1;}
wwq=wwq+1;
if(wwq>26){wwq=1;}



		 z=z+zz;
           if(z>181){zz=-w;}
           if(z<-180){zz=w;}
 

y=y+yy;
           if(y>191){yy=-w;}
           if(y<-190){yy=w;}

  


		x=x+xx;
           if(x>200){xx=-w;}
           if(x<-201){xx=w;}

a=a+aa;
           if(a>210){aa=-w;}
           if(a<-211){aa=w;}

             //color changer up-down counters
           rr=rr+rq;
           if(rr>229){rq=-wq;}
           if(rr<27){rq=wwq;}
	bw=bw+bwq;
           if(bw>226){bwq=-wwq;}
           if(bw<27){bwq=wq;}
         gg=gg+gq;
           if(gg>227){gq=-wwq;}
           if(gg<27){gq=wq;}

           bb=bb+bq;
           if(bb>228){bq=-wq;}
           if(bb<27){bq=wwq;}

	for (int ii=-80;ii<81;ii=ii+20)
          {
x=x+ii;
z=z+ii;
 //apply the new numbers for color
        Color cNew = new Color(rr, gg, bb);
        bufferGraphics.setColor(cNew);  

          
       


x=x+y;
z=z+a;

 

        //draw the dots set 3
        bufferGraphics.fillOval(cx- x-na,cy+ z-na,nap,nap);
      bufferGraphics.fillOval(cx+ x-na,cy- z-na,nap,nap);
 //draw the dots set 4
        bufferGraphics.fillOval(cx+ z-na,cy+ x-na,nap,nap);
      bufferGraphics.fillOval(cx- z-na,cy- x-na,nap,nap);

     //draw the dots set 1
        bufferGraphics.fillOval(cx- x-na,cy- z-na,nap,nap);
      bufferGraphics.fillOval(cx+ x-na,cy+ z-na,nap,nap);
 //draw the dots set 2
        bufferGraphics.fillOval(cx+ z-na,cy- x-na,nap,nap);
      bufferGraphics.fillOval(cx- z-na,cy+ x-na,nap,nap);



//apply the new numbers for color
        Color ccNew = new Color(bw, bw, bw);
        bufferGraphics.setColor(ccNew);  
       
//z=z+nap+nap;
//x=x+nap+nap;
na=na+5;
nap=nap+10;
             //draw the dots set 1
        bufferGraphics.drawOval(cx- x-na,cy- z-na,nap,nap);
      bufferGraphics.drawOval(cx+ x-na,cy+ z-na,nap,nap);
 //draw the dots set 2
        bufferGraphics.drawOval(cx+ z-na,cy- x-na,nap,nap);
      bufferGraphics.drawOval(cx- z-na,cy+ x-na,nap,nap);

 //draw the dots set 3
        bufferGraphics.drawOval(cx- x-na,cy+ z-na,nap,nap);
      bufferGraphics.drawOval(cx+ x-na,cy- z-na,nap,nap);
 //draw the dots set 4
        bufferGraphics.drawOval(cx+ z-na,cy+ x-na,nap,nap);
      bufferGraphics.drawOval(cx- z-na,cy- x-na,nap,nap);
na=na-5;
nap=nap-10;

  
//x=x-nap-nap;
//z=z-nap-nap;
x=x-y;
z=z-a;

x=x-ii;
z=z-ii;
}
       
    }//end of for loop i
     // move the entire buffer onto the display screen and change
     // pixels that are a different color...
      g.drawImage(buffer, 0, 0, this);
    }
} // end of the program...

