Reference

This page will be full of code examples and external resources for when you're programming and forget how to do something

If statements

If statements should be used when your program needs to make decisions.

This is a basic if statement.
int x = 0;
      if (x > 100) {
        print("X is big!");
      } else {
        print("X is small");
      }
Check if the variable input is equal to the variable passcode.
        int passcode = 651;
        int input = 123;
        if (input == passcode) {
          print("You typed the correct password");
        }
      
Draw a background if the mouse is on the left half of the screen.
        if (mouseX < screenWidth / 2) {
          background(255, 0, 0);
        }
      

While loops

Use these when something needs to happen over and over.

This prints every number between 0 and 100.
        int x = 0;
        while (x < 100) {
          print(x);
          x += 1;
        }
      
Draws circles accross the screen every 50 pixels.
        int positionX = 0;
        while (positionX < screenWidth) {
          ellipse(positionX, 500, 20, 20)
          positionX += 50;
        }
      
Sets total equal to 100 letter a's
        String total = "";
        int number = 0;
        while (number < 100) {
          total += "a";
          number += 1;
        }
      

Functions

Functions are commands. There are many built into Processing, but you will also need
to know how to create your own.
To call a function means to use the function.

This is a function that will print "Yas" if x is greater than 50.
          void maybePrint(int x) {
            if (x > 50) {
              print("Yas");
            }
          }
        
This is how you can use that function.
          maybePrint(60);
        

This function will draw a circle to the screen at x, y.
          void drawCircle(int x, int y) {
            ellipse(x, y, 50, 50);
          }
        
Here's how you can call this function.
          drawCircle(100, 50);
        

This function will print out every number between 0 and x.
          void printNumbersUpTo(int x) {
            int y = 0;
            while (x < x) {
              print(y);
              y += 1;
            }
          }
        
This is how you call this function.
          printNumbersUpTo(75);
        

Arrays

Arrays are created in the following way :

            int[] firstArray = new int[] { 99, 2, 5 };
            int[] secondArray = new int[65];
          

The first array is created with three elements, 99 2 and 5.
The second array is created with 65 elements in it, and they are all set to 0.

Arrays can be used in the following way :
The following code will increase the first element by 2. Then next, it will print the first element.

            int[] ages = new int[] { 10, 12, 16 };
            ages[0] += 2;
            print(ages[0]);
          

Elements in an array are accessed by using their indexes :

            int[] array = new int[50];
            array[0] = 10;
            array[1] = 50;
            array[2] = 5;
            array[0] = array[0] + array[1] + array[2];
            print(array[0]);
          

We can create an array of anything, here's some examples :

            String[] array = new String[10];
            array[0] = "Moose";
            array[1] = " is an awesome snake";
            array[2] = array[0] + array[1];
            print(array[2]);
          

Here's how you print everything in an array :

            int[] numbers = new int[] { 999, 888, 777, 1, 2, 3 };

            int i = 0;
            while (i < numbers.length) {
              print(numbers[i]);
              i += 1;
            }
          

Lists

This is how you create an ArrayList :

            // Create a list of Strings
            ArrayList<String> list = new ArrayList<String>();

            // Create a list of integers
            ArrayList<Integer> list = new ArrayList<Integer>();

            // List of floats
            ArrayList<Float> list = new ArrayList<Float>();
          

This is how you add to an ArrayList :

            ArrayList<String> list = new ArrayList<String>();
            list.add("Hi");
          

This is how you remove from an ArrayList :

            ArrayList<String> list = new ArrayList<String>();
            list.remove("Hi");
          

This is how get stuff from an ArrayList :

            ArrayList<String> list = new ArrayList<String>();
            list.get(0);
          


HashMap
        HashMap<String, Integer> map = new HashMap<String, Integer>();

        map.put("Jon", 21);
        map.put("Gary", 14);
        map.put("Andrew", 19);

        print(map.get("Jon"));
        

Program Examples
This program allows you to control the dot with keys.
                    int x = 100, y = 100;
          int speed = 3;

          void setup() {
            size(800, 600);
          }

          void draw() {
            background(255, 255, 255);

            fill(255, 0, 0);

            updatePosition();

            ellipse(x, y, 10, 10);
          }

          void updatePosition() {
              if (keyPressed) {
                if (key == 'w') {
                  y -= speed;
                }
                if (key == 's') {
                  y += speed;
                }
                if (key == 'a') {
                  x -= speed;
                }
                if (key == 'd') {
                  x += speed;
                }
              }
          }
        

This is the rain program

          int[] raindrops = new int[50];
          float[] speed = new float[50];

          void setup() {
            size(500, 500);

            int i = 0;
            while (i < raindrops.length) {
              speed[i] = 1;
              raindrops[i] = (int) random(-height, 0);
              i += 1;
            }
          }

          void draw() {
            background(255, 255, 255);

            fill(0, 0, 255);

            int i = 0;
            while (i < raindrops.length) {
              ellipse(width * ((1.0 * i) / raindrops.length), raindrops[i], 10, 10);
              raindrops[i] += speed[i];
              speed[i] *= 1.03;

              if (raindrops[i] > 500) {
                raindrops[i] = (int) random(-height, 0);
                speed[i] = 1;
              }
              i += 1;
            }

          }