/**butterfly(number).java by gregvan 
 * RECOMMENDED SIZE: height 560, width 760
 **/

import java.applet.*;
import java.awt.*;

public class butterfly44 extends Applet implements Runnable
{
int qqq=1;
int xx,yy;
int xxf=1;
int yyf=1;
int q=2;
int qq=1;
int qfac=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 gv;
int wf=1;
int wwf=1;
int w,ww;
int wwq,wq;
int jomamma=10;
int jomamma1=8;
int tf=1;
int ttf=1;
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
	 

//random starting colors
rr=(int)(Math.random() *255);
gg=(int)(Math.random() *255);
bb=(int)(Math.random() *255);
bw=(int)(Math.random() *255);
xx=bw-128;
yy=rr-128;

w=(int)(Math.random() *15);
ww=(int)(Math.random() *13);

               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)
        {

jomamma=jomamma+1;
if (jomamma>12){jomamma=1;}

jomamma1=jomamma1+1;
if (jomamma1>8){jomamma1=1;}

w=w+wf;
if (w>3+jomamma){wf=-1;}
if (w<2){wf=1;}

ww=ww+wwf;
if(ww>3+jomamma1){wwf=-1;}
if(ww<2){wwf=1;}
             //for loop to paint many images before sleeping .1 second     
       for (int i=0;i<6;i++)
        {

 
             //color changer up-down counters
           rr=rr+rq;
           if(rr>253){rq=-w;}
           if(rr<13){rq=1;}
	bw=bw+bwq;
           if(bw>254){bwq=-1;}
           if(bw<2){bwq=1;}
         gg=gg+gq;
           if(gg>250){gq=-ww;}
           if(gg<15){gq=3;}

           bb=bb+bq;
           if(bb>251){bq=-w;}
           if(bb<14){bq=1;}
  wq=wq+tf;
if(wq>w+2){tf=-1;}
if(wq<-w){tf=1;}


wwq=wwq+ttf;
if(wwq>ww+2){ttf=-1;}
if(wwq<-ww){ttf=1;}

xx=xx+(xxf*wq);
if (xx>cx){xxf=-1;}
if (xx<-cx){xxf=1;}
//if (xx>cx){xx=0;}
//if (xx<-cx){xx=0;}

yy=yy+(yyf*wwq);
if(yy>cy){yyf=-1;}
if(yy<-cy){yyf=1;}
//if(yy>cy){yy=0;}
//if(yy<-cy){yy=0;}

qfac=qfac+qqq;
if(qfac>7){qqq=-1;}
if(qfac<2){qqq=1;}

//stars
//apply the new numbers for stars
        Color wNew = new Color(rr, gg, bb);
        bufferGraphics.setColor(wNew); 


q=3;


bufferGraphics.drawLine(cx-xx,cy+yy,cx-xx-q,cy+yy+q);
bufferGraphics.drawLine(cx+xx,cy+yy,cx+xx+q,cy+yy+q);

bufferGraphics.drawLine(cx-xx-xx,cy-yy,cx-xx-xx-q,cy-yy-q);
bufferGraphics.drawLine(cx+xx+xx,cy-yy,cx+xx+xx+q,cy-yy-q);

bufferGraphics.drawLine(cx-xx,cy+200-yy-yy,cx-xx-q,cy+200-yy-yy+q);
bufferGraphics.drawLine(cx+xx,cy+200-yy-yy,cx+xx+q,cy+200-yy-yy+q);

//stars,colored
//apply the new numbers for stars
        Color xNew = new Color(gg,bb,rr);
        bufferGraphics.setColor(xNew); 
xx=xx+qfac;
yy=yy+qfac;


bufferGraphics.drawLine(cx-xx,cy+yy,cx-xx-q,cy+yy+q);
bufferGraphics.drawLine(cx+xx,cy+yy,cx+xx+q,cy+yy+q);

bufferGraphics.drawLine(cx-xx-xx,cy-yy,cx-xx-xx-q,cy-yy-q);
bufferGraphics.drawLine(cx+xx+xx,cy-yy,cx+xx+xx+q,cy-yy-q);

bufferGraphics.drawLine(cx-xx,cy+200-yy-yy,cx-xx-q,cy+200-yy-yy+q);
bufferGraphics.drawLine(cx+xx,cy+200-yy-yy,cx+xx+q,cy+200-yy-yy+q);
xx=xx-qfac;
yy=yy-qfac;

//stars,colored
//apply the new numbers for stars
        Color xzNew = new Color(bb,rr,gg);
        bufferGraphics.setColor(xzNew); 
xx=xx-qfac;
yy=yy-qfac;




bufferGraphics.drawLine(cx-xx,cy+yy,cx-xx-q,cy+yy+q);
bufferGraphics.drawLine(cx+xx,cy+yy,cx+xx+q,cy+yy+q);

bufferGraphics.drawLine(cx-xx-xx,cy-yy,cx-xx-xx-q,cy-yy-q);
bufferGraphics.drawLine(cx+xx+xx,cy-yy,cx+xx+xx+q,cy-yy-q);

bufferGraphics.drawLine(cx-xx,cy+200-yy-yy,cx-xx-q,cy+200-yy-yy+q);
bufferGraphics.drawLine(cx+xx,cy+200-yy-yy,cx+xx+q,cy+200-yy-yy+q);
xx=xx+qfac;
yy=yy+qfac;


//apply the new numbers for black erase
        Color zNew = new Color(0, 0, 0);
        bufferGraphics.setColor(zNew);

gv=gv+1;
if(gv>cx+100){gv=-(cx+100);}

if(gv==100)
{
xx=0;
yy=0;
}


bufferGraphics.drawLine(cx+gv-w,cy+cx,cx+gv+gv+ww,cy-cx);
bufferGraphics.drawLine(cx-gv+w,cy+cx,cx-gv-gv-ww,cy-cx);

bufferGraphics.drawLine(cx+cx,cy+gv+ww,0,cy+gv+gv+gv-w);
bufferGraphics.drawLine(0,cy+gv+ww,cx+cx,cy+gv+gv+gv-w);



   }//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...

