We are about to switch to a new forum software. Until then we have removed the registration on this forum.
processing "compiles" the processing source code to java source code before running the sketch. here is how processing handles multiple source files:
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 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.
setup()
should be in the "main" file. i recommend to also put draw()
and event handlers like keyPressed()
in the main file.size()
must be the first line in setup()
. alternatively define settings()
. see https://processing.org/reference/settings_.html.main()
. see https://github.com/processing/processing/wiki/Export-Info-and-Tips. quote: It is important that you don't have a method named main()
in your sketch, because it will fool the preprocessor into thinking you have a clue, when in fact you don't.more notes:
.java
files among the .pde
files. i have not really tried this and do not know how it behaves.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: