Intermediate: Motherfucking Classes

Notice:

 

 

Before you continue, be sure to read Fucking Particles

or…

 

In the real world we have products,

but in order for us to get them we have to buy them.

If a friend of us sees a new watch we bought and wants one,

he’s going to have to buy a new one to have it.

If products were classes your friend could have a new watch

just by declaring a variable.

Let’s see an example.

Open Processing.

Copy, paste, read and run:

//This is how you create an object once you have a class.
myWatch myc;

myWatch myc2;

myWatch myc3;

//This is how you create an array of your class.

myWatch[] myArray;

void setup()
{
size(500,500);
background(0);
smooth();

//Here we initialize the object, giving as parameters x position, y position, size.
myc = new myWatch(10,20,50);
//By giving different parameters I can have a customized clone.
myc2 = new myWatch(40,120,50);
//The sky is the limit.
myc3 = new myWatch(110,220,50);

//Here you specify the length of the array.
myArray = new myWatch[10];

//You can even make arrays of your objects.
for(int i=0; i < myArray.length; i++)
{
//This loop initializes each of the objects.
//We place each one every 20 pixels hence the i*20
myArray[i] = new myWatch(i*20, 300,5);
}
}

void draw()
{
//We call the objec’s draw function inside the main draw function.
myc.make();
//You got to call all of your objects draw function.
myc2.make();
//All of them.
myc3.make();

for(int j=0; j < myArray.length; j++)
{
//This loop runs the make() “draw” function of the object,
//for each object of the array.
myArray[j].make();
}
}

//This is how we create a class.
//We write class, then the name of the Class and then {  }

class myWatch
{
//Here we can declare the variables we are going to use.
//In this case, this object is going to be a rectangle.

//These variables are the position and size of the rectangle.
int myx, myy, mys;

//This function as you see has the same name as the class
//the parameters that you put will have to match the ones
//you send when you create a myWatch
//It will look something like myC = new myWatch(10,15,50);

myWatch(int xposition, int yposition, int rSize)
{
//Here we take the values and put them in the object’s variables.
myx = xposition;
myy = yposition;
mys = rSize;
}

void make()
{
//Here is where we will draw, the equivalent of our main draw() function.
//Without stroke.
noStroke();
//Fill Whithe 255.
fill(255);
//Rectangle parameters: rect(x position,y position,width,height);
//Notice we multiply times 3 to create a rectangle.
rect(myx,myy,mys*3,mys);
}

}

 

You should get something like this:

 

Not very amazing at sight but mighty powerful in concept.

 

 

Me Gusta Processing

If you want to start playing with the reference:

http://www.processing.org/reference/


Flattr this

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s