(I'm pretty advanced in Java and eclipse. I could compile my own p5 core.jar if needed, but I think overriding should have to do.)
So I want to use a PApplet merely as a object for a GUI. Not as an applet, but really in a window (presentation mode).
This is because I think running all your code from in the draw() method is a bit dirty, and because I think it's more intuitive otherwise.
If there is a completely other way of doing what I want to do, don't hesitate to tell me.
This is an example that would make clear what I want. but it doesn't work for reasons I don't know. I don't get any errors though.
I used the Stripe example (with classes) given in the Processing in Eclipse page to illustrate my problem.
processing.org/learning/tutorials/eclipse/
The whole Stripe class is unchanged, it can be seen there, i will not rewrite it.
Main.java
Code:
//the main method
public class Main {
public static void main(String args[]) {
new Run();
}
}
Run.java
Code:
import processing.core.PApplet;
public class Run {
Stripe[] stripes = new Stripe[50]; //array of 50 strypes.
GUI gui = new GUI(); //GUI is a class that extends PApplet.
public Run(){
//setup
PApplet.main(new String[] { "GUI" }); //this inits the PApplet, it also runs the setup() method i presume.
gui.init(); //it could be that the setup would be run from here. but anyways, i've put noLoop() in setup() as you can see further. Because i think this is needed.
//construct all the stripes, the Stripes method is unchanged.
for (int i = 0; i < stripes.length; i++) {
stripes[i] = new Stripe(gui); //I give the reference of the GUI to Stripe, so it knows where it should draw.
}
//this would be a first drawing loop, but leaveable and more in other cases. unlike the draw loop
while(true){
//actually, it allready fails here. the function does get called though. (if i'd be printing the gui.random(20) to the console, it gives a stream of random numbers.)
gui.background(gui.random(20));
//this is more stuff that happens in the demo. it fails, but it's the same problem as above.
//as mentioned, the Stripe class is unchanged.
for (int i = 0; i < stripes.length; i++) {
stripes[i].move();
stripes[i].display();
}
//i guess something other would be needed here, but i tried a lot here.
//my biggest insight was that i would be able to make a PGraphics buffer to draw to and draw it to the screen in the draw() method using image(buffer, 0, 0), but it seems i can't even draw stuff in a PGraphics thingy outside the draw() method.
gui.redraw();
}
}
}
GUI.java
Code:
import processing.core.*;
public class GUI extends PApplet {
public void setup() {
size(300,200, P2D);
noLoop(); //i think this is needed.
}
//override the draw() method with an empty one because not doing so would terminate the program as you can see in the source of PApplet.java.
public void draw() {
}
}