/* DROP.java - Produces title screen incorporating
   a turlte that change shell color and shape. The turtle
   rises and drops one layer at a time.
   Created by Randy McGirr
   Trona, CA
   2011
*/
import java.awt.*;
import java.applet.*;
import java.util.*;

public class DROP extends Applet implements Runnable{

  Dimension d;
  Image offI;
  SHELL shells[] = new SHELL[18];
  HEAD head;
  LEG front;
  LEG back;
  TAIL tail;
  int steps=0;
  int delay=10;
  boolean up = true;
  int cycle, inc, curr, cnt, ccc;
  int body, fleg, bleg;
  Thread tt;
  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 };
  String bt[] = {"b", "l", "a", "c", "k", "t", "u", "r", "t", "l", "e", ".", "u", "s"};
  String title[] = {"E", "V", "I", "L", " ", "A", "N", "I", "M", "A", "L", "S"};
  Random r;

  public void init(){
    d = getSize();
    r = new Random();
    head = new HEAD(100,-160);
    front = new LEG(150,-160);
    back = new LEG(100,-160);
    tail = new TAIL(100,-160);
    setShells(); 
    offI = createImage(d.width, d.height);
    tt = new Thread(this);
    tt.start();
  }
  
  public void reset(){
    for(int i=0; i<shells.length; i++){
      shells[i].reset();
    }
    front.reset();
    back.reset();
    tail.reset();
    head.reset();
  }
  
  public void setShells(){
    for(int i=0; i<shells.length; i++){
      if(i<3) shells[i]=new SHELL(170+(i*20), 50);
      else if(i<7) shells[i]=new SHELL(160+((i-3))*20, 70);
      else if(i<12) shells[i]=new SHELL(150+((i-7))*20, 90);
      else shells[i]=new SHELL(140+((i-12))*20, 110);
    }
  } 
  
  public void UP(){
    if(steps<18){
      shells[steps].move(0,-10);
    }
    if(steps<3){ if(shells[steps].getY()<70) steps++; }
    else if(steps<7){ if(shells[steps].getY()<90) steps++; }
    else if(steps<12){ if(shells[steps].getY()<110) steps++; }
    else if(steps<18){ if(shells[steps].getY()<130) steps++; }
    if(steps==18){
      head.move(0,-10);
      tail.move(0,-10);
      front.move(0,-10);
      back.move(0,-10);
      if(front.getY()<165){
        delay=200;
      }
    } 
  }
  
  public void DOWN(){
    //move entire row at a time in four steps
    if(steps==0){
      head.move(0,10);
      tail.move(0,10);
      front.move(0,10);
      back.move(0,10);
    }
    else if(steps==1){
       for(int i=12; i<18; i++){
         shells[i].move(0,10);
       }
    }
    else if(steps==2){
      for(int i=7; i<12; i++){
        shells[i].move(0,10);
      }
    }
    else if(steps==3){
      for(int i=3; i<7; i++){
        shells[i].move(0,10);
      }
    }
    else if(steps==4){
      for(int i=0; i<3; i++){
        shells[i].move(0,10);
      }
    }
    if(steps==0 && front.getY()>345) steps=1; // done with bottom row
    if(steps==1 && shells[12].getY()>310) steps=2; // done with 2nd row
    if(steps==2 && shells[7].getY()>290) steps=3;
    if(steps==3 && shells[3].getY()>270) steps=4;
    if(steps==4 && shells[0].getY()>250){
    delay=200;
    };
  }
  
  public void incShells(){
    for(int i=0; i<shells.length; i++) shells[i].move(0,10);
    head.move(0,10);
    tail.move(0,10);
  }
  
  public void DELAY(){
    delay--;
    if(delay==0){
      up=!up;
      steps=0;
    }
  }

  public void run(){
    int waiting=0; //wait time
    while(true){
      waiting++;
      waiting%=10;
      cycle++;
      cycle%=10;
      if(delay>0) DELAY();
      else if(up) UP();
      else if(waiting%2==0) DOWN();
      repaint(); 
      try{ 
        Thread.sleep(25); 
      }catch(InterruptedException ie){ } 
    } 
  }
  
  public Color rColor(){
    int pick=Math.abs(r.nextInt()%colors.length);      
    return colors[pick];
  }

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

  public void paint(Graphics g){
    Graphics og = offI.getGraphics();
    og.setColor(Color.black);
    og.fillRect(0,0,d.width,d.height);
    og.setFont(new Font("Courier", Font.BOLD, 48));
    for(int i=0; i<14; i++){
      og.setColor(rColor());
      og.drawString(bt[i], 362+i*26, 300);
    }
    for(int i=0; i<12; i++){
      og.setColor(rColor());
      int offset=r.nextInt()%3;
      og.drawString(title[i], 350+i*33, 160+offset);
    }
    og.setColor(Color.green);
    //og.drawString(""+front.getY(), 10,200);
    tail.draw(og);
    back.draw(og);
    front.draw(og);
    head.draw(og);
    for(int i=0; i<shells.length; i++){
      shells[i].draw(og, cycle);
    }
    g.drawImage(offI, 0, 0, this);
    og.dispose();
  }
}
//<applet code=DROP.class height=420 width=920></applet>
