Modularity

Built-In Methods

These methods get called automatically (they are callbacks) by Processing. You override them and implement them yourself.

mouseDragged(): Called when the screen is pressed and finger moved. Will happen repeatedly as the finger is dragged on the screen.

mousePressed(): Gets called when the screen is pressed.

mouseReleased(): Gets called when the screen which has been pressed is released.

keyPressed(): Gets called when a key is pressed. May happen multiple times in a row with one key press.

keyReleased(): Gets called when a key is released.

Test it out:

void setup()
{
}

void draw()
{
}

void mouseDragged() 
{
  println("mouseDragged");
}

void mousePressed() 
{
  println("mousePressed");
}

void mouseMoved() {
  // Doesn't work, no mouse moved in Processing for Android
  println("mouseMoved");
}

void mouseReleased() {
  println("mouseReleased");
}

void keyPressed() {
  println("keyPressed");
}

void keyReleased() {
  println("keyReleased");
}

Mouse Released Example:

void mouseReleased()
{
  fill(255, 127, 0);
  ellipse(mouseX, mouseY, 10, 10);
}

Drawing Example:

int x, y;

void setup()
{
  size(500, 500);
  background(255);
}

void draw()
{
  // Empty!
}

void mouseDragged() 
{
  line(x, y, mouseX, mouseY);
  x = mouseX;
  y = mouseY;
  println("mouseDragged");
}

void mousePressed() 
{
  x = mouseX;
  y = mouseY;
  println("mousePressed");
}

Creating our own methods

We can create our own methods that we can call elsewhere in your code. There are many advantages to writing methods rather than putting everything in the setup() or draw() methods.

Reuse: Writing a function for a commonly used set of commands that you can call over and over again. For instance, you have a complex shape that you use throughout your code. Rather than cutting and pasting those instructions everywhere you can write them once and call it over and over again.
Encapsulation: Putting functionality in a method and thereby seperating it from the rest of the code helps in understanding the program. It also makes making changes to that code much easier.

Methods comprise of the following parts:
Return Type: This is the type of data (or variable) that the method returns. So far we have only seen the built-in methods which return nothing and therefore use the type: void. Any of the other data types are acceptable as well.
Name: This is simply the name of the method. You will call or execute the method using this. An example is setup.
Arguments: You can pass data into a method through the use of arguments. These are specified as variables of a certain type (your choosing) that the method can then make use of. These variables are local to the method.
Code Block: These are the instructions or code that the method will execute when called.

Here is an example:

// return type, function name, arguments, including types
int ourCoolMethod(int someInt, int someOtherInt)
{
  // Declare a variable
  int aThridInt;
  
  // Do something valuable
  aThridInt = someInt * someOtherInt;
  aThridInt++;
  
  // Return something (an integeter in our case as it must match the return type)
  return aThridInt;
}

We would run this method somewhere else in our code such as:

void setup() {        
  int myResult = ourCoolMethod(10, 2);
  print(myResult);

  int a = 10;
  int b = 3;
  int c = ourCoolMethod(a, b);
  print(c);
}

// return type, function name, arguments, including types
int ourCoolMethod(int someInt, int someOtherInt)
{
  // Declare a variable
  int aThridInt;
  
  // Do something valuable
  aThridInt = someInt * someOtherInt;
  aThridInt++;
  
  // Return something (an integeter in our case as it must match the return type)
  return aThridInt;
}

Crazy Example:

int acounter = 0;

void setup()
{
  size(400,400);
  background(255);
}

void draw()
{
  fill(acounter%255);
  if (acounter%2 == 0)
  {
    drawLessCrazyShape(acounter%width,acounter%height,60);
  }
  else
  {
    drawCrazyShape(width-(acounter%width),height-(acounter%height));
  }
  acounter++;
}

void mousePressed()
{
    fill(mouseX,0,mouseY);
    drawCrazyShape(mouseX, mouseY);
}

void mouseDragged()
{
   fill(255,0,0);
   drawLessCrazyShape(mouseX,mouseY,50);
}

void drawCrazyShape(int x, int y)
{
  beginShape(POLYGON); 
    vertex(x, y); 
    x = x/(y+1)*2;
    vertex(x, y);
    y++;
    x--; 
    vertex(x, y); 
    y = x/(y+1)*2;
    vertex(x, y);
    x -= 30; 
    vertex(x, y); 
    vertex(y*2, x); 
  endShape();
}

void drawLessCrazyShape(int one, int two, int size)
{
  beginShape(POLYGON); 
    
    vertex(one, two); 
    
    one -= size;
    two += size;
    vertex(one, two);
    
    one -= size;
    two -= size;
    vertex(one, two); 

    one += size;
    two -= size;
    vertex(one, two);

    one -= size;
    two += size;
    vertex(one, two); 
    
  endShape();
}