Java and therefore Processing is an Object Orientated Language. This means that Java makes it easy to use objects in your code.
An Object is a datatype just like a variable with a couple of differences. Objects can hold multiple pieces of data and can hold methods to manipulate that data.
A Class is the blueprint for an Object. It is the code that represents the data and methods that are contained within.
I like to think of the following example when thinking about Classes and Objects.
We are all people, human people. That is a class. The class states that we have hair, eyes, ears, are capabile of walking and so on.
Our human class, would contain variables that hold the color of our eyes, hair, how fast we walk and so on. It would also contain methods for walking, talking, eating and everything else that we do.
In order to create a person (object), we would use the human (class) as a blueprint and fill in the values particular to the person we are creating.
In (some form of) English:
Define Human Class Human Has A Hair Color, Eye Color and Shoe Size. Human Can Walk and Run. Create New Human This Human has Green Hair, Orange Eyes and wears a size 10 shoe.
In Processing we would open a new Tab and type the following:
// Give our class a name and tell Processing it is a class.
class Human
{
// Variables for our individual humans.
int hairColor; // Shade of gray for simplicity sake
int eyeColor;
int shoeSize;
// Constructor, what we use to create a NEW human object: a person
// A special type of method.
Human(int theHairColor, int theEyeColor, int theShoeSize)
{
hairColor = theHairColor;
eyeColor = theEyeColor;
shoeSize = theShoeSize;
}
// our walk function for humans, returns distance walked, takes in number of steps
int walk(int numSteps)
{
int distance = numSteps * shoeSize;
return distance;
}
// our run funtion for humans, returns distance walked, takes in number of steps
int run(int numSteps)
{
int distance = walk(numSteps) * 2;
return distance;
}
}
In order to create a human in our main program we would do the following:
You will notice that you call methods of the class/object using “.” method name on the actual object.
Human shawn; // Declare the variable shawn to be an object of type Human
void setup()
{
shawn = new Human(30,129,12); // Create the new human, run the constructor
int distanceWalked = shawn.walk(10); // Have shawn walk, get the distance walked
println("Shawn Walked: " + distanceWalked);
int distanceRan = shawn.run(10); // Have shawn run
println("Shawn Walked: " + distanceRan);
}
Using Objects makes it very easy to create multiple versions of the same thing, each with it’s own variables.
We can add a new Human called dan to illustrate:
Human shawn; // Declare the variable shawn to be an object of type Human
Human dan;
void setup()
{
shawn = new Human(30,129,12); // Create the new human, run the constructor
dan = new Human(80,90,9);
int distanceWalked = shawn.walk(10); // Have shawn walk, get the distance walked
int dDistanceWalked = dan.walk(10);
println("Shawn Walked: " + distanceWalked + " and Dan walked: " + dDistanceWalked);
int distanceRan = shawn.run(10); // Have shawn run
println("Shawn Walked: " + distanceRan);
}
Let’s go through another example:
// The name of our class
class Ball
{
// our Class variables.
int wWidth;
int wHeight;
int bSize;
int x, y;
int xDirection, yDirection;
// Constructor. This gets called when we create a "new Ball"
Ball(int windowWidth, int windowHeight, int ballSize)
{
wWidth = windowWidth;
wHeight = windowHeight;
bSize = ballSize;
x = 1;
y = 1;
xDirection = 1;
yDirection = 1;
}
// This is an Overloaded constructor
Ball(int windowWidth, int windowHeight, int ballSize, int xPosition, int yPosition)
{
this(windowWidth, windowHeight, ballSize);
x = xPosition;
y = yPosition;
}
// Our method to determine current position of the ball.
void compute()
{
if (x < wWidth && x > 0)
{
// Move along x axis
x += xDirection;
}
else
{
// Change direction, from positive to negative and vice versa
xDirection = xDirection * -1;
x += xDirection;
}
if (y < wHeight && y > 0)
{
y += yDirection;
}
else
{
yDirection = yDirection * -1;
y += yDirection;
}
}
// This actually displays our ball, it gets called in the main draw function
void display()
{
compute(); // first we run the computation
ellipseMode(CENTER);
ellipse(x,y,bSize,bSize);
}
}
Here is what would be in the main Processing program:
Ball aBall, anotherBall;
void setup()
{
size(500,500);
aBall = new Ball(width,height,100); // Call the constructor of Ball to create a new Ball
anotherBall = new Ball(width,height,5,100,2); // Same..
}
void draw()
{
background(255);
aBall.display(); // Call the display function/method on the aBall object.
anotherBall.display(); // Call it on the anotherBall object.
}