Hardware Reference
In-Depth Information
Here's a second program that's
a bit more exciting. It illustrates
some of the main programming struc-
tures in Processing.
8
/*
Triangle drawing program
Context: Processing
Draws a triangle whenever the mouse button is not pressed.
Erases when the mouse button is pressed.
*/
NOTE: All code examples in this topic
will have comments indicating the
context in which they're to be used:
Processing, Processing Android mode,
Arduino, PHP, and so forth.
// 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);
}
}
 
Search WWH ::




Custom Search