/* LARGE.java - Produces a turtle that changes colors
  and which changes shell shape.
  Created by Randel McGirr, 2011
*/
import java.awt.*;
import java.awt.geom.*;
import java.applet.*;
import java.util.*;

public class LARGE extends Applet implements Runnable{

  Dimension d;
  Image offI;
  Color dark = new Color(30,120,30);
  Color light = new Color(255,160,40);
  Color colors[] = { Color.red, Color.yellow, Color.green, Color.cyan,
    new Color(125,125,125), Color.magenta, new Color(99,50,255), Color.orange, Color.pink };
  Random r;
  int body, fleg, bleg, phase;
  Thread tt;

  public void init(){
    d = getSize();
    r = new Random();
    fleg=417;
    bleg=360;
    body=330;
    offI = createImage(d.width, d.height);
    tt = new Thread(this);
    tt.start();
  }
  
  public void run(){
    int pause=333;
    while(true){
      phase+=1;
      phase%=2;
      repaint();
      try{ Thread.sleep(pause);
      }catch(InterruptedException ie){}
    }
  }
  
  public void update(Graphics g){
    paint(g);
  }
  
  public void paint(Graphics g){
    Graphics2D offG = (Graphics2D)offI.getGraphics();
    offG.setColor(Color.black);
    offG.fillRect(0,0,d.width,d.height);
    int endPT=6;
    int start=body+40;
    for(int yy=0; yy<4; yy++){
      for(int xx=0; xx<endPT; xx++){
        int pick = Math.abs(r.nextInt()%colors.length);
        offG.setColor(colors[pick]);
        if(phase%2==0) offG.fillRect(start+xx*40,250-yy*40,38,38);
        else offG.fillOval(start+xx*40,250-yy*40,38,38);
      }
      start+=20;
      endPT--;
    }
     //tail
    int c[] = {body+42,body+68,body+38,body+2,body+22,body+42 };
    int d[] = {291,291,301,308,301,291 };
    Polygon tail = new Polygon(c,d,c.length);
    offG.fillPolygon(tail);
    //back leg
    int bx[]={bleg+68,bleg+88,bleg+108,bleg+48,bleg+68};
    int by[]={291,291,330,330,291};
    Polygon backleg=new Polygon(bx,by,bx.length);
    offG.fillPolygon(backleg);    
    //front leg
    int fx[]={fleg+118,fleg+138,fleg+158,fleg+98,fleg+118};
    int fy[]={291,291,330,330,291};
    Polygon frontleg=new Polygon(fx,fy,fx.length);
    offG.fillPolygon(frontleg);    
    //head
    int hx[]={body+250,body+300,body+320,body+350,body+370,body+350,body+320,body+300,body+270,body+250};
    int hy[]={291,291,248,248,291,319,319,309,309,291};
    Polygon head=new Polygon(hx,hy,hx.length);
    offG.fillPolygon(head);    
    offG.setColor(Color.black);
    offG.fillOval(body+342,271,9,9);
    offG.drawLine(body+360,309,body+340,299);
    offG.setColor(new Color(100,255,0));
    g.drawImage(offI,0,0,this);
  }
}

//<applet code=LARGE.class height=600 width=1200></applet>
