/**tma2001(letter).java by gregvan 
 * RECOMMENDED SIZE: height 400, width 600
 **/

import java.applet.*;
import java.awt.*;

public class tma2001z extends Applet implements Runnable
{
int xx,yy;


int t;
int ts=1;
int tc;
int tcs=2;
int tb;
int ftl2,ftl3;
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 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);


wq=(int)(Math.random() *15);
wwq=(int)(Math.random() *4);

               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)
        {

  wq=wq+1;
if(wq>15){wq=1;}
wwq=wwq+1;
if(wwq>4){wwq=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>240){rq=-wq;}
           if(rr<16){rq=wwq;}
	bw=bw+bwq;
           if(bw>239){bwq=-wwq;}
           if(bw<17){bwq=wq;}
         gg=gg+gq;
           if(gg>238){gq=-wwq;}
           if(gg<18){gq=wq;}

           bb=bb+bq;
           if(bb>237){bq=-wq;}
           if(bb<19){bq=wwq;}



t=t+ts;
if(t>200)
{ts=ts+1;
t=0;
if(ts>142){ts=1;}
}

tc=tc+tcs;
if(tc>200)
{tcs=tcs+1;
tc=0;
if(tcs>157){tcs=1;}
}

tb=tb+1;
if(tb>200){tb=-25;}



ftl2=ftl2+1;
if(ftl2>250){ftl2=-250;}

ftl3=ftl3+1;
if(ftl3>350){ftl3=-350;}



xx=xx+42+wq;
if (xx>300){xx=xx-600;}
yy=yy+57-wwq;
if(yy>200){yy=yy-400;}




if(ftl2>-50)
{

//apply the new numbers for b&w rectangles
        Color bwNew = new Color(bw, bw, bw);
        bufferGraphics.setColor(bwNew); 
 for(int ii=0;ii<31;ii=ii+10)
{
t=t+ii;
//draw lines set1 top and bottom
bufferGraphics.drawLine(cx-100-t-t,cy-25-t,cx+100+t+t,cy-25-t);
bufferGraphics.drawLine(cx-100-t-t,cy+25+t,cx+100+t+t,cy+25+t);
//sides
bufferGraphics.drawLine(cx-100-t-t,cy-25-t,cx-100-t-t,cy+25+t);
bufferGraphics.drawLine(cx+100+t+t,cy-25-t,cx+100+t+t,cy+25+t);
t=t-ii;
}  
}  
if (ftl3>-50)
{
//apply the new numbers for colored rectangles
        Color ccNew = new Color(gg, rr, bw);
        bufferGraphics.setColor(ccNew); 
 for(int iii=0;iii<46;iii=iii+15)
{
tc=tc+iii;
//draw lines set2 top and bottom
bufferGraphics.drawLine(cx-100-tc-tc,cy-25-tc,cx+100+tc+tc,cy-25-tc);
bufferGraphics.drawLine(cx-100-tc-tc,cy+25+tc,cx+100+tc+tc,cy+25+tc);
//sides
bufferGraphics.drawLine(cx-100-tc-tc,cy-25-tc,cx-100-tc-tc,cy+25+tc);
bufferGraphics.drawLine(cx+100+tc+tc,cy-25-tc,cx+100+tc+tc,cy+25+tc);
tc=tc-iii;
}    
}//ftl3
//apply the new numbers for totally black rectangles
        Color bNew = new Color(0, 0, 0);
        bufferGraphics.setColor(bNew); 
//draw lines set2 top and bottom
bufferGraphics.drawLine(cx-100-tb-tb,cy-25-tb,cx+100+tb+tb,cy-25-tb);
bufferGraphics.drawLine(cx-100-tb-tb,cy+25+tb,cx+100+tb+tb,cy+25+tb);
//sides
bufferGraphics.drawLine(cx-100-tb-tb,cy-25-tb,cx-100-tb-tb,cy+25+tb);
bufferGraphics.drawLine(cx+100+tb+tb,cy-25-tb,cx+100+tb+tb,cy+25+tb);
//sides
bufferGraphics.drawLine(cx-101-tb-tb,cy-25-tb,cx-101-tb-tb,cy+25+tb);
bufferGraphics.drawLine(cx+101+tb+tb,cy-25-tb,cx+101+tb+tb,cy+25+tb);

//stars
//apply the new numbers for stars
        Color wNew = new Color(255, 255, 255);
        bufferGraphics.setColor(wNew); 


bufferGraphics.drawLine(cx-xx,cy-yy,cx-xx,cy-yy);
bufferGraphics.drawLine(cx-xx,cy+yy,cx-xx,cy+yy);

bufferGraphics.drawLine(cx+xx,cy-yy,cx+xx,cy-yy);
bufferGraphics.drawLine(cx+xx,cy+yy,cx+xx,cy+yy);


bufferGraphics.drawLine(cx-yy,cy-xx,cx-yy,cy-xx);
bufferGraphics.drawLine(cx-yy,cy+xx,cx-yy,cy+xx);

bufferGraphics.drawLine(cx+yy,cy-xx,cx+yy,cy-xx);
bufferGraphics.drawLine(cx+yy,cy+xx,cx+yy,cy+xx);

   }//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...

