HashSet grapes = new HashSet();

float waterwant = 90;

float midhomeX = 0;
float midhomeY = 0;
float mainX,mainY;
void setup(){
  frameRate(60);
  size(150,150);
  smooth();
  ///*
  grapes.add(new grape(60,28));
  grapes.add(new grape(90,28));
  grapes.add(new grape(75,28));

  grapes.add(new grape(67,40));
  grapes.add(new grape(83,40));
  //*/
  grapes.add(new grape(75,50));

  //calculate midpoint of grapes

  i = grapes.iterator();
  while(i.hasNext()){
    grape g= (grape)i.next();
    midhomeX += g.x; 
    midhomeY += g.y;
  }
  midhomeX /= grapes.size();
  midhomeY /= grapes.size();

  mainX = midhomeX;
  mainY = midhomeY;

  i = grapes.iterator();
  while(i.hasNext()){
    grape g= (grape)i.next();
    g.setRel(midhomeX,midhomeY);
  }  

  textFont(loadFont("Arial-Black-12.vlw"),12);


}
Iterator i;
float counter;
void draw() {
  counter += .3;
//  background(128,128,255);
  float waterHeight = waterwant;
  if(mouseY > waterwant - 10){
    waterHeight = mouseY+10  ;
  }

  stroke(0);
  //strokeWeight(2);
  noStroke();

  fill(0,0,255);

  rect(0,waterHeight,150,75);  

  fill(128,128,255);

  stroke(0);strokeWeight(2);
  for(int x = 0; x < 10;x++){

     ellipse(-20+(round(counter)%20)+x*20,waterHeight,20,20); 
  }
  noStroke();

  rect(0,0,150,waterHeight+4);
  
  midhomeX = mainX;
    midhomeY = mainY;
  
  if(mouseY < 75){
    if(mouseX > 50 && mouseX < 100){
      if(mouseX < 75){
        midhomeX = mouseX + 30;
      } 
      else {
        midhomeX = mouseX - 30;
      } 

    } else {
      
    }
  }



  //move grapes
  i = grapes.iterator();
  while(i.hasNext()){
    grape g= (grape)i.next();
    //g.move();
    g.homex = midhomeX + g.xrel;
    g.homey = midhomeY + g.yrel;


    g.move();
  }


  //calculate midpoint of grapes
  float midx = 0, midy = 0;
  i = grapes.iterator();
  while(i.hasNext()){
    grape g= (grape)i.next();
    midx += g.x; 
    midy += g.y;
        
  }
  midx /= grapes.size();
  ;
  midy /=  grapes.size();
  ;





  //DRAW FROM EACH GRAPE TO MIDPOINT
  stroke(0,128,0);
  strokeWeight(4);
  i = grapes.iterator();
  while(i.hasNext()){
    grape g= (grape)i.next();
    line(midx,midy,g.x,g.y);
  }
  //DRAW FROM TOP TO MIDPOINT

  line(75,0,midx,midy);


  //draw each grape
  i = grapes.iterator();
  while(i.hasNext()){
    grape g= (grape)i.next();
    g.draw();  
  }


}


class grape{
  float x,y,xs=0,ys=0, xrel,yrel;
  float homex,homey;
  grape(float px, float py){
    x = px; 
    y = py; 
    homex = x; 
    homey = y;
  }
  void draw(){
    stroke(0);
    strokeWeight(2);
    fill(128,0,128);
    ellipse(x,y,15,15);
  }
  void move(){
    //    x = homex; y = homey;
    if(mouseY <  waterwant && sqrt(    pow(x-mouseX,2)+pow(y-mouseY,2)    ) < 15) {
      if(x < mouseX){ 
        x = mouseX - 10; 
      } 
      else { 
        x = mouseX + 10; 
      }
      if(y < mouseY){ 
        y = mouseY - 10; 
      } 
      else { 
        y = mouseY + 10; 
      }
    }
    else {

      if(abs(x - homex) > 2.5) {
        if(x < homex) xs++; 
        if(x > homex) xs--; 
      }
      if(abs(y - homey) > 2.5) {
        if(y < homey) ys++; 
        if(y > homey) ys--; 
      }

      xs *= .82;
      ys *= .82;
      x += xs;
      y += ys;


    }
  }


  void setRel(float mx, float my){
    xrel = x - mx;
    yrel = y - my;
  }

}