FSOSS 2010/processing.js/example5

From CDOT Wiki
Jump to: navigation, search
/*
  FSOSS 2010
  Andor Salga
  Example of ArrayList and its methods add, remove, get, size
*/

ArrayList circles;

void setup(){
  size(500, 500);
  circles = new ArrayList();
 
  // set the fill and stroke colors to blue
  // for the circles once on setup
  stroke(#336699);
  fill(#336699);
}

void draw(){
  // clear the background to white
  background(#FFFFFF);
 
  // ArrayList's size function returns the number
  // of elements in the list
  for(int i = 0; i < circles.size(); i += 2){
    
    float x = (Float)circles.get(i);
    float y = (Float)circles.get(i+1);
    
    ellipse(x, y, 15, 15);
  }
 
  // if the mouse is pressed, add a circle
  if(mousePressed){
    circles.add((float)mouseX);
    circles.add((float)mouseY);  
  }
 
  // if the mouse button isn't down, remove one circle  
  else{
    int size = circles.size();
    if(size > 0){
      // remove the last two numbers
      circles.remove(size-1);
      circles.remove(size-2);
    }
  }
}

Run me