Top 10 Processing Features Not Covered

Processing has many useful features that we did not cover in this course. So in this note we present the 10 of the most useful, or at least interesting, features of Processing (and Java) that we didn’t cover in this course.

Arrays

An array is similar to an ArrayList, but without the ability to change it’s size. For example:

float[] tempReadings = { 6.5, 5.1. 5.2, 3.6, 3.4, 2.07};

tempReadings is an array of type float, and it contains 6 elements. You use []-backet notation to read and write individual elements of an array, e.g.:

println(tempReadings[0]);  // prints 6.5
println(tempReadings[1]);  // prints 5.1
println(tempReadings[2]);  // prints 5.2
println(tempReadings[3]);  // prints 3.6

tempReadings[0] = 4.5; // sets location 0 to 4.5

As you can see, the []-bracket notations is more compact than the set/get notation an ArrayList uses.

The major limitation with arrays is that they can’t easily change their size. There’s no way to add a new element to tempReadings without copying the current elements into a new array that is longer.

Traditional For-loops

We’ve used two kinds of loops in this course: for-each loops, and while loops. A traditional for-loop (sometimes called a C-style loop) is, essentially, like a while-loop except all the loop control information is at the top of the loop. For example, this for-loop prints the numbers from 1 to 1000:

for(int i = 0; i < 1000; ++i) {
    println(i + 1);
}

It does the same thing as this while loop:

int i = 0;
while (i < 1000) {
    println(i + 1);
    i += 1;
}

The for-loop version is more compact, and, at least once you get used to it, easier to read because all the loop control information is one one place (as compared to the while-loop, where its at the beginning an end of the loop).

Exceptions

Processing can deal with errors using special error objects called exceptions. Using a special language construct called a try/catch block, you can, if you like, “catch” exceptions when they occur and, perhaps, try to recover from them, or, at least, have the program fail in a “graceful” way.

Error-handling was hardly mentioned in this course. However, it is a major topic in real-world programming, where it is important that programs work sensibly even in cases where things go wrong.

Files and the Network

Reading/writing and sending/receiving information over networks are among the most useful and interesting things that computers can do. Processing has a large number of functions for reading and writing different kinds of files, and for sending and receiving information over networks that your computer might be attached to.

Interfaces

Interface are, essentially, a kind of class with the unusual feature that none of the functions defined with in it have a body: they just have headers.

For example, in animation it is often useful to write an interface such as this:

interface Animatable {
    void render();
    void update();
}

Other classes can no implement this interface, which means they must provide implementations of the functions in the interface. For example, here are two different classes that implement Animatable:

class Box implements Animatable {
    void render() {
        // ... code to draw the box ...
    }
    void update() {
        // ... code to update the box ...
    }
}

class Ball implements Animatable {
    void render() {
        // ... code to draw the ball ...
    }
    void update() {
        // ... code to update the ball ...
    }
}

This lets us write code like this:

ArrayList<Animatable> things = new ArrayList<Animatable>();
things.add(new Box());
things.add(new Ball());

for(Animatable a : things) {
    a.update();
    a.render();
}

Any object that implements Animatable can be added to things.

Other Object-Oriented Programming Features

There are a number of object-oriented programming features that we didn’t discuss in this course, such as:

  • Private. A class label functions and variables as being private, which means they can only be used by other functions in the class. This supports information hiding, which is an important design principle in object- oriented programming.
  • Static. Variables and functions in a class can be declared static, which means they are not part of objects for the class, but instead are part of the class itself. That means static variables/functions have only one instance no matter how many objects of the class you create.

Generics

We’ve seen that you can pass values to functions, and generics are, essentially, technique that let you pass types to classes. For example, ArrayList is implemented using generics. It’s what lets us write ArrayList<Sprite>, or ArrayList<String).

Generics are generally easy to use (as we’ve seen with ArrayList), but they are surprisingly tricky to use correctly when you create your own generic classes.

Other Containers

ArrayList and arrays are perhaps the two most common ways of storing data in Processing programs. But there are many other choices, such as: maps, lists, sets

  • Lists, which let you efficiently insert and delete items.
  • Sets, which store values without allowing any duplicates.
  • Maps, which store (key, value) pairs and can be efficiently accessed by keys.

GUIs and 3D graphics

If you want to create a professional looking interface your program, using buttons, sliders, windows, menus, and so on, there are standard Java libraries you can call through Processing to do that. However, if you need a fancy graphical user interface it’s probably best to move to Java instead sticking to Processing. It’s not much of a jump given how similar Processing is to Java.

Processing also has many functions for 3D graphics. You can find many examples of how to use them on the Processing reference page.

Sound

Sound is an interesting topic. It seems relatively simple, but it turns out to be surprisingly complex. Sound is not supported directly by Processing, but you can use the Minim library which provides many useful functions for playing, recording, and modifying sound.