# Program in a Program?

**URL:** <https://discourse.processing.org/t/program-in-a-program/386>\
**Category:** Project Guidance\
**Created:** [May 25, 2018, 10:18pm UTC](https://discourse.processing.org/t/program-in-a-program/386 "2018-05-25T22:18:14Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![colouredmirrorball](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/colouredmirrorball/32/234_2.png) [@colouredmirrorball](https://discourse.processing.org/u/colouredmirrorball)\
**Post date:** [May 29, 2018, 4:03pm UTC](https://discourse.processing.org/t/program-in-a-program/386/14 "2018-05-29T16:03:37Z")

</div>

Inspired by [this thread](https://discourse.processing.org/t/closing-a-second-window/381/14) 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.

```auto

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");
  }
}

```

---

_[View the full topic](https://discourse.processing.org/t/program-in-a-program/386)._
