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 />

706<br />

Files<br />

if(key == CODED) {<br />

if (keyCode == UP) {<br />

y--;<br />

} else if (keyCode == DOWN) {<br />

y++;<br />

} else if (keyCode == LEFT) {<br />

x--;<br />

} else if (keyCode == RIGHT) {<br />

x++;<br />

}<br />

}<br />

}<br />

The Files section includes four functions:<br />

•openStream()<br />

•open()<br />

•loadStrings()<br />

•loadBytes()<br />

loadStrings() <strong>and</strong> loadBytes() allow you to load the contents of a file or URL as a string<br />

array or byte array, respectively. A byte array of a text file will include all the individual<br />

characters converted into their ASCII equivalents. If only a file name is specified as an<br />

argument with either of these functions, the file needs to reside within the sketch’s data<br />

directory. It’s also possible to specify a URL, assuming you use a valid path. Relative paths<br />

should be specified in relation to the data directory in the current sketch directory. For<br />

example, if your file (file.txt) is outside the data directory, in another directory called<br />

docs (also located within the sketch (parent) directory), it can be accessed (using UNIX/OS<br />

X path syntax) as: ../docs/file.txt, or on Windows: ..\docs\file.txt. The double dot<br />

syntax (..) refers to the parent directory; (single dot syntax(.) refers to the current directory).<br />

There is some general information about paths at http://en.wikipedia.org/wiki/<br />

Path_(computing).<br />

The openStream() function provides a simplified way of using Java’s InputStream class.<br />

The InputStream class provides you with more control when inputting data. To learn more<br />

about Java’s InputStream class, check out http://java.sun.com/j2se/1.4.2/docs/api/<br />

java/io/InputStream.html.<br />

Here’s what the <strong>Processing</strong> reference says about the open() function: “Attempts to open a<br />

file or process using the platform’s shell.” A shell is just an interface to your operating system.<br />

There are GUI (graphical user interface) shells, such as the Finder (OS X) or Windows<br />

Explorer, where you simply click to launch an application. There are also comm<strong>and</strong>-line<br />

shells, which is the type of shell the <strong>Processing</strong> reference is referring to. To communicate<br />

with your system via a comm<strong>and</strong>-line shell, you type text-based comm<strong>and</strong>s in a terminal<br />

(emulator) program, such as the Terminal application in OS X. (On Windows, you can<br />

access the comm<strong>and</strong> shell by selecting the Run comm<strong>and</strong> in the Start menu <strong>and</strong> then

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

Saved successfully!

Ooh no, something went wrong!