/**art(number).java by gregvan 
 * RECOMMENDED SIZE: height 400, width 400
 **/

import java.applet.*;
import java.awt.*;

public class art0016 extends Applet implements Runnable
{
 int joe=40; 
int joef=1;  
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 x1,z1;
int zz1=1;
int xx1=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+100);
 y=cy+100;
//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)
        {
ww=(int)(Math.random() *6);
w=(int)(Math.random() *3);
wq=(int)(Math.random() *15);
wwq=(int)(Math.random() *16);

  
             //for loop to paint many images before sleeping .1 second     
        for (int i=0;i<4;i++)
          {
ww=ww+1;
if (ww>4){ww=1;}

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=-ww;}
           if(z<-400){zz=w;}

   z1=z1+zz1;
           if(z1>301){zz1=-ww;}
           if(z1<-300){zz1=w;}


		x=x+xx;
           if(x>370){xx=-w;}
           if(x<-371){xx=ww;}

x1=x1+xx1;
           if(x1>320){xx1=-w;}
           if(x1<-321){xx1=ww;}

             //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;}
joe=joe+joef;
if(joe>280){joef=-1;}
if(joe<10){joef=1;}

  for (int ii=-joe;ii<=joe;ii=ii+joe+joe)
{

z=z+ii;
z1=z1-ii;
x=x+ii;
x1=x1-ii;
if(ii<0)
{
           //apply the new numbers for color
        Color cNew = new Color(rr, gg, bb);
        bufferGraphics.setColor(cNew); 
}
if(ii>-1)
{
 //apply the new numbers for color
        Color claNew = new Color(255-gg,255- rr,255- bb);
        bufferGraphics.setColor(claNew); 
}
 
	  //draw the lines set 1
        bufferGraphics.drawLine(cx+ x1,cy+ y,cx+ z1,cy+a);
      bufferGraphics.drawLine(cx- x1,cy- y,cx- z1,cy-a);
 //draw the lines set 2
        bufferGraphics.drawLine(cy- y,cx+ x1,cy- a,cx+z1);
      bufferGraphics.drawLine(cy+ y,cx- x1,cy+ a,cx-z1);

          //draw the lines set 3
        bufferGraphics.drawLine(cx- x1,cy+ y,cx- z1,cy+a);
      bufferGraphics.drawLine(cx+ x1,cy- y,cx+ z1,cy-a);
 //draw the lines set 4
        bufferGraphics.drawLine(cy+ y,cx+ x1,cy+ a,cx+z1);
      bufferGraphics.drawLine(cy- y,cx- x1,cy- a,cx-z1);


if(ii<0)
{
         //apply the new numbers for black and white
        Color ccNew = new Color(rr, rr, rr);
        bufferGraphics.setColor(ccNew);  
       
}
if(ii>-1)
{
         //apply the new numbers for black and white
        Color cclaNew = new Color(bb, bb, bb);
        bufferGraphics.setColor(cclaNew);  
       
}


          //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+1;
y=y+3;
z=z+2;
a=a-4;  
if(ii>0)
{

           //apply the new numbers for color
        Color cccNew = new Color(gg, bw, rr);
        bufferGraphics.setColor(cccNew);
}
if(ii<1)
{

           //apply the new numbers for color
        Color ccclaNew = new Color(gg, rr, rr);
        bufferGraphics.setColor(ccclaNew);
}


  //draw the lines set 1
        bufferGraphics.drawLine(cx+ x1,cy+ y,cx+ z1,cy+a);
      bufferGraphics.drawLine(cx- x1,cy- y,cx- z1,cy-a);
 //draw the lines set 2
        bufferGraphics.drawLine(cy- y,cx+ x1,cy- a,cx+z1);
      bufferGraphics.drawLine(cy+ y,cx- x1,cy+ a,cx-z1);

          //draw the lines set 3
        bufferGraphics.drawLine(cx- x1,cy+ y,cx- z1,cy+a);
      bufferGraphics.drawLine(cx+ x1,cy- y,cx+ z1,cy-a);
 //draw the lines set 4
        bufferGraphics.drawLine(cy+ y,cx+ x1,cy+ a,cx+z1);
      bufferGraphics.drawLine(cy- y,cx- x1,cy- a,cx-z1);  
	


          //black lines     
     Color dlabNew = new Color(0, 0, 0);
     bufferGraphics.setColor(dlabNew);    



         //draw the black  lines in a slightly
         //different location...

 



   
          //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-1;
y=y-3;
z=z-2;
a=a+4;  

z=z-ii;
z1=z1+ii;
x=x-ii;
x1=x1+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...

