/**octet.java by gregvan 
 * RECOMMENDED SIZE: height 400, width 600
 **/

import java.applet.*;
import java.awt.*;

public class octet4 extends Applet implements Runnable
{
    
int rr,gg,bb;//color variables       
int rq=1; // color up-down counters
int gq=1;
int bq=1;
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 w=1;        
        Thread    myRunner;
Color cNew = new Color(125, 0, 255);
Color dNew = new Color(0, 0, 0);

        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();
        }

        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() *300);
               y=(int)(Math.random() *200);
               z=(int)(Math.random() *400);

               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=w+1;
if (w>200){w=1;}
             //for loop to paint 5 images before sleeping .1 second     
        for (int i=0;i<5;i++)
          {
             //color changer up-down counters
           rr=rr+rq;
           if(rr>250){rq=-1;}
           if(rr<5){rq=1;}

           gg=gg+gq;
           if(gg>250){gq=-2;}
           if(gg<5){gq=2;}

           bb=bb+bq;
           if(bb>250){bq=-3;}
           if(bb<5){bq=3;}

           //move the endpoints of the lines
           //up-down counters     

           z=z+zz;
           if(z>400){zz=-1;}
           if(z<-400){zz=4;}

           a=a+aa;
           if(a>300){aa=-2;}
           if(a<-300){aa=1;}

           x=x+xx;
           if(x>350){xx=-3;}
           if(x<-350){xx=1;}

           y=y+yy;
           if(y>250){yy=-1;}   
           if(y<-250){yy=5;} 

        for (int i2=-w;i2<w+1;i2=i2+w)
        {
	x=x+i2+i2;
	y=y-i2;
	a=a-i2;
	z=z+i2;
           //apply the new numbers for color
        Color cNew = new Color(rr, gg, bb);
        bufferGraphics.setColor(cNew);  
          //draw the lines set 1
        bufferGraphics.drawLine(300+ x,200+ y,300+ z,200+a);
        bufferGraphics.drawLine(300- x,200- y,300- z,200-a);
     
        bufferGraphics.drawLine(300+ x,200- y,300+ z,200-a);
        bufferGraphics.drawLine(300- x,200+y,300- z,200+a); 
	   //draw the lines set 2
        bufferGraphics.drawLine(300+ y,200+ x,300+ a,200+z);
        bufferGraphics.drawLine(300- y,200- x,300- a,200-z);
     
        bufferGraphics.drawLine(300+ y,200- x,300+ a,200-z);
        bufferGraphics.drawLine(300- y,200+x,300- a,200+z); 
	

      
          //black and white lines     
     Color dNew = new Color(bb, bb, bb);
        bufferGraphics.setColor(dNew);  
         //draw the black and white lines in a slightly
         //different location...
     x=x+yy;
     y=y+aa;
     z=z-xx;
     a=a-zz;
     
          //draw the lines set 1
        bufferGraphics.drawLine(300+ x,200+ y,300+ z,200+a);
        bufferGraphics.drawLine(300- x,200- y,300- z,200-a);
     
        bufferGraphics.drawLine(300+ x,200- y,300+ z,200-a);
        bufferGraphics.drawLine(300- x,200+y,300- z,200+a); 
	   //draw the lines set 2
        bufferGraphics.drawLine(300+ y,200+ x,300+ a,200+z);
        bufferGraphics.drawLine(300- y,200- x,300- a,200-z);
     
        bufferGraphics.drawLine(300+ y,200- x,300+ a,200-z);
        bufferGraphics.drawLine(300- y,200+x,300- a,200+z); 
       
     x=x-yy; //move ends back to where the were before 
     y=y-aa;
     z=z+xx;
     a=a+zz;  
     

        x=x-i2-i2; // do the opposite to restore...
	y=y+i2;
	a=a+i2;
	z=z-i2;
     } //i2 loop end
   
    }//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...
