Intro To Processing
Intro To Processing
The Basics
Processing
• Processing started by Ben Fry and Casey Reas while both were graduate students
at MIT Media Lab in 2001.
• Processing is Java.
• Designed for visual artists with limited programming experience who want to
create art without knowing complicated Java syntax.
• In its current version, hundred of libraries have been written for computer vision,
data visualization, music composition, networking, 3D drawings and
programming electronics.
Structure of a Sketch
A sketch or program in processing only needs two methods: setup and draw.
void setup(){
…
}
void draw(){
…
}
setup()
//declare global variables on top
void draw(){
…
}
draw()
void setup(){
…
}
void draw(){
…
}
File System
When a sketch is saved, Processing will create new folder whose name
matches the name of the sketch(without the .pde extension).
void setup(){
…
}
void draw(){
…
}
Example
int x, y; //declare global variables
• background(int range): range can be any integer from 0=black to 255=white. This
should go in draw().
• background(int r, int g, int b): Background with RGB values. This should
go in draw().
Some Methods
• fill(int r, int g, int b):By calling fill BEFORE a shape will set the color of
the shape. Call it again before drawing another shape to change color.
• strokeWeight(int pixel): Call BEFORE drawing a shape will set thickness of the
boundary.
• rect(int x, int y, int width, int height): position is top left corner by
default.
Draw a Line
Draw a Rectangle
Adding Text
textSize(32);
fill(255, 0, 0);
text("Hello, World!", 100, 200);
Example 1
int x,y;
void setup(){
size(800,600); // 800 pixels by 600 pixels
x = width / 2;
//width=800, height=600, these variables are reserved.
y = height / 2; }
void draw(){
background(255);//white background
fill(255, 0, 0);//red fill
ellipse(x, y, 80, 80);//red-filled circle radius 40
//continued on the next slide…
Example 1 Continued..
fill(0, 255, 0);//green fill
ellipse(x, y, 80, 80);//green-filled circle radius 40
fill(0, 0, 255); //blue fill
//blue filled rectangle at (40,50) width=60,height=70
rect(40, 50, 60, 70);
}
Example 2:Make it move.
int x, y, xspeed = 5;
void setup(){
size(600,400);
x = width/2;
y = height/2;}
// make a red circle moves horizontally
// across the screen
void draw(){
background(255);//white background
fill(255,0,0); //fill red
ellipse(x,y,80,80);
x += xspeed; //moves 5*60=300 pixels per sec.
}
Processing (keyPressed)
The draw() loop runs continuously until it is interrupted by an
event, for example, a keyboard or mouse event.
void draw(){…}
Processing keeps track of the position of the mouse at any given time
through the variables mouseX, mouseY.
println(4);
println(4 + 3/2);
println(“Hello, world”);
float vs double
• float: 32-bit data type representing real numbers to about 7 decimal places.(Not on
AP Exam)
• double: 64-bit data type representing real numbers to about 16 decimal places.
float vs double
http://www.processing.org
Things to Try
Lab 1: Make the ball move horizontally back and forth in the middle of the
screen, bouncing each time it hits the left or right side of the screen.
Lab 2: Use mouseX and mouseY to write a program that draw a red ball which
follows your mouse.
Things to Try
Lab 3: Create a window of size 800p by 600p. Use for loops to draw horizontal
and vertical lines every 100 pixels. You can use strokeWeight(int p) to adjust
the thickness of the lines.
Things to Try
Lab 4: Draw a blue rectangle at the center of the screen. Let the user move the
rectangle up, down, left, right using the keyboard arrow keys. Hint: It is
better(more smooth) if you use both keyPressed and keyReleased.
References
For more tutorials/lecture notes in Java, Python, game
programming, artificial intelligence with neural networks:
https://longbaonguyen.github.io
• https://processing.org/tutorials/