import java.awt.*;
import java.awt.image.*;

public class Stick {
  double x1, y1, y2, x2, width;
  boolean head;
  Image hair = null;
  ImageObserver parent;

  Stick(double a, double b, double c, double d, double w, ImageObserver im){
    x1 = a;
    x2 = b;
    y1 = c;
    y2 = d;
    width = w;
    parent = im;
  }

  public void hasHead(boolean h){
    head = h;
  }

  public void setHair(Image h){
    hair = h;
  }

  public void xMove(){
    xMove(1.0);
  }

  public void xMove(double a){
    x1 += a;
    x2 += a;
  }

  public void yMove(){
    yMove(1.0);
  }

  public void yMove(double a){
    y1 += a;
    y2 += a;
  }

  public void moveEnd1(double dx, double dy){
    x1+=dx;
    y1+=dy;
  }

  public void moveEnd2(double dx, double dy){
    x2+=dx;
    y2+=dy;
  }

  public void rotateAround1(double a){
    double factor;

    double ang = Math.atan((y2-y1)/(x2-x1));
    if(x2 < x1){
      ang += (Math.PI);
    }
    factor = Math.sqrt( ((y2-y1)*(y2-y1)) +((x2-x1)*(x2-x1)));
    ang +=a;
    y2 = y1+ factor*Math.sin(ang);
    x2 = x1+ factor*Math.cos(ang);
  }

  public void rotateAround2(double a){
    double factor;

    double ang = Math.atan((y2-y1)/(x2-x1));
    if(x1 < x2){
      ang += (Math.PI);
    }
    factor = Math.sqrt( ((y2-y1)*(y2-y1)) +((x2-x1)*(x2-x1)));
    ang +=a;
    y1 = y2+ factor*Math.sin(ang);
    x1 = x2+ factor*Math.cos(ang);
  }



  public void draw(Graphics g, Transform2D t){    
    double llx, lly, lrx, lry, ulx, uly, urx, ury;


    double ang = Math.atan((y2-y1)/(x2-x1));
    ang += (Math.PI/2);
    ulx = x1-((width/2)*Math.cos(ang));
    llx = x1+((width/2)*Math.cos(ang));
    urx = x2-((width/2)*Math.cos(ang));
    lrx = x2+((width/2)*Math.cos(ang));

    uly = y1-((width/2)*Math.sin(ang));
    lly = y1+((width/2)*Math.sin(ang));
    ury = y2-((width/2)*Math.sin(ang));
    lry = y2+((width/2)*Math.sin(ang));

    int sx[] = { (int) t.x(llx, lly), (int) t.x(lrx, lry), (int) t.x(urx, ury), (int) t.x(ulx, uly) };
    int sy[] = { (int) t.y(llx, lly), (int) t.y(lrx, lry), (int) t.y(urx, ury), (int) t.y(ulx, uly) };
    g.setColor(Color.black);
    g.fillPolygon(sx, sy, 4);
    if(head){
      g.fillArc( (int) x1-20, (int) y1-20, 40, 40, 0, 360);
      g.setColor(Color.white);
      g.fillArc( (int) x1-15, (int) y1-15, 30, 30, 0, 360);
    }
    if(hair != null){
      g.drawImage(hair, (int) x1-40, (int) y1-40, parent);
    }
    g.setColor(Color.white);
  }
}
