/**art8(number).java by gregvan 
 * RECOMMENDED SIZE: height 400, width 400
 **/

import java.applet.*;
import java.awt.*;

public class art80 extends Applet implements Runnable
{
    
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;
a=-(cy+10);
 y=cy+10;
//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() *3);
wq=(int)(Math.random() *15);
wwq=(int)(Math.random() *16);

  
             //for loop to paint 8 images before sleeping .1 second     
        for (int i=0;i<8;i++)
          {

w=w+1;
if (w>3){w=1;}
wq=wq+1;
if (wq>15){wq=1;}
wwq=wwq+1;
if(wwq>16){wwq=1;}


		 z=z+zz;
           if(z>401){zz=-w;}
           if(z<-400){zz=w;}

  


		x=x+xx;
           if(x>470){xx=-w;}
           if(x<-471){xx=w;}



             //color changer up-down counters
           rr=rr+rq;
           if(rr>239){rq=-wq;}
           if(rr<17){rq=wwq;}
	bw=bw+bwq;
           if(bw>236){bwq=-wwq;}
           if(bw<17){bwq=wq;}
         gg=gg+gq;
           if(gg>237){gq=-wwq;}
           if(gg<17){gq=wq;}

           bb=bb+bq;
           if(bb>238){bq=-wq;}
           if(bb<17){bq=wwq;}

	
if(z>0){
           //apply the new numbers for color
        Color cNew = new Color(rr, gg, bb);
        bufferGraphics.setColor(cNew);  
	}
if(z<1){
         //apply the new numbers for black and white
        Color ccNew = new Color(rr, rr, rr);
        bufferGraphics.setColor(ccNew);  
       }




          //draw the lines set 1
        bufferGraphics.drawLine(cx+ x,cy+ y,cx+ z,cy+a);
      bufferGraphics.drawLine(cx- x,cy- y,cx- z,cy-a);
 //draw the lines set 2
        bufferGraphics.drawLine(cy- y,cx+ x,cy- a,cx+z);
      bufferGraphics.drawLine(cy+ y,cx- x,cy+ a,cx-z);

          //draw the lines set 3
        bufferGraphics.drawLine(cx- x,cy+ y,cx- z,cy+a);
      bufferGraphics.drawLine(cx+ x,cy- y,cx+ z,cy-a);
 //draw the lines set 4
        bufferGraphics.drawLine(cy+ y,cx+ x,cy+ a,cx+z);
      bufferGraphics.drawLine(cy- y,cx- x,cy- a,cx-z);


if(x>0){
           //apply the new numbers for color
        Color cccNew = new Color(gg, bw, rr);
        bufferGraphics.setColor(cccNew);  
	}

if(x<1){

          //black and white lines     
     Color dNew = new Color(bw, bw, bw);
     bufferGraphics.setColor(dNew);    
}


         //draw the black and white lines in a slightly
         //different location...

 x=x+3;
y=y+5;
z=z+2;
a=a-4;   



   
          //draw the lines set 1
        bufferGraphics.drawLine(cx+ x,cy+ y,cx+ z,cy+a);
      bufferGraphics.drawLine(cx- x,cy- y,cx- z,cy-a);
 //draw the lines set 2
        bufferGraphics.drawLine(cy- y,cx+ x,cy- a,cx+z);
      bufferGraphics.drawLine(cy+ y,cx- x,cy+ a,cx-z);

 //draw the lines set 3
        bufferGraphics.drawLine(cx- x,cy+ y,cx- z,cy+a);
      bufferGraphics.drawLine(cx+ x,cy- y,cx+ z,cy-a);
 //draw the lines set 4
        bufferGraphics.drawLine(cy+ y,cx+ x,cy+ a,cx+z);
      bufferGraphics.drawLine(cy- y,cx- x,cy- a,cx-z);


 x=x-3;
y=y-5;
z=z-2;
a=a+4;   

  


       
    }//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...

