Exercises

A series of exercises for learning computer programming with Processing

A Basic into to Processing

Sketch 00 : Peanut Butter Sandwich

Using the following five items, write instructions for making a peanut butter and jelly sandwich:

  1. knife
  2. jar of peanut butter
  3. jar of jelly
  4. loaf of bread
  5. plate

save your instructions as a text file:
yourLastName_peanutButter.txt (mine would be schwartz_peanutButter)

Sketch 01 : Hello World

Sample file: Hello, World!

Copy and paste the code from the sample file into a new processing sketch. Manipulate the file to do the following things:

  1. Alter what the text says
  2. Change the background color and size of the sketch
  3. Display a numerical calculation (23*77 or 243/13, etc)
  4. Change the font & position of the text
  5. Display more text in a different font

Try to make an interesting composition given your limited set of tools.

Export and save your sketch in your student folder on the server with the folder name:

yourLastName_helloWorld (for me it would be schwartz_helloWorld)

Program 00

  1. Identify an activity that you perform daily
  2. Perform the activity and pay close attention to each step (try to be as granular as possible)
  3. Write a series of instructions explaining how to perform the activity
  4. Try to include at least one "improvement" in your "code" and explain this enhancement after your instructions. The improvement could be a way to perform the activity in less steps or with less money or that is easier/better in some way

Save your file as:
yourLastName_observationalAlgorithm

Sketch 02: 4 Types of Lines

note: - you may only use horizontal, vertical, or diagonal lines

Part 1:

  1. Draw (on paper) a regular pattern with 10 lines of 1 type.
  2. Write pseudo code to instruct someone else on how to create this pattern
  3. Replicate someone else's drawing with a Processing sketch
  4. Save it as yourLastName_oneLine

Part 2:
1. Replicate your sketch from Part 1 but use a for structure to automate your code.
2. Save it as yourLastName_oneLineFor

If time permits:)

Part 3:

  1. Draw (on paper) a regular pattern that includes your pattern from Part 1 and incorporates another pattern using the other 3 line types.
  2. Write pseudo code
  3. Replicate your pattern with a Processsing sketch
  4. Save it as yourLastName_fourLinesFor

Sketch 03 : Bounce

  1. 3.0 Move a shape from left to right across the screen.
  2. 3.1 Move a shape from left to right across the screen. When it moves off the right edge return it to the left;
  3. 3.2 Move a shape from left to right across the screen. When it hits the wall make it bounce back from right to left (forever)
  4. Save it as yourLastName_bounce

Sketch 04 : Imagine


// create an image variable
PImage img;

// load the image (it must be in your sketch's data folder
img = loadImage("someimage.jpg");

// tint (gray, alpha) or tint (val,val,val,alpha) tint(255,0,0,50);

// display the image image(name, x, y, width, height)
image(img,0,0);

  1. 4.1 load an image
  2. 4.2 load 3 versions of an image with 3 different tints
  3. 4.3 using a "for" loop, load lots of images and change their location and tint
  4. Save your file as yourLastName_Imagine

Program DRAW

  1. Design a Drawing Application using User Stories
  2. Implement your Drawing Application in Processing
  3. You may work in pairs
  4. Save your program as yourLastName_draw and turn in your User Stories

Class notes

User Stories

Try to break up your project into specific user stories - you should have at least 2 (but more might be necessary). The idea is to think about your software design in terms of specific requirements and to create a user story about each one. These can simply be typed into a text document and included in your processing project folder.

here's the basic format:
1. statement in the form "As a [user role], I want to [goal], so I can [reason]."
2. brief discussion/explanation of the requirement (including a diagram/sketch if necessary)
3. confirmation - how will you know if the feature is working

for example
1. As a [user], I want to [press/release the mouse], so I can [start/stop drawing]
2. Discussion - the tool should only draw when the mouse button is pressed down, when the user releases the button, drawing should stop
3. Confirmation
1. success: user presses the mouse and drawing starts, user releases the mouse and drawing stops
2. failure: drawing continues if user is not pressing the mouse button

Sample Code


// set an image variable
PImage arrow;

// set a speed variable
float speed;

void setup() {
// set your display size(400,400);

// load the image
arrow = loadImage("arrow.png");

}

void draw() {
// draw with points
//point(mouseX, mouseY);

// calculate speed
speed = abs(mouseX-pmouseX);

// control the stroke weight strokeWeight(speed);

// draw with lines
//line(mouseX, mouseY,pmouseX,pmouseY);

// draw with an image
image(arrow,mouseX,mouseY,speed,speed);
}

Program TYPE

Create a typing program to display a different image for each letter on the keyboard.

Class Notes

capture keyboard input

int x = 100;
int y = 100;

PFont font;

void setup() {
size(500,300);
smooth();
strokeWeight(4);
font = loadFont("Arial-Black-48.vlw");
textFont(font); }

void draw () {
background(204);
if (keyCode == UP) {
y--;
}
line (20,y,100,y);

text(key,28,75);
}


create simple mouse buttons

void setup() {
size(500,300);
noStroke(); }

void draw() {
background(204);
if ((mouseX <= width/2) && (mouseY <= height/2)) {
fill(0);
rect(0,0,width/2,height/2); // upper left
} else if ((mouseX <= width/2) && (mouseY > height/2)) {
fill(255,0,0);
rect(0,height/2,width/2,height/2); // upper right
} else if ((mouseX > width/2) && (mouseY < height/2)) {
fill(0,255,0);
rect(width/2,0,width/2,height/2); // lower left
} else {
fill(0,0,255);
rect(width/2,height/2,width/2,height/2); // lower left
}
}


capture mouse input

void setup () {
smooth();
size(300,300);
noStroke(); }

void draw() {
background(222);
float x = mouseX;
float y = mouseY;
float ix = width - mouseX;
float iy = width - mouseY;
if (mousePressed) {
cursor(CROSS); // ARROW, CROSS, HAND, MOVE, TEXT, WAIT
ellipse(ix,iy,20,20); }

}

Program TIME

Create a clock that communicates the passage of time through graphical quantity or imagery rather than numerical symbols.


basic clock:


int s,m,h;
String time;
PFont myfont;

int x = 65;
int y = 60;

void setup() {
size(300,100);
myfont = loadFont("Garamond-48.vlw"); textFont(myfont);

}

void draw() { background(0);

s = second();
m = minute();
h = hour();

time = h + ":" + m + ":" + s;

text(time,x,y);

}


line clock:


void setup() {

size(100,100); stroke(255);

}

void draw () {
background(0);
float s = map(second(),0,60,0,100);
float m = map(minute(),0,60,0,100);
float h = map(hour(),0,60,0,100);
line (s,0,s,33);
line (m,34,m,66);
line (h,67,h,100);

}

Program MAP

Examples

Step 0 - show the map

Step 1 - a dot for every state

Step 2 - change dot size by population

Step 3 - change dot color by population

Step 4 - add rollovers

download all examples

Assignment

Find a source of data about all 50 states and present it on a map.