/**pa.java by gregvan now resizeable!
 * RECOMMENDED SIZE: height 400, width 600
*or larger/smaller as you wish 
 **/

import java.applet.*;
import java.awt.*;

public class pa3 extends Applet implements Runnable
{
int pd;//how far between lines
int cx,cy; //center of screen x and y
int bw;//black and white variable
int bwq=1;//b&w initial counter value    
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;

      
        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();
	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;
               y=(int)(Math.random() *300);
		y=y-150;
               z=(int)(Math.random() *500);
		z=z-250;
               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)
        {

  gg=gg+gq;
           if(gg>252){gq=-2;}
           if(gg<3){gq=2;}
             //for loop to paint 10 images before sleeping .1 second     
        for (int i=0;i<10;i++)
          {
             //color changer up-down counters
           rr=rr+rq;
           if(rr>253){rq=-1;}
           if(rr<2){rq=1;}

           bb=bb+bq;
           if(bb>250){bq=-3;}
           if(bb<5){bq=3;}
	//black and white changer
	  bw=bw+bwq;
	if(bw>245){bwq=-1;}
	if(bw<15){bwq=2;}
	

           //move the endpoints of the lines
           
pd=pd+1;
if(pd>4){pd=1;}




//up-down counters     
//cx,cy is half the size of the applet
           z=z+zz;
           if(z>cx+100){zz=-1;}
           if(z<-(cx+100)){zz=pd;}

           a=a+aa;
           if(a>cy+100){aa=-1;}
           if(a<-(cy+100)){aa=pd;}

           x=x+xx;
           if(x>cx+50){xx=-pd;}
           if(x<-(cx+50)){xx=1;}

           y=y+yy;
           if(y>cy+50){yy=-pd;}   
           if(y<-(cy+50)){yy=1;} 


           //apply the new numbers for color
        Color cNew = new Color(rr, gg, bb);
        bufferGraphics.setColor(cNew);  
          //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);
     Color ccNew = new Color(gg, rr, bb);
        bufferGraphics.setColor(ccNew);  
        //draw the lines set 2
        bufferGraphics.drawLine(cx+ y,cy- x,cx+ a,cy+z);
      bufferGraphics.drawLine(cx- y,cy- x,cx- a,cy+z);
     




     //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+yy+yy;
     y=y+xx+xx;
     z=z-aa;
     a=a-zz;
   //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); 
 Color ddNew = new Color(gg,gg,gg);
     bufferGraphics.setColor(ddNew);    

 //draw the lines set 2
        bufferGraphics.drawLine(cx+ y,cy- x,cx+ a,cy+z);
      bufferGraphics.drawLine(cx- y,cy- x,cx- a,cy+z);
     x=x-yy-yy; //move ends back to where the were before 
     y=y-xx-xx;
     z=z+aa;
     a=a+zz;  
 


     //another set of colored lines    
     Color xNew = new Color(gg,bb,rr);
     bufferGraphics.setColor(xNew);    
         //draw the lines in a slightly
         //different location...
     x=x+yy;
     y=y+xx;
     z=z-aa-aa;
     a=a-zz-zz;
   //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);
 Color xxNew = new Color(rr,bb,gg);
     bufferGraphics.setColor(xxNew);     
 //draw the lines set 2
        bufferGraphics.drawLine(cx+ y,cy- x,cx+ a,cy+z);
      bufferGraphics.drawLine(cx- y,cy- x,cx- a,cy+z);
     x=x-yy; //move ends back to where the were before 
     y=y-xx;
     z=z+aa+aa;
     a=a+zz+zz;  


       
    }//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...
