import java.awt.*;
import java.applet.*;
import java.util.*;

public class TURTLE03 extends Applet implements Runnable{

  Dimension d;
  Image offI;
  SHELL shells[] = new SHELL[18];
  int cycle, inc, curr, cnt, ccc;
  int body, fleg, bleg;
  Thread tt;

  public void init(){
    d = getSize();
//    r = new Random();
    reset();
    setShells(); 
    offI = createImage(d.width, d.height);
    tt = new Thread(this);
    tt.start();
  }
  
  public void reset(){
    inc=0;
    fleg=0;
    bleg=0;
    body=0;
    cnt=0;
    curr=0;
    ccc=0;
  }
  
  public void setShells(){
    for(int i=0; i<shells.length; i++){
      if(i<3) shells[i]=new SHELL(70+(i*20), 210);
      else if(i<7) shells[i]=new SHELL(60+((i-3))*20, 230);
      else if(i<12) shells[i]=new SHELL(50+((i-7))*20, 250);
      else shells[i]=new SHELL(40+((i-12))*20, 270);
    }
  } 
  
  public void incShells(){
    for(int i=0; i<shells.length; i++) shells[i].move();
    body+=12;
  }

  public void run(){
     while(true){
       cycle++;
       cycle%=2;
       inc++;
       inc%=3;
       if(inc%3==0) fleg+=12;
       else if(inc%3==1) bleg+=12;
       else incShells();
       cnt++;
       if(cnt>10 && cnt%4==0) curr++;
       if(cnt>10 && ccc<255) ccc+=3;
       if(body>500){
         reset();
         for(int i=0; i<shells.length; i++){
           shells[i].reset();
         }
       }
       repaint(); 
       try{ 
         Thread.sleep(150); 
       }catch(InterruptedException ie){ } 
     } 
  }

  public void update(Graphics g){
    paint(g);
  }

  public void paint(Graphics g){
    Graphics og = offI.getGraphics();
    og.setColor(new Color(ccc,ccc,0));
    og.fillRect(0,0,d.width,d.height);
    og.setFont(new Font("Courier",Font.BOLD,60));
    og.setColor(new Color(255-ccc,255-ccc,0));
    og.drawString("blackturtle.us",80,360);
    for(int i=0; i<shells.length; i++){
      int col=1;  // tell shell to pick random color
      if(i<curr) col=0; // black
      if(cycle==0) shells[i].draw(og, 0, col);
      else shells[i].draw(og, 1, col);
    }
     //tail
    int c[] = {body+42,body+55,body+40,body+22,body+32,body+42 };
    int d[] = {291,291,296,300,296,291 };
    Polygon tail = new Polygon(c,d,c.length);
    og.fillPolygon(tail);
    //back leg
    int bx[]={bleg+67,bleg+77,bleg+87,bleg+57,bleg+67};
    int by[]={291,291,310,310,291};
    Polygon backleg=new Polygon(bx,by,bx.length);
    og.fillPolygon(backleg);    
    //front leg
    int fx[]={fleg+117,fleg+127,fleg+137,fleg+107,fleg+117};
    int fy[]={291,291,310,310,291};
    Polygon frontleg=new Polygon(fx,fy,fx.length);
    og.fillPolygon(frontleg);    
    //head
    int hx[]={body+140,body+165,body+175,body+190,body+200,body+190,body+175,body+165,body+150,body+140};
    int hy[]={291,291,270,270,291,305,305,300,300,291};
    Polygon head=new Polygon(hx,hy,hx.length);
    og.fillPolygon(head);    
    og.setColor(new Color(ccc, ccc, 0));
    og.fillOval(body+186,280,5,5);
    og.drawLine(body+195,300,body+185,295);
    g.drawImage(offI, 0, 0, this);
    og.dispose();
  }
}
//<applet code=TURTLE03.class height=520 width=648></applet>
