
370 MAKING THINGS TALK
Chapter 1
Hello World!
Language: Processing
Prints out “Hello World!”
println("Hello World!\n");
Triangle drawing program
Language: Processing
Draws a triangle whenever the mouse button is not
pressed. Erases when the mouse button is pressed.
// declare your variables:
float redValue = 0; // variable to hold the red color
float greenValue = 0; // variable to hold the green color
float blueValue = 0; // variable to hold the blue color
// the setup() method runs once at the beginning of the program:
void setup() {
size(320, 240); // sets the size of the applet window
background(0); // sets the background of the window to black
fill(0); // sets the color to fill shapes with (0 = black)
smooth(); // draw with antialiased edges
}
// the draw() method runs repeatedly, as long as the applet window
// is open. It refreshes the window, and anything else you program
// it to do:
void draw() {
// Pick random colors for red, green, and blue:
redValue = random(255);
greenValue = random(255);
blueValue = random(255);
// set the line color:
stroke(redValue, greenValue, blueValue);
// draw when the mouse is up (to hell with conventions):
if (mousePressed == false) {
// draw a triangle:
triangle(mouseX, mouseY, width/2, height/2,pmouseX, pmouseY);
}
// erase when the mouse is down:
else {
background(0);
fill(0);
}
}
Hello