Reputation: 45
I am currently working on a data visualization which takes information from a movie, that plays in realtime and creates simultaneously. For that I want to have two seperate sketch windows. One window should show the movie playing in realtime and the other the ongoing visualization.
I can't seem to figure out how to easily add another sketch window and tried out some examples which are not working in Processing 3 anymore. (sojamos library Control p5 for example)
Then I stumbled upon this example: https://gist.github.com/atduskgreg/666e46c8408e2a33b09a
Eventhough I can get it to display two sketch windows at a time, I obviously cannot use the data from one window on the other one.
Is there another way to do this?
Thank you!
Upvotes: 1
Views: 2114
Reputation: 1648
Nothing prevents you from declaring a function in the PWindow
class (which creates a second window) which takes arguments you could use inside and call it from the other sketch.
So you can pass data in form as function arguments to the second window.
This small example passes the relative mousePressed
position from the first window to the second via a function (here called evokedFromPrimary
) and stores it in an ArrayList
which draws them in the second window:
PWindow win;
public void settings() {
size(320, 240);
}
void setup() {
win = new PWindow();
}
void draw() {
background(255, 0, 0);
text("Click in this window to draw at a relative position in the other window", 10, 10, this.width - 20, 100);
}
void mousePressed() {
println("mousePressed in primary window");
float relPosX = map(mouseX, 0, this.width, 0, 100);
float relPosY = map(mouseY, 0, this.height, 0, 100);
win.evokedFromPrimary(relPosX, relPosY);
}
class PWindow extends PApplet {
ArrayList<PVector> vectors = new ArrayList<PVector>();
PWindow() {
super();
PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
}
void evokedFromPrimary(float relPosX, float relPosY) {
println("evoked from primary");
float xPos = map(relPosX, 0, 100, 0, this.width);
float yPos = map(relPosY, 0, 100, 0, this.height);
vectors.add(new PVector(xPos, yPos));
}
void settings() {
size(500, 200);
}
void setup() {
background(150);
}
void draw() {
background(150);
//store the vector size before using to avoid ConcurrentModificationException
int listLength = vectors.size();
for(int i = 0; i < listLength; i++) {
PVector v = vectors.get(i);
ellipse(v.x, v.y, random(50), random(50));
}
}
void mousePressed() {
println("mousePressed in secondary window");
}
}
The Pwindow code here is in the same .pda file.
Upvotes: 3