openobject.org

Introduction to Processing

From Physical Programming

Processing is a programming language and integrated development environment (IDE) specifically designed for visual outputs. Processing will run on GNU/Linux, Mac OS X, and Windows systems and is released under a GNU General Public License so it is free to download and use.

Processing is based on the Java programming language. It has a relatively simple structure and syntax and has been designed to introduce non-programers to programming.

Programs written in Processing are called 'sketches'. Each sketch has its own document window were the program code is entered and run. The top of the window contains a toolbar, the bottom shows feedback messages. When sketches are run a 'Display Window' opens. The Display Window displays the visual output from the sketch.

Image:ide.gif

Processing has three programming modes: Basic, Continuous, and Java.

Basic Mode

Basic mode is for beginners who are new to programming. Code entered in basic mode is executed in order from top to bottom.

size(200, 200);
background(255);
noStroke();
fill(255, 204, 0);
rect(30, 20, 50, 50);

Each line of code above calls a function. The function name is always followed by a pair of regular brackets. These brackets contain 'parameters'; information that is passed to the function. Some functions have no parameters and hence their brackets are empty.

A list of available functions can be found on the Processing Reference Pages.

Loop functions can also be used in Basic mode.

Continuous Mode

Continuous mode provides structure for creating your own functions and classes. These structures can be thought of as similar to the patches in Pure Data, Quartz Composer, or Explicit History. They provide the building blocks that make up a complex program. They can have inputs (parameters) and outputs (returns). You make connections between functions by 'calling' the function. In the example below circles(40,80) calls the circles() function with parameters 40 and 80. All continuous mode sketches begin with a setup() function.

void setup() {
  size(200, 200);
  noStroke();
  background(255);
  fill(0, 102, 153, 204);
  smooth();
  noLoop();
}

void draw() {
  circles(40, 80);
  circles(90, 70);
}

void circles(int x, int y) {
  ellipse(x, y, 50, 50);
  ellipse(x+20, y+20, 60, 60);
}

The second function called is draw(). The draw() function repeats continuosly until the program exits or it is stopped with a noLoop(). The program must contain a draw() function if you want it to respond to events such as mouse clicks. A program can only have one draw() function.

void setup() {
  size(200, 200);
}

void draw() { }

void mousePressed() {
  line(mouseX, 10, mouseX, 90);
}

Some functions 'return' a value or object when they are called. The type of data to be returned is defined in the first line of a function. If a function does not return any data then the data type is given as 'void'. None of the above functions return a value so the data types are all defined as void.

Java Mode

Java mode allows for complete Java programs to be written inside the Processing Environment. It is only recommended for advanced users.


More information on the Processing Environment can be found on the Processing website.

The above code is based on examples from the excellent Processing Reference Library accessed 17th April 2008.