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

From CDOT Wiki
Jump to: navigation, search
(Created page with 'category: FSOSS 2010 PJS Examples <source lang="JavaScript"> FSOSS 2010: boolean drawingLine = false; float lineX1; float lineY1; float lineX2; float lineY2; Array…')
 
Line 3: Line 3:
 
/*
 
/*
 
   FSOSS 2010
 
   FSOSS 2010
 +
  Example of line, ArrayList, mousePressed, mouseReleased
 
*/
 
*/
  
 
boolean drawingLine = false;
 
boolean drawingLine = false;
  
float lineX1;
+
float lineX;
float lineY1;
+
float lineY;
 
 
float lineX2;
 
float lineY2;
 
  
 
ArrayList lineCoords;
 
ArrayList lineCoords;
Line 30: Line 28:
 
   }
 
   }
 
   
 
   
  //
 
 
   if(drawingLine){
 
   if(drawingLine){
     line(lineX1, lineY1, mouseX, mouseY);
+
     line(lineX, lineY, mouseX, mouseY);
 
   }
 
   }
 
}
 
}
Line 42: Line 39:
 
void mousePressed(){
 
void mousePressed(){
 
   drawingLine = true;
 
   drawingLine = true;
   lineX1 = (float) mouseX;
+
   lineX = (float) mouseX;
   lineY1 = (float) mouseY;
+
   lineY = (float) mouseY;
 
}
 
}
  
Line 53: Line 50:
 
   drawingLine = false;
 
   drawingLine = false;
 
   
 
   
   lineCoords.add(lineX1);
+
   lineCoords.add(lineX);
   lineCoords.add(lineY1);
+
   lineCoords.add(lineY);
 
   lineCoords.add((float) mouseX);
 
   lineCoords.add((float) mouseX);
 
   lineCoords.add((float) mouseY);
 
   lineCoords.add((float) mouseY);

Revision as of 15:05, 25 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);
}

Run me