Object Based Programming in Processing


// Declare and construct two objects (h1, h2) from the class HLine

HLine h1 = new HLine(20, 2.0);
HLine h2 = new HLine(50, 2.5);

void setup() {
size(200, 200); frameRate(30);
}

void draw() {
background(204);
h1.update();
h2.update();
}

class HLine {
float ypos, speed;
HLine (float y, float s) {
ypos = y;
speed = s;
}

void update() { ypos += speed; if (ypos > height) { ypos = 0;
}
line(0, ypos, width, ypos);
}

Description: Keyword used to indicate the declaration of a class. A class is a composite of fields (data) and methods (functions that are a part of the class) which may be instantiated as objects. The first letter of a class name is usually uppercase to separate it from other kinds of variables. A related tutorial on Object-Oriented Programming is hosted on the Oracle website.

Syntax

class ClassName {
  statements
}

Instances of the objects have to be individually defined, with the objects operating as a collection of functions wrapped up in the structure class. Other examples have shown me that an Array can be used to create, store and manage large numbers of instances of the same object.

The objects rely on parameters that tie back into the  statements in the class to function. This has been consistent in the other examples I have seen containing objects, and when I tried to write code without it, a Null value error was returned.

While not present in all of the user examples I have seen, the .update() function seems like an efficient way of managing the interactivity and motion, allowing me to keep everything in the class.

The class gets called like a function, parameters included, in a constructor that has all of the data associated with the object stored within it.

As mentioned above, I like the idea of using an update function to manage the objects over time. In this example, the function used to display the objects has been incorporated into the update function.

After trying my hand at creating an object based composition with processing, depending on how hard it is, review of the Oracle lesson may be necessary.
 


Take Away

Before, I was thinking about using color and/or shape to visualize the audio, but I think the more appropriate route will be to create a set of objects with responsive rather than generated behaviors and have them either be continually created or continually updated in response to the audio input.