Programming computers to do useful and interesting tasks requires that we write instructions in a language that the computer understands, called a programming language. In this course, we’ll use Processing, a Java-based language designed for visual applications.
Programs are developed using a development environment of some sort. Common environments include Eclipse, Visual Studio, and Netbeans. The basic elements of the Processing development environment are shown here:
You write program commands in the program area, press the run button (the play button just above the program area), and Processing will display a new window showing your visual output. Textual output, if there is any, is shown below the program area. As one would expect, this empty program produces an empty output.
Processing allows basic visual output to be generated by
calling graphical methods with respect to a coordinate
plane. Methods are called very much as functions are called in
Excel, with the name of method first followed by a parenthesized
list of argument values (e.g., point(30, 40)
).
This program issues one command. It calls the point method to
render a single point of color, called a pixel, on an x-y
coordinate plane with the origin, point (0, 0)
, on
the upper left. The point is placed the specified number of pixels
to the right of the origin (i.e., 30) and below the origin (i.e.,
40). This model is common for computational graphics packages. The
default drawing color is black and the default background color is
gray.
This speck of an example may seem a bit simple, but in computing it is generally best to start simple, very simple, and build from there. The next example draws a point and a circle.
This program includes two commands, executed one after the other. The first renders a point in the middle of the 100x100 pixel canvas and the second renders an ellipse with the given center point, width and height. We drew the ellipse first because it would have covered up the point had it been drawn second.
Note that circles are ellipses with equivalent width and height (i.e., the diameter). One might have wished for a circle method, which would be easier to use than the ellipse method because it would only require a center point and a diameter, but computing packages tend to provide a smaller number of more generalized methods, like ellipse(), that can be used in a greater variety of situations.
Use a sequence of graphical methods to draw a simple face,
such as the one shown here. For more methods, see the
Processing reference manual (http://processing.org/reference/)
for more “2D Primitives”. See, in
particular, line() and rect() ,
which you’ll use to draw the mouth and nose
respectively.
When you have your basic face working, try the following extensions:
|
Color is an important aspect of the visual world that computing systems model using a variety of digital encoding schemes. No scheme is perfect, but there are good schemes in common use that provide a useful approximation of color. We’ll consider two such schemes, grayscale and RGB.
One simple encoding scheme is to restrict the set of possible colors to shades of gray ranging from white to black and to represent these shades with a single number between 0 and 255.
Black is represented by 0 indicating no light; white is represented by 255 indicating full light; shades of gray are represented by numbers between 0 and 255.
RGB is a more sophisticated color encoding scheme that supports colors beyond “black and white” using a additive model that encodes colors according to the relative intensities of Red, Green and Blue (RGB).
Black is represented by no red, green or blue (0, 0, 0); white is represented by full intensity red, green and blue (255, 255, 255); other mixed shades are represented by specific intensities of the three additive primary colors.
The Processing environment provides a tool that makes finding the right color a bit easier.
This tool allows you to either choose a color from the color palette or to directly specify the RGB values. Note that it also supports HSB (Hue-Saturation-Brightness), another color encoding scheme.
As an example of the use of color, consider the improved smiley face example shown here.
background(255); smooth(); // This improves the rendering. // Draw the yellow circle. fill(250, 250, 90); ellipse(50, 50, 75, 75); //Draw the eyes. fill(0); ellipse(38, 40, 10, 20); ellipse(63, 40, 10, 20); // Draw the mouth noFill(); strokeWeight(2); arc(50, 48, 50, 50, radians(40), radians(140)); // See the reference for a discussion of arcs and radians. // Draw the reflections in the eyes. stroke(255); strokeWeight(2); point(40, 38); point(65, 38); |
Note the following features of the this program:
//
, which causes
Processing to ignore the remainder of the line. As a general
rule, programmers don’t comment on obvious things (e.g.,
point(10,10) // Draw a point at (10,10).
). Instead, they
make comments on things that others might not infer from the
code itself (e.g., // Draw the reflections in the
eyes.
). fill()
method, which takes either a grayscale value (e.g., 255 for
white) or RGB values (e.g., 250, 250, 90 for yellow). Calling
noFill()
tells Processing to stop filling open
figures; in the example, the mouth arc would have looked like a
pie slice had it been filled.stroke()
sets the color of the
stroke; strokeWeight()
sets the thickness of the
stroke. In the example, the face is outlined with a single-width
black stroke; the mouth and eye reflections are drawn with
double-width black strokes.Programs are not limited to geometric figures and color. They can manipulate anything for which there is a digital encoding scheme, including characters, numbers, images, audio clips and video.
Processing can load and render images as shown in this example.
// a simple image // kvlinden, 12may2009 size(120,120); background(0); image(loadImage("blueMarble.gif"),10,10); |
Note the following features of the this program:
loadImage()
, which expects a filename and returns
an image object holding the image that was loaded in the
specified file.Processing can create, load and render text fonts as shown in this example.
// a simple saying // kvlinden, 12may2009 size(375,200); background(255); textFont(loadFont("Papyrus-Regular-80.vlw"), 80); fill(160,110,31); text("1000 miles", 0, 120); textFont(loadFont("BlackadderITC-Regular-54.vlw"), 54); fill(232,161,47,150); text("A journey of", 80, 55); text("starts with one step.", 10, 155); |
Note the following features of the this program:
loadFont()
and
textFont()
as shown. The font file name given to
loadFont()
specifies the font face (e.g.,
“Papyrus”), its weight (e.g.,
“Regular”) and its size (e.g.,
“80” points). The font size given to
textFont()
is used to scale the font. The color of
the font is set using the fill()
as discussed
above.text()
.data
sub-folder of your
sketch project.
Processing allows you to combine geometric figures, images, text into a single sketch.
Submit all the code for your face sketch and your logo/poster.