/* HEART.java - Produces turtle with heart shell plus
   title and attribution.
   Created by Randel W. McGirr
              Trona, CA
              February 2011 
*/
import java.awt.*;
import java.awt.geom.*;
import java.applet.*;
import java.util.*;

public class HEART 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;
  Image V,D,S,O,N,G;
  AffineTransform vtrans, dtrans, strans, otrans, ntrans, gtrans;
  Thread tt;

  public void init(){
    d = getSize();
    r = new Random();
    fleg=330;
    bleg=330;
    body=330;
    vtrans=new AffineTransform();
    dtrans=new AffineTransform();
    strans=new AffineTransform();
    otrans=new AffineTransform();
    ntrans=new AffineTransform();    
    gtrans=new AffineTransform();
    V=makeImage("V", Color.red,290,1);
    D=makeImage("D", Color.yellow,320,2);
    S=makeImage("S", Color.magenta,0,3);
    O=makeImage("O", Color.cyan,10,4);
    N=makeImage("N", Color.orange,40,5);
    G=makeImage("G", Color.green,70,6);
    offI = createImage(d.width, d.height);
    tt = new Thread(this);
    tt.start();
  }
  
  public void run(){
    int pause=333;
    while(true){
      repaint();
      try{ Thread.sleep(pause);
      }catch(InterruptedException ie){}
    }
  }
  
  private Image makeImage(String t, Color c, int r, int w){
    Image temp = this.createImage(100,100);
    Graphics gr = temp.getGraphics();
    gr.setColor(Color.black);
    gr.fillRect(0,0,100,100);
    gr.setFont(new Font("Corrier", Font.BOLD, 120));
    gr.setColor(c);
    gr.drawString(t,2,98);
    if(w==1){
      vtrans.setToTranslation(110, 180);
      vtrans.rotate(Math.toRadians(r), 50, 50);
    }
    else if(w==2){
      dtrans.setToTranslation(190, 85);
      dtrans.rotate(Math.toRadians(r), 50, 50);
    }
    else if(w==3){
      strans.setToTranslation(400, 45);
      strans.rotate(Math.toRadians(r), 50, 50);
    }
    else if(w==4){
      otrans.setToTranslation(505, 55);
      otrans.rotate(Math.toRadians(r), 50, 50);
    }
    else if(w==5){
      ntrans.setToTranslation(630, 100);
      ntrans.rotate(Math.toRadians(r), 50, 50);
    }
    else if(w==6){
      gtrans.setToTranslation(720, 180);
      gtrans.rotate(Math.toRadians(r), 50, 50);
    }
    
    return temp;
  }
  
  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]);
        offG.fillOval(start+xx*20,260-yy*20,12,12);
        offG.fillOval(start+xx*20+6,260-yy*20,12,12);
        int tx[]={start+xx*20+2,start+xx*20+16,start+xx*20+9,start+xx*20+2};
        int ty[]={260-yy*20+10,260-yy*20+10,260-yy*20+17,260-yy*20+10};
        Polygon heart= new Polygon(tx,ty, tx.length);
        offG.fillPolygon(heart);
        offG.setColor(Color.black);
        int gx[]={start+xx*20+6,start+xx*20+12,start+xx*20+9,start+xx*20+6};
        int gy[]={260-yy*20-1,260-yy*20-1,260-yy*20+4,260-yy*20-1};
        Polygon gap= new Polygon(gx,gy, gx.length);
        offG.fillPolygon(gap);
      }
      start+=10;
      endPT--;
    }
    int bcolor=Math.abs(r.nextInt()%colors.length);
    offG.setColor(colors[bcolor]);
     //tail
    int c[] = {body+42,body+55,body+40,body+22,body+32,body+42 };
    int d[] = {281,281,286,290,286,281 };
    Polygon tail = new Polygon(c,d,c.length);
    offG.fillPolygon(tail);
    //back leg
    int bx[]={bleg+68,bleg+78,bleg+88,bleg+58,bleg+68};
    int by[]={281,281,300,300,281};
    Polygon backleg=new Polygon(bx,by,bx.length);
    offG.fillPolygon(backleg);    
    //front leg
    int fx[]={fleg+118,fleg+128,fleg+138,fleg+108,fleg+118};
    int fy[]={281,281,300,300,281};
    Polygon frontleg=new Polygon(fx,fy,fx.length);
    offG.fillPolygon(frontleg);    
    //head
    int hx[]={body+140,body+165,body+175,body+190,body+200,body+190,body+175,body+165,body+150,body+140};
    int hy[]={281,281,260,260,281,295,295,290,290,281};
    Polygon head=new Polygon(hx,hy,hx.length);
    offG.fillPolygon(head);    
    offG.setColor(Color.black);
    offG.fillOval(body+186,270,5,5);
    offG.drawLine(body+195,290,body+185,285);
    offG.setColor(new Color(100,255,0));
    offG.setFont(new Font("Courier", Font.BOLD, 90));
    offG.drawString("blackturtle.us",90,380);
    offG.drawImage(V, vtrans, this);
    offG.drawImage(D, dtrans, this);
    offG.drawImage(S, strans, this);
    offG.drawImage(O, otrans, this);
    offG.drawImage(N, ntrans, this);
    offG.drawImage(G, gtrans, this);
    g.drawImage(offI,0,0,this);
  }
}

//<applet code=HEART.class height=450 width=1000></applet>
