/* JAG.java - Produces black turtle with changing shell shape on a
    multi-color background with a jagged wave pattern scrolling across screen.
    Created by Randel McGirr
    Trona, CA
    blackturtle.us 2011
*/
import java.awt.*;
import java.awt.geom.*;
import java.applet.*;
import java.util.*;

public class JAG 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;
  int xwave, cnt, pick;
  Thread tt;

  public void init(){
    d = getSize();
    r = new Random();
    fleg=417;
    bleg=360;
    body=330;
    xwave=0;
    cnt=0;
    pick=2;
    offI = createImage(d.width, d.height);
    tt = new Thread(this);
    tt.start();
  }
  
  public void run(){
    int pause=50;
    while(true){
      xwave++;
      cnt++;
      if(xwave>99) xwave=0;
      if(cnt%15==0){
        pick = Math.abs(r.nextInt()%colors.length);
        phase++;
        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.yellow);
    offG.fillRect(0,0,d.width,d.height);
    int yval[]={-90,10,-90,10,-90,10,-90,10,-90,10,-90,10,-90,10,-90,10,-90,10,-90,10,-90,10,-90,10,-90,10,-90,30,30,-90};
    int cval[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    int xval[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  for(int lvl=0; lvl<7; lvl++){
    for(int ct=0; ct<colors.length; ct++){
      for(int inc=0; inc<yval.length; inc++){
//y values
        cval[inc]=yval[inc]+ct*20+lvl*100;
        if(yval[inc]>d.height) yval[inc]=d.height+lvl*100;
//x values
        if(inc==0 || inc==28 || inc==29) xval[inc]=-100+xwave;
        else if(inc==27) xval[27]=xval[26];
        else xval[inc]=xval[inc-1]+50;
      }  
      offG.setColor(colors[ct]);
      Polygon wave = new Polygon(xval,cval,xval.length);
      offG.fillPolygon(wave);
    }
  }
    int endPT=6;
    int start=body+40;
    offG.setColor(Color.black);
    for(int yy=0; yy<4; yy++){
      for(int xx=0; xx<endPT; xx++){
        if(phase%2==0) offG.fillRect(start+xx*40,250-yy*40,38,38);
        else offG.fillOval(start+xx*40,250-yy*40,36,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(colors[pick]);
    offG.fillOval(body+342,271,9,9);
    offG.drawLine(body+358,308,body+340,299);
    offG.setColor(new Color(100,255,0));
    g.drawImage(offI,0,0,this);
  }
}

//<applet code=JAG.class height=620 width=1200></applet>
