Difference between revisions of "FSOSS 2010/processing.js/example6"

From CDOT Wiki
Jump to: navigation, search
Line 54: Line 54:
 
   lineCoords.add((float) mouseX);
 
   lineCoords.add((float) mouseX);
 
   lineCoords.add((float) mouseY);
 
   lineCoords.add((float) mouseY);
   println("Coords: (" + (int)lineX + ", " + (int)lineY + ", " + mouseX + ", " + mouseY + ")");
+
   println(lineX + " " + lineY + " " + mouseX + " " + mouseY);
 
}
 
}
 
</source>
 
</source>
  
[http://studio.sketchpad.cc/???? Run me]
+
[http://studio.sketchpad.cc/JzipgTGyKN Run me]

Revision as of 12:20, 26 October 2010

/*
  FSOSS 2010
  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