04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

112<br />

(if you really want to). Above the hardware level is where you’ll do most of your graphics<br />

coding in <strong>Processing</strong>.<br />

No matter how high level the process of drawing to the screen is made for you, either<br />

through a programming language like <strong>Processing</strong>, or even more simply, a pencil tool in a<br />

graphics application, the same underlying stuff still needs to happen. These underlying<br />

processes can be quite complex in a language like Java, <strong>and</strong> involve low-level processing,<br />

pretty close to the hardware. For example, there’s a class in Java called BufferedImage that<br />

provides access to much of the procedural <strong>and</strong> structural anatomy of an image. Happily,<br />

you don’t need to underst<strong>and</strong> the BufferedImage class to work with images in <strong>Processing</strong>;<br />

but it can be helpful, or at least interesting, to see a little of what goes into creating an<br />

image behind the scenes. I really want to stress that you don’t need to underst<strong>and</strong> anything<br />

about the BufferedImage class to work in <strong>Processing</strong>, but learning a little about it<br />

will give you more insight into how images work in Java <strong>and</strong> in computer graphics in general.<br />

The BufferedImage class is composed of a bunch of other classes. Figure 4-3 shows a<br />

simplified diagram of its structure.<br />

Figure 4-3. Simplified BufferedImage class structure<br />

The BufferedImage class diagram looks pretty scary. I’ve had advanced computer science<br />

students brought to their knees trying to unravel it. What makes it so complicated is the<br />

abstraction involved in its design <strong>and</strong> ultimately the processing required to generate <strong>and</strong><br />

manipulate images on a computer screen. If you begin to feel sick to your stomach reading<br />

about this stuff, just skip ahead to the “Graphic formats” section.<br />

fill(0, 0, 255);<br />

rect(0, 0, 10, 10);<br />

These two lines of processing code draw a blue rectangle with a black stroke (border) to<br />

the screen. Let’s look a little below the surface at what’s happening. The rect() function<br />

in <strong>Processing</strong>, which relates to the Rectangle() method call in Java, is a drawing method<br />

built into the <strong>Processing</strong> language. You simply pass four arguments to the rect() function:<br />

x, y, width, <strong>and</strong> height, <strong>and</strong> the function draws a rectangle to those specifications. Drawing<br />

methods usually work by plotting points, called vertices (singular is vertex), <strong>and</strong> then connecting<br />

the vertices with some type of line. In <strong>Processing</strong>, as in numerous other programming<br />

languages with graphics capabilities, there are drawing methods to draw points, lines,<br />

<strong>and</strong> basic shapes (simple polygons). In the preceding rectangle example, four vertices are

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!