Reputation: 853
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 a way to combine them all into one program. Any tips on how I could go about doing this? Can I just enclose them into like a class then have the main program call each one individually? Or would I need to go in and rewrtie everything to work together? Each application makes very different use of the draw() with all types of flags and noLoop() and Loop() calls to pause as necessary to collect data so rewrite all as 1 would be difficult. Furthermore the code is extremely sloppy in its current form. I basically hacked away and pulled together randoms bits to get each one to do what I needed to do. Each one is a mess so combining all into one program seems like it would be a real nightmare.
Is there anyway to package them so as one ends the other automatically opens? Kinda like how in a webpage you can just link or automatically open another page after one closes? Each application just needs to run once so I don't have to worry about going back and forth.
Eventually I'm going to build a visualization that updates in real time with the data from these applications. I think I may build a web application for that part because I want the visualization to be able to be viewed from multiple locations/platforms.
I've included a dropbox link to all the code. Someone suggested embedding the application directly into Java using a Swing Interface. Now I'm reading this which I think is explaining how to go about doing this. I've never used Java Swing before so I am currently lost....
Upvotes: 2
Views: 1760
Reputation: 2800
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...
Upvotes: 3
Reputation: 1249
Take a look at http://www.onar3d.com/mother/
It is a library for VJs combine a bunch of little sketches into a performance. You will have to add some code to each of your sketches in order for them to talk and you'll need to write a short sketch to sequence the sketches.
All of the apps will live inside of one processing sketch, so you won't have one app and window close and then the next one open, it will all be one seamless experience. If that is less desirable then @krowe's solution is simple and reliable.
Upvotes: 1
Reputation: 2280
Easy solution: Just make a batch file and add one line for each application. To make one wait for the next call each like so:
START /WAIT MyApp1.exe
START /WAIT MyApp2.exe
START /WAIT MyApp3.exe
Upvotes: 2