Implementing a Processing app inside another one

The following is one technique that could be used to open your slot machine sketch inside of another sketch. This demo requires that you have ‘processing-java’ installed, which is a command line app: https://github.com/processing/processing/wiki/Command-Line. You can then use Processing’s exec() to run your sketch by supplying the sketch’s folder location. I used macos to design and run this demo.

import java.io.*;

int _wndW = 400;
int _wndH = 300;

Process proc = null;

void setup() {
  size(_wndW, _wndH);
  surface.setTitle("Space Casino");
  background(0);
  stroke(0);
  strokeWeight(3.0);
}

void draw() {
  fill(0, 255, 0);
  rect(100, 100, 150, 30, 15);
  fill(0);
  textSize(18.0);
  textAlign(CENTER, CENTER);
  text("Run sketch", 100, 98, 150, 30);
}

void mousePressed() {
  if ((mouseX >= 100) && (mouseX <= 100 + 150) && (mouseY >= 100) && (mouseY <= 100 + 30)) {
    try {
      proc = exec("/usr/local/bin/processing-java", "--sketch=/Users/yourName/Documents/Processing/SpaceCasinoSlotMachine", "--run");

      BufferedReader out = new BufferedReader(new InputStreamReader(proc.getInputStream()));
      BufferedReader err = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
      String s = null;
      while ((s = out.readLine()) != null) {
        println(s);
      }
      while ((s = err.readLine()) != null) {
        println(s);
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

1 Like