 |
Programming Modes
Processing allows people to program at three levels of complexity: Basic
Mode, Continuous Mode, and Java Mode. People new to programming should
begin with the Basic Mode to learn about coordinates, variables, and loops
before moving to Continuous and Java modes.
Basic
This mode is used drawing static images and learning fundamentals of programming.
Simple lines of code have a direct representation on the screen. The following
example draws a yellow rectangle on the screen:
size(200, 200);
background(255);
noStroke();
fill(255, 204, 0);
rect(30, 20, 50, 50);
Continuous
This mode provides a setup() structure that is run once
when the program begins and a draw() structure which by default continually
loops through the code inside. This additional structure allows writing
custom functions and classes and using keyboard and mouse events.
This example draws four circles on the screen and utilizes a custom function
called circles(). The circles() function is not a part of the Processing
language, but was written for this example. The code in draw() only runs
once because noLoop() is called in setup().
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);
}
This example draws rectangles that follow the mouse position (stored in
the system variables mouseX and mouseY). The draw() section
runs forever until the program is stopped, thus creating the potential for
motion and interaction.
void setup()
{
size(200, 200);
rectMode(CENTER);
noStroke();
fill(0, 102, 153, 204);
}
void draw()
{
background(255);
rect(width-mouseX, height-mouseY, 50, 50);
rect(mouseX, mouseY, 50, 50);
}
Java
This mode is the most flexible, allowing complete Java programs to be written
from inside the Processing Environment. Writing in Java Mode removes the
limitations of the Processing Libraries and gives access to the full Java
programming language.
public class MyDemo extends PApplet {
void setup()
{
size(200, 200);
rectMode(CENTER);
noStroke();
fill(0, 102, 153, 204);
}
void draw()
{
background(255);
rect(width-mouseX, height-mouseY, 50, 50);
rect(mouseX, mouseY, 50, 50);
}
}
|
|