how processing handles multipe source files

edited July 2017 in Using Processing

processing "compiles" the processing source code to java source code before running the sketch. here is how processing handles multiple source files:

  1. all files get concatenated together
  2. everything gets wrapped in a java class with a main method

in the end all your processing source code is in one giant java class with a main method.

note this guide covers only the common cases. some edge cases will not be mentioned.

a "picture" is worth a thousand words.

suppose you have the following files:

catmousegame/catmousegame.pde

void setup() {
  size(400, 400);
  initcat();
  initmouse();
}

void draw() {
  background(0);
  movecat();
  movemouse();
  if (catonmouse()) {
    cateatmouse();
    newmouse();
  }
  drawcat();
  drawmouse();
}

void keyPressed() {
}

catmousegame/cat.pde

int catx;
int caty;
int mouseeaten;

void initcat() {
}

void movecat() {
}

void cateatmouse() {
}

void drawcat() {
}

catmousegame/mouse.pde

int mousex;
int mousey;

void initmouse() {
}

void newmouse() {
}

void movemouse() {
}

void drawmouse() {
}

catmousegame/game.pde

boolean catonmouse() {
  return false;
}

this will "compile" to the following java file:

import processing.core.*; 
// bunch of imports

public class catmousegame extends PApplet {

public void setup() {

  initcat();
  initmouse();
}

public void draw() {
  background(0);
  movecat();
  movemouse();
  if (catonmouse()) {
    cateatmouse();
    newmouse();
  }
  drawcat();
  drawmouse();
}

public void keyPressed() {
}

int catx;
int caty;
int mouseeaten;

public void initcat() {
}

public void movecat() {
}

public void cateatmouse() {
}

public void drawcat() {
}

public boolean catonmouse() {
  return false;
}

int mousex;
int mousey;

public void initmouse() {
}

public void newmouse() {
}

public void movemouse() {
}

public void drawmouse() {
}

  public void settings() {  size(400, 400); }
  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "catmousegame" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}

see here for how to see the generated java code: https://forum.processing.org/two/discussion/23400/how-to-get-the-generated-java-code-linux

things of note:

  • the "main" file (the one with the same name as the sketch folder) will be first. all other files will be sorted alphabetically.
  • all code ends up in the same class. that means all functions and variables in all files must have unique names.
  • the name of the sketch becomes the name of the class.
  • a bunch of imports and a main method is added. this is now a complete and runnable java file.

the following points are mostly true. they are not absolutely true. but if you don't follow them then obscure errors might occur. in other words: only deviate from these if you know what you are doing.

more notes:


i posted this because i wanted to know how processing handles multiple files. i searched around the docs and forum and only got vague hints. i compiled this information and hope it will help future hackers trying to learn about this.

here are some of the vague hints:

Sign In or Register to comment.