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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Setting<br />

The Setting section includes the basic functions just discussed. One issue that may be new<br />

to you is the concept of a rendering state or graphics context. Graphics application software<br />

can make it seem that color is generated on a per-shape basis. If you need a blue rectangle,<br />

you grab the paint bucket tool, select a blue color, <strong>and</strong> click in your shape (or some<br />

similar procedure). What a tool like the paint bucket conceals is how color is implemented<br />

as a state change in a programming language like Java <strong>and</strong> <strong>Processing</strong>. As a comparison,<br />

I’ve included the code for a simple Java graphics application followed by its <strong>Processing</strong><br />

equivalent. Both programs generate the same output, except that the Java program does it<br />

in 19 lines (not including comments or skipped lines), while <strong>Processing</strong> does it in 5. Do not<br />

try to run the Java application from within <strong>Processing</strong>; it will not work. (As an aside, for the<br />

few experienced Java readers out there—<strong>and</strong> this comes from Ben Fry directly—classes<br />

from the java.awt.* <strong>and</strong> javax.swing.* packages should not be used within <strong>Processing</strong>,<br />

as they will generate inconsistent results.) What the Java vs. <strong>Processing</strong> code comparison<br />

should show you is how much stuff <strong>Processing</strong> h<strong>and</strong>les under the hood, or encapsulates,<br />

making our coding lives happier. Here’s the Java version:<br />

import java.awt.*;<br />

import java.applet.*;<br />

public class SimpleJavaExample extends Frame {<br />

//constructor<br />

public SimpleJavaExample(){<br />

setSize(200, 200);<br />

setBackground(Color.white);<br />

// this program still requires window closing behavior<br />

}<br />

//main method<br />

public static void main(String[]args){<br />

new SimpleJavaExample().setVisible(true);<br />

}<br />

//overrides default paint method<br />

public void paint(Graphics g){<br />

g.setColor(new Color(0, 0, 200));<br />

g.fillRect(25, 50, 50, 50);<br />

g.fillOval(125, 50, 50, 50);<br />

g.setColor(new Color(0, 0, 0));<br />

g.drawRect(25, 50, 50, 50);<br />

g.drawOval(125, 50, 50, 50);<br />

}<br />

}<br />

In <strong>Processing</strong>, to get the same output, you can just write the following:<br />

size(200, 200);<br />

background(255);<br />

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

rect(25, 25, 50, 50);<br />

ellipse(150, 50, 50, 50);<br />

PROCESSING LANGUAGE API<br />

725<br />

A

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

Saved successfully!

Ooh no, something went wrong!