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.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

162<br />

Java mode<br />

This mode allows you to work in pure Java, from directly within the <strong>Processing</strong> text editor.<br />

Java mode is extremely flexible, giving you access to the entire Java API. However, this<br />

mode is for advanced users who already have a working knowledge of Java. I’ll discuss<br />

Java mode in more detail in Chapter 14.<br />

Rendering modes<br />

<strong>Processing</strong>, as of this writing, has three rendering modes: JAVA2D, P3D, <strong>and</strong> OPENGL. The<br />

mode can be explicitly set as an optional argument within <strong>Processing</strong>’s size() method. For<br />

example, to use the P3D rendering mode, you would write: size(200, 200, P3D);. The<br />

rendering modes control how visual data is rendered, or converted to pixels on the screen.<br />

In addition to these three modes, there is a fourth mode, called P2D, that will likely be<br />

functional in the near future (it very well may be active by the time you’re reading this).<br />

P2D is an alternative renderer (obviously for 2D rendering) to JAVA2D.<br />

JAVA2D mode<br />

JAVA2D uses Java’s 2D graphics library for creating 2D rendering. According to Sun, Java 2D<br />

is “a set of classes for advanced 2D graphics <strong>and</strong> imaging.” The library contains over 100<br />

classes organized into packages according to related functionality (e.g., imaging, drawing,<br />

<strong>and</strong> color). JAVA2D is the default rendering mode in <strong>Processing</strong>. Thus, when no rendering<br />

argument is explicitly specified within the size() function, JAVA2D is used. Coders normally<br />

don’t bother adding the JAVA2D argument to the size() call for this reason.<br />

Here is a JAVA2D example that generates a polygon, in this case an octagon. You can<br />

change the value of the sides variable to generate other types of polygons. All the examples<br />

in the book thus far have been in JAVA2D mode. Figure 5-12 shows the output of the<br />

example.<br />

/*<br />

Polygon<br />

Ira Greenberg, November 3, 2005<br />

*/<br />

// declare variables<br />

int sides = 8; // change to create other polys<br />

float angle;<br />

float radius = 100;<br />

float px, py, cx, cy;<br />

float[]ptsX= new float[sides];<br />

float[]ptsY= new float[sides];<br />

//initialize sketch<br />

void setup(){<br />

size(200, 200, JAVA2D);

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

Saved successfully!

Ooh no, something went wrong!