title
Joe McKay
This section comprises of code snippets that I use to teach and make my own work. They are complicated but useful bits of code that I constantly refer to, so why not put them up for everyone?

Custom Routines

You can use a routine to return values and do some math for you. This is handy. The first routing, Joe, returns an integer, the second routine returns a boolean true / false value. You can send all the variables you want to the routine, just remember to initialize them properly.

  
	color colorx = color(255, 20, 100); 
	void setup() {
	  size(400, 400);
	  smooth();
	}

	void draw() {
	  background(colorx); 
	  int num = joe(15, 3, 3); 
	  int num2 = joe(30, 7, 2);
	  if (joe2(200)) { 
	    colorx = color(random(255), random(255), random(255));
	  }  
	   if (joe2(10)) { 
	    fill(random(255), 100, random(255));
	  }
	 ellipse(num, num, 20, 20); 
	  ellipse(num2, num2, 100, 100); 
	  println(num);
	}

	int joe(int a, int b, int c) { 
	  int test = a * b + c;  
	  return (test);
	}

	boolean joe2(int incoming) { 
	  if (int(random(incoming)) == 1) {
	    return true;
	  }
	  else {
	    return false;
	  }
	}                        
	

Your browser does not support the canvas tag.

Classes
A Class (processing for object orient programming) allows you to control several instances of an object without having to manually keep track of every variable.
The following sketch uses an ArrayList - this special list is keeps track of how many instances of the object there are. Each object then manages their own variables. Yes, it's hard to get your head around, but when you do it gives you another level of control. ArrayLists are nice because you do not have to define their length when you create them, however, the syntax is slightly more complicated at first.

Often classes will be on their own tab for organizational purposes.

ArrayList ballslist; // a list to contain the instances of the class. 
void setup() {
  size(600, 400);
  smooth();
  ballslist = new ArrayList();
  newball();
}

void newball() {
  for (int i = 0; i < 20; i ++) {
    int ballwidth = 20; 
    color bcolor = color(100, 120, 160, 200); 
    float speedx = (random(-4, 4)); 
    float speedy = (random(-4, 4)); 
    float ballx = width/2; 
    float bally = height/2; 
    // call the BallClass and creates a new ball. Passes variables to the class.
    ballslist.add(new BallClass(ballx, bally, ballwidth, bcolor, speedx, speedy));
  }
}


void draw() {
  background(255);
  smooth(); 
  if (ballslist.size() <= 5) {
    newball();
  } 
  // loops through every ball created
  for (int i = ballslist.size()-1; i > = 0; i--) { 
    // gets the stats on the current ball and names this intsance "dball".
    BallClass dball = (BallClass) ballslist.get(i); 
    // calls the display section of BallClass below and puts the ball on the screen. 
    dball.moveballs();
    dball.display();
    if (dball.finished()) { 
      ballslist.remove(i);
    }
  }
}


class BallClass {
  float x, y, speedx, Balpha, speedy; // declare the variables used in the class
  color ballColor; 
  int BallWidth, wallcounter;
  // the variables in the line below must be the same type as the vaiables being sent to the class. 
  BallClass(float tempX, float tempY, int tempballwidth, int tempcolor, float tempspeedx, float tempspeedy) { 
    x = tempX; // assign incoming temporary values to the variables you set above. 
    y = tempY;
    BallWidth = tempballwidth;
    ballColor = tempcolor;
    speedx = tempspeedx; 
    speedy = tempspeedy;
    wallcounter = 0;
  }

  boolean finished() {
    if (wallcounter == 10) {
      return true;
    }
    else {
      return false;
    }
  }

  void moveballs() {
    x += speedx; 
    y += speedy; 
    if (x > width || x < 0) {
      speedx *= -1;
      wallcounter ++;
    }
    if (y > height || y < 0) {
      speedy *= -1;
      wallcounter ++;
    }
  }

  void display() {
    stroke(1); 
    fill(ballColor); 
    ellipse(x, y, BallWidth, BallWidth);
  }
}


	

Buttons are a great case for using classes. Here's a simple button class to augment.

ArrayList buttonslist; // a list to contain the instances of the class. 
boolean mouseup = false; 
void setup() {
  size(420, 420);
  smooth();
  noStroke();
  buttonslist = new ArrayList();
  setupbuttons();
}

void draw() {
  background(255);
  smooth(); 
  for (int i = buttonslist.size()-1; i >= 0; i--) { // loops through every button created
    ButtonClass dbutton = (ButtonClass) buttonslist.get(i); // gets the stats on the current button and names this intsance "dbutton". 
    dbutton.display(); // calls the display section of ButtonClass below and puts the button on the screen. 
    // dbutton.checkclick(); // calls the display section of ButtonClass below and puts the button on the screen.
  }
}

void setupbuttons() {
  for (int i = 70; i < width - 60; i += 50) {
    int buttonred = 200; // I'm using mouseX and mouseY just to change the colours a bit for the demo. 
    int buttongreen = 100; 
    int  buttonblue = 100; 
    float buttonalpha = 256; 
    int buttonWidth = 30;
    int buttonHeight = 30; 
    boolean clicked = false; 
    int buttonX = i; 
    int buttonY = 200; 
    // call the ButtonClass and creates a new button. Passes variables to the class.
    buttonslist.add(new ButtonClass(buttonX, buttonY, buttonWidth, buttonHeight, buttonred, buttongreen, buttonblue, buttonalpha, clicked));
  }
}


void mouseReleased() {
  for (int i = buttonslist.size()-1; i >= 0; i--) { // loops through every button created
    ButtonClass dbutton = (ButtonClass) buttonslist.get(i); // gets the stats on the current button and names this intsance "dbutton". 
    if (
    mouseX > dbutton.x &&
      mouseX < dbutton.x + dbutton.buttonWidth &&
      mouseY > dbutton.yloc &&
      mouseY < dbutton.yloc + dbutton.buttonHeight
      ) {

      if (dbutton.clicked == true) {
        //       
        dbutton.clicked = false;
        // dbutton.y += dbutton.buttonHeight/2;
      }
      else {
        dbutton.clicked = true;
        //dbutton.y -= dbutton.buttonHeight/2;
      }
    }
  }
}



class ButtonClass {
  float x, y, yloc, Balpha; // declare the variables used in the class
  color buttonColor; 
  int Bred, Bgreen, Bblue;
  int buttonWidth, buttonHeight;
  boolean clicked; 

  // the variables in the line below must be the same type as the vaiables being sent to the class. 
  ButtonClass(float tempX, float tempY, int tempbuttonWidth, int tempbuttonHeight, int tempR, int tempG, int tempB, float tempA, boolean tempclicked) { 
    x = tempX; // assign incoming temporary values to the variables you set above. 
    y = tempY;
    Bred = tempR;
    Bgreen = tempG;
    Bblue = tempB;
    Balpha = tempA;
    buttonWidth = tempbuttonWidth; 
    buttonHeight = tempbuttonHeight; 
    clicked = tempclicked;
    yloc = y;
  }

  void display() {
    if (clicked == true) {
      Bred = 200; 
      buttonHeight = 80;
      yloc = y - buttonHeight;
    }
    else {
      Bred = 0;  
      buttonHeight = 30;
      yloc = y - buttonHeight;
    }
    // float yLoc = y - buttonHeight/ 2; 
    buttonColor = color(Bred, Bgreen, Bblue, Balpha);
    fill(buttonColor); 
    rect(x, yloc, buttonWidth, buttonHeight); 
    fill(255, 255, 255);
  }
}