/* SINE.java - Produces black turtle with changing shell shapes on a background
   with smooth, multi-colored waves which scroll across screen.
   Created by Randel McGirr 2011
   Trona, CA
*/
import java.awt.*;
import java.awt.geom.*;
import java.applet.*;
import java.util.*;

public class SINE extends Applet implements Runnable{

  Dimension d;
  Image offI;
  Color dark = new Color(30,120,30);
  Color light = new Color(255,160,40);
  Color colors[] = { new Color(255,100,100), new Color(255,255,100), new Color(125,255,0), new Color(174,255,255),
    new Color(175,175,175), new Color(255,100,255), new Color(160,60,255), new Color(255,205,0), new Color(255,200,200)};
  Random r;
  int body, fleg, bleg, phase;
  int offset, cnt, pick;
  int yval[]=new int[360];
  int xval[]=new int[360];
  Thread tt;

  public void init(){
    d = getSize();
    r = new Random();
    fleg=417;
    bleg=360;
    body=330;
    offset=-100;
    cnt=0;
    pick=2;
    offI = createImage(d.width, d.height);
    tt = new Thread(this);
    tt.start();
  }
  
  public void run(){
    int pause=50;
    while(true){
      offset++;
      cnt++;
      if(offset>-1) offset=-100;
      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);
    for(int r=0; r<3; r++){ //r = repeats of pattern
      for(int ct=0;ct<colors.length;ct++){
        for(int i=0; i<357; i++){
          xval[i]=i*4+offset;
          yval[i]=((int)(8000*Math.sin((double)(-i)))/400)+r*320;
          yval[i]+=ct*36;
        }
        xval[357]=xval[356];
        xval[358]=xval[359]=0;
        yval[357]=yval[358]=yval[356]+200;
        yval[359]=yval[0];
        offG.setColor(colors[ct]);
        Polygon wave = new Polygon(xval,yval,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=SINE.class height=620 width=1200></applet>
