import processing.core.*; import java.applet.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.text.*; import java.util.*; import java.util.zip.*; import javax.sound.midi.*; import javax.sound.midi.spi.*; import javax.sound.sampled.*; import javax.sound.sampled.spi.*; import java.util.regex.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.sax.*; import javax.xml.transform.stream.*; import org.xml.sax.*; import org.xml.sax.ext.*; import org.xml.sax.helpers.*; public class basho extends PApplet {
yoyo y;
HashSet frogs = new HashSet();
HashSet deadfrogs = new HashSet();

HashSet flies = new HashSet();

int frogcount = 3;


public void setup(){
  size(400,400);

  y = new yoyo(229,200,"yoyo.gif");

 frogs= new HashSet();
 deadfrogs= new HashSet();
 flies = new HashSet();


  for(int i = 0; i < frogcount;i++){
    //frog f = new frog(round(random(400)),round(random(400)),"froglive.gif");
    
    //frogs.add(f);
    addFrog();
  }
String[] a = {"fly1.gif","fly2.gif"};
  for (int i =0; i < 3;i++){
     flies.add(new fly(round(random(100)+100 ),round(random(100)+100),a ));
  }

}

int c;




public void draw(){
  background(128);
  //m.x = mouseX;
  // m.y = mouseY;

  c++;
  if(c % 50 == 0){
    //m.changeframe(m.currentframe==0?1:0); 
  }


  y.move();

  y.draw();

  int howmanyToAdd = 0;

  Iterator it = frogs.iterator();
  while(it.hasNext()){
    frog f = (frog)it.next();
    f.bounds();
    f.move();
    

    f.draw();
    if(y.overlap(f)){
    //frog df = new frog(f.x,f.y,"deadfrog.gif");
    //deadfrogs.add(df);
    f.xs = y.xs;
    f.ys = y.ys;
   // it.remove();
     howmanyToAdd ++;
    
    }
  }

for (int i = 0; i <  howmanyToAdd ;i++){
 //  addFrog(); 
}


  it = flies.iterator();
    while(it.hasNext()){
    fly f = (fly)it.next();
    f.move();

    f.draw();
    
    }

}

class fly extends sprat{
    float xs,ys, fy,fx;
  float sz = 20;
   fly(int x, int y, String s[]){
    super(x,y,s);
    fy = y; 
    fx = x;

  }
  
  public void move(){
     if(fx < 200){xs += .3f;} 
          if(fy < 200){ys += .3f;}
     if(fx> 200){xs -= .3f;} 
          if(fy > 200){ys -= .3f;}
  
    fx += xs;
    fy += ys;
    x = round(fx);
    y = round(fy);
  }
}

class frog extends sprat{
  frog(int x, int y, String s){
    super(x,y,s);
    fy = y; 
    fx = x;

  }
  float xs,ys, fy,fx;
  float sz = 20;
  public void move(){
    ys += .1f;
    fy+=ys;
    fx+=xs;
    x = round(fx);
    y = round(fy);
  }

  public void bounds(){
     if(fx < 0){
        xs = abs(xs);
        fx = 0 ;
     } 
     if(x+sz+sz >= 400){
        xs = -abs(xs);
        fx = 400 - sz - sz;
     } 
     if(y < 0){
        ys = abs(ys);
        
        fy = 0;
     } 
     if(y+sz >= 400){
        ys = -abs(ys);
        fy = fy - sz - sz;
     } 
  }
}

class yoyo extends sprat{
  float fx,fy,xs,ys;
  boolean mOn = true;
  yoyo(int x, int y, String s){
    super(x,y,s);
  }
  public void move(){
    ys += .8f; //gravity
    if(mOn) {
      xs += ( mouseX - (fx+20)) / 10.0f; 
      ys += ( mouseY - (fy+20)) / 10.0f;
    }
    // if(mOn){
    xs *= .8f;
    ys *= .8f;
    // }
    fx += xs;
    fy += ys;
    y = round(fy);
    x = round(fx);
  }
  public void draw(){
    stroke(0);
    strokeWeight(2);
    line(mouseX,mouseY,fx+20,fy+20);
    super.draw();
  }


}



class sprat{
  int x,y,w,h;
  int framecount;
  int currentframe;
  PImage img[];
  sprat(int px,int py, String imgname){
    String s[] = new String[1];
    s[0] = imgname;
    init(px,py,s);
  } 
  sprat(int px,int py, String[] imgname){
    init(px,py,imgname);
  } 

  public void init (int px,int py, String[] imgname){
    x = px; 
    y = py; 
    img = new PImage[imgname.length];
    for(int i = 0; i < imgname.length; i++){
      img[i] = loadImage(imgname[i]);      
    }
    changeframe(0);
  } 
  public void changeframe(int whatframe){
    currentframe = whatframe;
    w = img[currentframe].width;
    h = img[currentframe].height;
  }

  public void draw(){
    image(img[currentframe],x,y);
  }

  //so we have a physical x and y on screen
  //find the pixel for this thing
  //WARNING no bounds checking
  public int pixelAtPhysicalLocation(int px, int py){
    int rx = px - x;
    int ry = py - y;
    return img[currentframe].pixels[rx + (ry * w)];
  }

  public boolean overlap(sprat other){
    intRange vOverlap = spratOverlapVert(other);
    intRange hOverlap = spratOverlapHoriz(other);
    if(vOverlap == null || hOverlap == null){
      return false;
    }
    //hrrm, why couldn't this be <= ????
    for(int a = hOverlap.min; a < hOverlap.max; a++){
      for(int b = vOverlap.min; b < vOverlap.max; b++){
        if(alpha(this.pixelAtPhysicalLocation(a,b)) > 128 && alpha(other.pixelAtPhysicalLocation(a,b)) > 128    ){
          return true; 
        }
      }
    }

    return false;
  }

  // to see if things overlap on one dimension
  // we sort the 4 points. if both points of 
  // one thing are lesser than both points of the other,
  // they can't be overlapping...
  public intRange spratOverlapHoriz(sprat b){
    sprat a = this;
    intWithRef vals[] = new intWithRef[4];
    vals[0] = new intWithRef((int)a.x,a);
    vals[1] = new intWithRef((int)(a.x+a.w),a);
    vals[2] = new intWithRef((int)b.x,b);
    vals[3] = new intWithRef((int)(b.x+b.w),b);
    Arrays.sort(vals);
    if (vals[0].src == vals[1].src){
      return null; 
    }
    return new intRange(vals[1].val,vals[2].val);
  }

  public intRange spratOverlapVert(sprat b){
    sprat a = this;
    intWithRef vals[] = new intWithRef[4];
    vals[0] = new intWithRef((int)a.y,a);
    vals[1] = new intWithRef((int)(a.y+a.h),a);
    vals[2] = new intWithRef((int)b.y,b);
    vals[3] = new intWithRef((int)(b.y+b.h),b);
    Arrays.sort(vals);
    if (vals[0].src == vals[1].src){
      return null; 
    }
    return new intRange(vals[1].val,vals[2].val);

  }

}

class intRange{
  int min, max;
  intRange(int p1, int p2){
    min = p1<p2?p1:p2; 
    max = p1>p2?p1:p2; 
  }
}

class intWithRef implements Comparable{
  int val; 
  Object src;
  intWithRef(int pval,Object psrc){
    val = pval; 
    src = psrc;
  } 
  public int compareTo(Object o){
    return val - ((intWithRef)o).val;
  }

}





public void addFrog(){
  //every dude is launched  from one of the 4 walls
  float posOnWall = random(400);
  float mainSpeed = random(10);
  //speed parallel to wall
  float lateralSpeed = random(10);
  if(posOnWall > 100){
    lateralSpeed *= -1; 
  } 
  float x=0,y=0, xs=0,ys=0;
  int r =   PApplet.parseInt(random(4));
  if(r == 0){ //leftwall
    x = -5;
    y = posOnWall;
    xs = mainSpeed;
    ys = lateralSpeed;
  }
  if(r == 1){ //rightwall
    x = 400+5;
    y = posOnWall;
    xs = -1*mainSpeed;
    ys = lateralSpeed;
  }
  if(r == 2){ //topwall
    x = posOnWall;
    y = -5;
    xs = lateralSpeed;
    ys = mainSpeed;
  }
  if(r == 3){ //topwall
    x = posOnWall;
    y = 400+5;
    xs = lateralSpeed;
    ys = -1* mainSpeed;
  }


  frog f = new frog(round(x),round(y),"froglive.gif");
  f.xs = xs;
  f.ys = ys;
  
  frogs.add(f);
}


  static public void main(String args[]) {     PApplet.main(new String[] { "basho" });  }}