Courses‎ > ‎APCS - Term 1‎ > ‎Konstantinovich‎ > ‎

2019-05-02

posted May 2, 2019, 6:23 AM by Konstantinovich Samuel
You can download and learn more about Processing at  https://processing.org/

On the school computers, just type processing in the terminal to run Processing.

-Processing is an IDE + library for Java.
-It creates a window that you are able to interact with, both by displaying information, and by reading input from the user (mouse + keyboard)
-You can use ALL of Java's features in Processing.



There is some class that you cannot see and there are two modes:



Simple Mode: (When you write only statements, and no methods)

public class HIDDEN_FROM_YOU{

    void setup(){  
        |YOU EDIT THIS SECTION IN THE IDE
        |Variables are local
        |You cannot write methods or inner classes

    }
}

Example of simple mode:


size(800,600);
background(255);
fill(255,255,0);//yellow
rect(100,height/2,60,50);
int x = 20;
int y = 20;

fill(255,100,255);//purple
while( x < width  && y < height){
   rect(x,y,50,50);
   x+= 100;
   y+= random(100)+50;
}




Standard Mode: (you can write the entire class, including instance variables and methods)
public class HIDDEN_FROM_YOU{

    |YOU EDIT THIS SECTION IN THE IDE
    |Variables are instance variables
    |You can write methods and inner classes

}

The typical parts of a processing program:

1. instance Variable declaration.
2. void setup() - a method that runs once when you first start the program. This sets the size of the screen, and typically initializes the instance variables
3. void draw() - a method that repeats forever until you stop the program. This is how we achieve animations and motion.




Example of a typical processing program:
int lastShade;
int direction;
 
void setup() {
  size(800, 600);
  background(102);
  lastShade = 0;
  direction = 7;
}

void draw() {
  fill(lastShade);
  //mouseX is the current Mouse X coordinate.
  //pmouseX is the previous Mouse X coordinate.
  variableEllipse(mouseX, mouseY, pmouseX, pmouseY);
  lastShade +=direction;
 
  if(lastShade > 255){
    lastShade = 255;
    direction *= -1;
  }
  if(lastShade < 0){
    lastShade = 0;
    direction *= -1;
  }
}

void variableEllipse(int x, int y, int px, int py) {
  float d = 1.5*dist(x,y,px,py);
  ellipse(x, y, d, d);
 
}


Example of how you would organize a processing program that lets you send clicks into an object, and re-draws the object repeatedly:
CustomThing w; //this is a custom class you don't have

void setup() {
  size(900, 900);
  background(0);
  w = new CustomThing(50, 50, width-100, height-100);
}
void draw() {
  background(255);
  w.display();
}

void mouseClicked(){
  if(mouseButton == LEFT)
   w.lclick();
if(mouseButton == RIGHT)
   w.rclick();
}



Comments