I\'ve built these 3 applications. Each of them collects data from user input and creates a csv file. 2 of them rely on the LEAP motion controller. I am trying to figure out
There are quite a few questions on the implementation really... Do you want just one big app or do you also want them as different ones? Do you want to run the three finished apps, or do you want to combine the source code? You can also make them into libraries... Regarding your comment in the other question (Create more than one window of a single sketch in Processing), yes of course you can create another PApplet to store your application. I modified the example a bit to showcase.
Here, I created two sketches RedBG and BlueBG like this:
int counter = 0;
public void setup() {
size(400, 400);
counter = 0;
}
public void draw() {
background(255, counter, counter);
fill(255-counter);
text(String.valueOf(counter), width*0.5, height*0.5, width, height);
counter++;
}
This is the red one, the blue one has background(counter, counter, 255);
, and they both work as proper sketches. Then I took the two codes and placed them in different tabs in a controller sketch, and wrapped them into classes like this:
public class RedBG extends PApplet {
int counter = 0;
public void setup() {
size(400, 400);
counter = 0;
}
public void draw() {
background(255, counter, counter);
fill(255-counter);
text(String.valueOf(counter), width*0.5, height*0.5, width, height);
counter++;
}
}
Then, the controller class is just a modification of my answer in here. Here's its code:
import javax.swing.*;
PApplet r, b;
PFrame rf, bf;
String nextWindow = "red";
int controllerCounter = 200;
String control = "preparing...";
void setup() {
size(400, 400);
r = new RedBG();
b = new BlueBG();
frame.setTitle("controller");
fill(0);
}
void draw() {
background(255);
if (controllerCounter < 1) {
switchState();
controllerCounter = 200;
}
control = null;
control = "Launching " + nextWindow + " in: " + controllerCounter;
text(control, width*0.5, height*0.5, width, height);
controllerCounter--;
}
void switchState() {
if (nextWindow == null) {
stopApplet(b);
nextWindow = "red";
}
else if (nextWindow.equals("red")) {
startApplet(r);
nextWindow = "blue";
}
else if (nextWindow.equals("blue")) {
stopApplet(r);
startApplet(b);
nextWindow = null;
}
}
void startApplet(final PApplet p) {
if (p == null) return;
final PFrame f = new PFrame(p);
p.frame = f;
f.setTitle(p.getClass() + " window");
//this thread is only necessary if you are restarting the PApplets
Thread t = new Thread(new Runnable() {
public void run() {
p.setup();
}
});
t.run();
}
void stopApplet(PApplet p) {
if (p == null || p.frame == null) return;
p.dispose();
p.frame.dispose();
}
public class PFrame extends JFrame {
public PFrame(PApplet p) {
setSize(400, 400);
add(p);
p.init();
show();
}
}
The two classes that came from the sketches are:
public class BlueBG extends PApplet {
int counter = 0;
public void setup() {
size(400, 400);
counter = 0;
}
public void draw() {
background(counter, counter, 255);
fill(255-counter);
text(String.valueOf(counter), width*0.5, height*0.5, width, height);
counter++;
}
}
and:
public class RedBG extends PApplet {
int counter = 0;
public void setup() {
size(400, 400);
counter = 0;
}
public void draw() {
background(255, counter, counter);
fill(255-counter);
text(String.valueOf(counter), width*0.5, height*0.5, width, height);
counter++;
}
}
In short, take all your code from the three sketches (all tabs), throw them in a new tab in the controller sketch, and wrap with a class extending PApplet.
Unfortunately you can't have the tabs of your three sketches in the controller sketch, unless you modify your code. In the modified sample that follows, only lala1() and lala3() are in the blue window. lala2() is in the controller window...
public class BlueBG extends PApplet {
int counter = 0;
public void setup() {
size(400, 400);
counter = 0;
}
public void draw() {
background(counter, counter, 255);
fill(255-counter);
text(String.valueOf(counter), width*0.5, height*0.5, width, height);
counter++;
lala1();
lala2();
lala3(this);
}
public void lala1() {
fill(255, 255, 0);
ellipse(100, 100, 100, 100);
}
}
public void lala2() {
fill(255, 0, 255);
ellipse(150, 150, 100, 100);
}
public void lala3(PApplet p) {
p.fill(0, 255, 255);
p.ellipse(200, 200, 100, 100);
}
Last but not least, sometimes the code will throw NullPointerException and weird error messages like "missing a pushMatrix() to go with that popMatrix()" on a background method call. This is caused by the setup()
call in the startApplet()
method and it is an issue of concurrency thus needs deeper thinking and knowledge... As a temporary measure I made it call setup()
from a thread... If you are not going to repeat the process, then the whole thread thing is not necessary, and you don't need to setup()
every time.!
P.S. This is a hacky-slashy solution... My suggestion is to merge your sketches into one and do it properly...