Course Specifics:

Syllabus

Assignments

Course Notes

Course Books

readings

 

Processing LInks:

processing.org

openprocessing.org

learningprocessing.com

 

Art / Design / Code:

CreativeApplications.net

CreateDigitalMotion.com

Marius Watz

Generator X

Code & Form

Casey Reas

Ben Fry

Golan Levin

Dan Shiffman

Jared Tarbell

PostSpectacular

Flight 404

Robert Hodgin

 

 

Course Notes 3 - Arrays, Objects and Classes

Click for stuff.

Your browser does not support the canvas tag.

Source code: class_example & particle Built with Processing

 

* Relavant course book pages: 395-420 & 301-314

Arrays:

Generally Speaking, an array is a type of variable that can store more than one piece of informaiton. In a sense, and array is a list of similar things stored under a common name. For instance, you could say that the decade of the 1980's is an array of years from 1980 - 1989. In this case, you would have an array name, such as 'eighties', which itself has ten slots, each numbered slot contains one year of the decade. The array 'eighties' would be created and filled like this:

 

int[ ] eighties = new int[10];

 

Here you see some immediate differences between creating, or declaring, an array and declaring a standard variable of type int, float, or char.

[ ] - the square brackets: these brackets are integral to arrays. First off, they tell processing that, in this case, instead of creating a single integer variable, we are creating an array of type integer.

 

Next we declare the variable name, eighties, and tell processing that it is a new array, and that the array length is 10; meaning this particular array can store 10 integers in one easy-to-acces spot.

 

Setting and Accessing array items:

 

Ok, we've created an array for the eighties, but it does not yet contain any information. To fill the slots of the array, we do something like this:

 

eighties[0] = 1980;

eighties[1] = 1981;

eighties[2] = 1982;

... and so on.

 

Notice that the first slot of the array is [0] and not [1]. All array numbering systems start at slot zero [0].

 

Take some time to read the relevant pages in the course book, and look at the Array reference page on processing.org. Arrays are incredibly useful tools in creating and storing data, and objects, which we will discuss shortly.

 

Classes and Objects:

Now we arrive at 'Object-Oriented Programming.' The idea of object-oriented programming is not new, but it is the heart of what makes Processing a powerful tool to control and manipulate multitudes of agents acting upon the screen.

 

The Processing coursebook does a thorough job explaining Classes and the concepts of of object-oriented programming (starting p. 395). In many ways Classes are easy to understand in the abstract: think of a class as synonomous to an 'Object.' The book uses the analogy of an apple: An apple has several traits, and runs several functions (grow, die, decay, etc), however i find it helpful to think of classes as analogous to simple living organisms. The reason for this is that I find it helpful to think of Classes as objects which are at once visual and behavioral. Certainly, we could make a class that draws ellipses on the screen of various sizes and colors, however our previous work with iteration can do that just as easily. What Classes bring to the table is the ability to create objects that can contain traits which are far more complex than simple appearance. Classes can be built to react to, and navigate in, their evnironment is intricate ways, they can be made to exhibit what could be classified as 'behaviors.' Classes can look at what's going on around them, and make decisions based on this information. For instance, you may want to create hundreds of objects which are at once 'frightened' of the use (the mouse), and are also 'attracted' to eachother.

 

So a class, or object, could be written that is generally free to move around the screen at will, but if it encounters the mouse, it will skitter off in some random direction. Once the class is written once, it can be re-used in the sketch as many times as needed. All of a sudden, you have hundreds of objects which are attracted to eachother, but are repelled by the mouse. Suddenly, you have an ecosystem of objects reacting to eachother and their environment.

 

Ok, away from the abstract now. Again, classes are clearly laid out in the text, but some things to keep in mind:

 

Classes must:

Classes usually:

Fields: If you were simulating leaves falling from a tree, you would need to keep track of several things. You would want to know its x and y position at minimum. Inside the class, you could create variables x and y. These variables then become fields of the class. So if at any time you wanted to know the x position of a leave, you could simply ask processing to tell you what the value of leaf.x is, and it would access the object 'leaf' and tell you the value of the variable x inside of that object.