/* RESIZE.java - Produces resizeable turtle.
      Created by Randel McGirr
       Searles Valley, CA
       2011 
*/
                                  
import java.awt.*;
import java.applet.*;
import java.util.*;

public class RESIZE{

  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 x, y, scale, cnt, target, grow, xdir, ydir, xedge, yedge;

  RESIZE(int xb, int yb){
    r = new Random();
    target = Math.abs(r.nextInt()%10)+10;
    scale=Math.abs(r.nextInt()%4)+1;
    xedge=xb;
    yedge=yb;
    xb-=100; yb-=100;
    x=Math.abs(r.nextInt()%xb)+40;
    y=Math.abs(r.nextInt()%yb)+40;
    xdir=Math.abs(r.nextInt()%2);
    ydir=Math.abs(r.nextInt()%2);
    grow=Math.abs(r.nextInt()%2);
  }

  public void move(){
    int xC=Math.abs(r.nextInt()%10)+5;
    int yC=Math.abs(r.nextInt()%10)+5;
    if(xdir==0){
      x+=xC;
      if(x>xedge-scale*40) xdir=1;
    }
    else{
      x-=xC;
      if(x<-scale*15) xdir=0;
    }
    if(ydir==0){  
      y+=yC;
      if(y>yedge-scale*30) ydir=1;
    }
    else{
      y-=yC;
      if(y<-scale*20) ydir=0;
    }
    if(cnt>target){
      if(grow==0){
        scale++;
        x-=10; y-=10;
        if(scale>4) grow=1;
      }
      else{
        scale--;
        x+=10; y+=10;
        if(scale<1) grow=0;
      }  
      cnt=0;
      target=Math.abs(r.nextInt()%10)+10;
    }
    cnt++;
  }

  public void draw(Graphics g){
    int endPT=6;
    int start=x+8*scale; // x replaces body
    for(int yy=0; yy<4; yy++){ // 4 layers
      for(int xx=0; xx<endPT; xx++){  // each layer smaller than prev
        int pick = Math.abs(r.nextInt()%colors.length);
        g.setColor(colors[pick]);
        g.fillRect(start+xx*scale*4,y+scale*24-yy*scale*4,scale*4-2,scale*4-2);
      }
      start+=2*scale;
      endPT--;
    }
     //tail
    int c[] = {x+8*scale,x+11*scale,x+8*scale,x+4*scale,x+6*scale,x+8*scale }; // x replaces body
    int d[] = {y+scale*28,y+scale*28,y+scale*29,y+scale*30,y+scale*28,y+scale*28 };
    Polygon tail = new Polygon(c,d,c.length);
    g.fillPolygon(tail);
    //back leg
    int bleg=x+scale*2;
    int bx[]={bleg+13*scale,bleg+15*scale,bleg+17*scale,bleg+11*scale,bleg+13*scale};
    int by[]={y+scale*28,y+scale*28,y+scale*32,y+scale*32,y+scale*28};
    Polygon backleg=new Polygon(bx,by,bx.length);
    g.fillPolygon(backleg);    
    //front leg
    int fleg=bleg+scale*9;
    int fx[]={fleg+13*scale,fleg+15*scale,fleg+17*scale,fleg+11*scale,fleg+13*scale};
    int fy[]={y+scale*28,y+scale*28,y+scale*32,y+scale*32,y+scale*28};
    Polygon frontleg=new Polygon(fx,fy,fx.length);
    g.fillPolygon(frontleg);    
    //head
    int hx[]={x+28*scale,x+33*scale,x+35*scale,x+38*scale,x+40*scale,x+38*scale,x+35*scale,x+33*scale,x+30*scale,x+28*scale};
    int hy[]={y+scale*28,y+scale*28,y+scale*24,y+scale*24,y+scale*28,y+scale*31,y+scale*31,y+scale*30,y+scale*30,y+scale*28};
    Polygon head=new Polygon(hx,hy,hx.length);
    g.fillPolygon(head);    
    g.setColor(Color.black);
    g.fillOval(x+scale*37,y+scale*26,scale+1,scale+1);
    g.drawLine(x+scale*39,y+scale*30,x+scale*37,y+scale*29);
  }
}

//<applet code=RESIZE.class height=200 width=380></applet>
