Program in a Program?

Inspired by this thread I tried my hand on a way to run multiple Processing sketches at the same time, spawned from a “parent” sketch. Then the secondary sketches are made invisible, and the parent sketch retrieves their graphic content. Could be a start to what @Repet is after.

Downside is that mouse and keyboard events do not get forwarded to the secondary sketches. I believe that can be solved by some tinkering, but might not be thread safe.


PWindow[] win;

public void settings() {
  size(600, 600);
}

void setup() { 
  win = new PWindow[4];
  for(int i = 0;i < win.length; i++)
  {
    win[i] = new PWindow();
  }
  surface.setLocation(20,20);
}

void draw() {
  background(0);
  image(win[0].get(), 0, 0);
  image(win[1].get(), 0, 300);
  image(win[2].get(), 300, 0);
  image(win[3].get(), 300, 300);
}

void mousePressed() {
} 

class PWindow extends PApplet {
  PWindow() {
    super();
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
  }
  
  int h = (int) random(255);
  PVector position;

  void settings() {
    size(300, 300);
  }

  void setup() {
    colorMode(HSB);
    position = new PVector(random(width), random(height));
    surface.setVisible(false);
  }

  void draw() {
    background(color(h++, 255, 255));
    ellipse(position.x, position.y, random(50), random(50));
    h=h%255;
  }

  void mousePressed() {
    println("mousePressed in secondary window");
  }
}