06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

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.

Java 425<br />

instead of <strong>Processing</strong> , specifying all libraries would always be required. <strong>Processing</strong> , however, assumes a<br />

base set of libraries from Java (e.g., java.applet.*) and from <strong>Processing</strong> (e.g., processing.core.* ) which is<br />

why we do not see these in every sketch.<br />

• public —In Java, variables, functions, and classes can be “ public ” or “ private. ” This designation<br />

indicates what level of access should be granted <strong>to</strong> a particular piece of code. It is not something<br />

we have <strong>to</strong> worry much about in the simpler <strong>Processing</strong> environment, but it becomes an important<br />

consideration when moving on <strong>to</strong> larger Java programs. As an individual programmer, you are<br />

most often granting or denying access <strong>to</strong> yourself, as a means for protecting against errors. We<br />

encountered some examples of this in Chapter 22’s discussion about encapsulation.<br />

• class JavaExample —Sound somewhat familiar? Java, it turns out, is a true object-oriented language.<br />

There is nothing written in Java that is not part of a class! We are used <strong>to</strong> the idea of the Zoog class,<br />

Car class, PImage class, and so on, but it is important <strong>to</strong> note that the sketch as a whole is a class,<br />

<strong>to</strong>o! <strong>Processing</strong> fills this stuff in for us so we do not have <strong>to</strong> worry about classes when we are first<br />

learning <strong>to</strong> program.<br />

• extends PApplet —Well, after reading Chapter 22, we should be quite comfortable with what this<br />

means. This is just another example of inheritance. Here, the class JavaExample is a child of the class<br />

PApplet (or, equivalently, PApplet is the parent of JavaExample). PApplet is a class developed by<br />

the crea<strong>to</strong>rs of <strong>Processing</strong> and by extending it, our sketch has access <strong>to</strong> all of the <strong>Processing</strong> goodies—<br />

setup( ), draw( ) , mouseX , mouseY , and so on. This little bit of code is the secret behind how almost<br />

everything works in a <strong>Processing</strong> sketch.<br />

<strong>Processing</strong> has served us so well because it eliminates the need <strong>to</strong> worry about the above four elements, all<br />

the while providing access <strong>to</strong> the benefi ts of the Java programming language. Th e rest of this chapter will<br />

show how we can begin <strong>to</strong> make use of access <strong>to</strong> the full Java API. (We briefl y began this journey when<br />

we worked with String parsing in Chapters 17 and 18.)<br />

23.3<br />

Exploring the Java API<br />

Th e <strong>Processing</strong> reference quickly became our best friend forever while learning <strong>to</strong> program. Th e Java API<br />

will start off more as an acquaintance we bump in<strong>to</strong> from time <strong>to</strong> time. Th at acquaintance might turn in<strong>to</strong><br />

a really excellent friend someday, but for now, small doses will be just fi ne.<br />

We can explore the full Java documentation by visiting:<br />

http://java.sun.com/<br />

Th ere, we can click over <strong>to</strong> API specifi cations:<br />

http://java.sun.com/reference/api/index.html<br />

and fi nd a selection of versions of Java. On a Mac, <strong>Processing</strong> will run with the selected version of Java<br />

(run Java Preferences, found, e.g., in: /Applications/Utilities/Java/J2SE 5.0/). On a PC, the <strong>Processing</strong><br />

“ standard ” download comes with Java version 1.4.2 (the Windows expert version allows you <strong>to</strong> install<br />

your own version of Java). Versions of Java will change in the future with updated info at processing.org.<br />

In any case, while there are diff erences between the version of Java, for what we are doing, they will not<br />

be terribly relevant, and we can look at the API for J2SE 1.4.2 for pretty much whatever we want <strong>to</strong> do.<br />

See Figure 23.1 .

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

Saved successfully!

Ooh no, something went wrong!