FSOSS 2010/processing.js/example6

From CDOT Wiki
Jump to: navigation, search
/*
  FSOSS 2010
  Andor Salga
  Example of line, ArrayList, mousePressed, mouseReleased
*/
 
boolean drawingLine = false;
 
float lineX;
float lineY;
 
ArrayList lineCoords;
 
void setup(){
  size(300, 300);
  lineCoords = new ArrayList();
}
 
void draw(){
  background(33, 66, 99);
 
  for(int i = 0; i < lineCoords.size(); i += 4){
    line( (Float)lineCoords.get(i),
          (Float)lineCoords.get(i+1),
          (Float)lineCoords.get(i+2),
          (Float)lineCoords.get(i+3));
  }
 
  if(drawingLine){
    line(lineX, lineY, mouseX, mouseY);
  }
}
 
/*
  Function gets called once, as soon as the
  mouse button is pressed.
*/
void mousePressed(){
  drawingLine = true;
  lineX = (float) mouseX;
  lineY = (float) mouseY;
}
 
/*
  Function gets called once, as soon as the
  mouse button is released.
*/
void mouseReleased(){
  drawingLine = false;
 
  lineCoords.add(lineX);
  lineCoords.add(lineY);
  lineCoords.add((float) mouseX);
  lineCoords.add((float) mouseY);
  println(lineX + " " + lineY + " " + mouseX + " " + mouseY);
}

Run me