Processing: Object Orientated Programming

This week we learned about object-orientated programming. Java, the language on which Processing is based is an object orientated language, where bits of code can be assigned to an object which has certain properties.

The object of the lesson was to simulate Brownian motion, which is the motion of particles suspended in water. The particles “jiggle” in place due to the liquid molecules colliding with it. More information can be found here.

http://en.wikipedia.org/wiki/Brownian_motion

Here is the code not using objects. It is rather simple in that every frame a new location is calculated and a rectangle is drawn in that position.

float x, y;
float c;


void setup(){
  size(600, 600);
  x= 100;
  y = 200;
  c = color(22,222,22);
}

void draw(){
  background(222);
  
  
  //simulating Brownian motion simulation of particles in fluid
  //assigning x = x + random number between -1 and 1
   x += random(-1,1);
   y += random(-1, 1);
   
   rect(x, y, 10, 10);
}

Brownian motion is simulated by random movement each frame on both the x and y axis. Without an object orientated approach this code would have to be copied out several times to create more particles in the sketch.

Here is an object orientated approach. There are two tabs in the same sketch, one for the main bulk of the sketch, and one for the object.

//MAIN SKETCH

//global variables and instances of objects
ArrayList agentList;


void setup(){
  size(600, 600);
  
  // arrays are fixed, array lists are not
  agentList = new ArrayList();
  

}

void draw(){
  background(222);
  // go through agentList
  for (int i = 0; i < agentList.size(); i++ ){
    //get agent in list
    // (Agent) casts the item to the ogject Agent
   Agent tempAgent = (Agent)agentList.get(i);
   tempAgent.update();
   tempAgent.draw();
  }
}

//called everytime you click the mouse
void mousePressed(){
  Agent tempAgent;
  tempAgent = new Agent(mouseX, mouseY, color(random(255), random(255), random(255)));
  agentList.add(tempAgent);
  println( agentList.size() );
}

//CLASS SKETCH

class Agent{
  // classes usually contain 3 things
  // class data
  float x, y;
  color c;

  //constructor
  // has to have no return type and same name as object
  Agent(int x, int y, color c){
    //denotes it is a different x to the one already defined
    //always specific to a object reffering to class when already in a class
    this.x = x;
    this.y = y;
    this.c = c;
  }
  
  //class methods
  void update(){
   //simulating Brownian motion simulation of particles in fluid
  //assigning x = x + random number between -1 and 1
   x += random(-1,1);
   y += random(-1, 1);
  }
  
  void draw(){
    fill(c);
    rect(x, y, 10, 10);
  }


  
  
}// end encapsulation of class


Click to create particles in the sketch

With this approach more functionality can be easily added to the object. It means the data can be more quickly replicated and added to a list which can be called when an event happens. Here when the user clicks the mouse the particles are added to the screen. We can also dynamically change the values, here we change the colour every time the mouse is pressed.

In an class object there are usually 3 types of data that need to be stored.

Class Data which is usually things like variables needed for the class.
Agent which has no data type and is the same name as the object. This specifies how the data is going to be used. Here we have two integers, x and y positions and the colours of the object.
Class Methods which is the actual functionality of the class. Here we have two methods, one which updates the position of the class and one which draws it in a new position.

The class can then be called in the main body of the sketch by initializing it at the top then calling it when needed. Here we used an array to add a new agent each time the mouse is pressed. A new agent is made only when the mouse is pressed down as shown in the function void MousePressed. Different variables can also be passed to the object as it is called. As we can see in the line Agent(mouseX, mouseY, color(random(255), random(255), random(255))); the agent takes the arguments of the mouse position, in order to provide the initial location of the object, and the colour is provided randomly each time a particle is made.

Using these techniques will be very helpful in developing my future programs as it allows more complex mechanisms to be added easily to elements in a sketch without the code becoming too complicated to read. I will continue to study more ways to add functionality to objects and will probably be using them in my final piece.

Wikipedia, 2014. Brownian Motion Available from: http://en.wikipedia.org/wiki/Brownian_motion [Accessed 05.11.2014]

Leave a Comment