I don’t actually know if this will work for you, because I use Eclipse IDE and install Processing into it (Proclipsing). But here’s the code anyway. I created a new code from scratch which provides a neat little interface to show you how to make it break and what I know about why it breaks. It’s essentially a white screen with some text.
This requires a few things: Package processing, class Main, class Sub, class Sub2, and image img.png (could be anything as long as it’s small enough, anywhere from 3232 to 100100).
Here is class Main:
//This is project processing2, package processing, class Main and class Sub.
//Sorry for the weird package name, I couldn't change it.
package processing;
import processing.core.PApplet;
import processing.core.PImage;
import processing.core.PFont;
import java.util.ArrayList;
public class Main extends PApplet {
PApplet p;
PImage image;
PFont font;
//A new Sub is added to list each time you press SPACE.
//Only one Sub2 is ever added to list2, but I decided to keep it as an ArrayList
//to keep it consistent.
ArrayList<Sub> list = new ArrayList<Sub>();
ArrayList<Sub2> list2 = new ArrayList<Sub2>();
public static void main(String[] args) {
//starts the PApplet from Eclipse
PApplet.main("processing.Main");
}
public void settings() {
size(1500, 800);
}
public void setup() {
//font stuff
font = createFont("Calibri", 30);
textFont(font);
list2.add(new Sub2(this));
}
public void draw() {
background(256, 256, 256);
for(int i = 0; i < list.size(); i++)
list.get(i).show();
for(int i = 0; i < list2.size(); i++)
list2.get(i).show();
fill (0, 0, 0);
text("Press SPACE to create new class Sub. This will create an image in the left.\n"
+ "You will notice that there is an image in the top. This is the Sub2 class. \n"
+ "Press ENTER to essentially instruct class Sub2 to create an image in the right,\n"
+ "and make itself invisible.\n"
+ "However, it does not work because the sketch does not like it when classes are created\n"
+ "from another class.", 280, 300);
//These keys is detected in keyPressed()
}
public void keyPressed() {
if (key == ' ')
list.add(new Sub(this, 500, 600));
if (keyCode == ENTER)
list2.get(0).visible = false;
//When you hit ENTER the first entry in list2 (the only one that exists) becomes invisible.
//It then creates a new Sub in list.
}
}
Here is class Sub:
//Sub is the first subclass of Main. It extends Main.
package processing;
import processing.core.PApplet;
public class Sub extends Main {
//I cannot extend PApplet from this class, so I make a PApplet and do PApplet.method();
PApplet p;
Sub(PApplet parent, int x, int y) {
p = parent;
image = p.loadImage("img.png");
}
public void show() {
//All this does is create an image while it exists
p.image(image, 300, 600);
}
}
And here is class Sub2:
//Sub2 also extends Main, and its primary job is to create Sub classes when ENTER is pressed.
package processing;
import processing.core.PApplet;
public class Sub2 extends Main {
boolean visible;
PApplet p;
Sub2(PApplet parent) {
p = parent;
//You must get an image to use (it could be anything, just call it img.png, and try to make it about 100*100 or similar.)
image = p.loadImage("img.png");
visible = true;
//Theoretically, if I press ENTER, it turns visible to false. This turns it invisible, then
//the if statement goes to else and repeatedly adds new Subs to list.
}
public void show() {
if (visible)
p.image(image, 500, 100);
else {
list.add(new Sub(this, 900, 500));
//This is where the code breaks. Notice that when you hit SPACE, it creates
//a Sub in the left of the screen, that's normal.
//It runs the constructor, doing loadImage normally.
//HOWEVER: When this code is run (add new Sub), it crashes, saying:
//"The sketch path is not set."
//"Files must be loaded inside setup() or after it has been called."
//This is because when you press SPACE, it creates a new Sub within the Main class.
//But when you press ENTER, it creates a new Sub within the Sub2 class.
//I don't know why that difference would cause everything to break.
}
}
}
So again, I don’t know if this will work for you. I also don’t know if this will help (is this what I’m supposed to show you?). But if it’s helpful, you can look over it or try to run it. The comments in the code will guide you a little.
Thanks for your patience.