makingthenoise.com : music, art, code
August 17, 2013 ,

One Sketch to Rule Them All (Part 2)

In my last post, I talked about how you can load multiple Processing sketches from one master sketch.  However, the 2 techniques shown in that post had the annoying side effect of popping up multiple windows as each sketch was loaded.  In this post, I’ll show a way flip between sketches all within the same window.

ChildSketchLoader

In this image, you can see that 2 different sketches are loading within the same window.  This allows you to seamlessly transition from 1 sketch to another without having to close windows or switch between windows once the sketch starts.

The code is here: http://pastie.org/8245189

As you can see, I’m declaring a PApplet called “childSketch” within my master Processing sketch.  Then, during the draw() routine, I’m calling:

childSketch.draw();
image(childSketch.get(), 0, 0);

What this does is tell the child sketch to render its output, and then we display that output as an image within the parent sketch.  If I want to flip between 2 different sketches, I can do so with a command like:

public void keyPressed() {
		if (key == '1') {
			childSketch = new SimpleSketch1();
			childSketch.init();
			childSketch.setup();
		} else if (key == '2') {
			childSketch = new SimpleSketch2();
			childSketch.init();
			childSketch.setup();
		}
	}

This works great for flipping between 2 sketches, and Java’s garbage collection takes care of any of the resources being used after a sketch is released.  But what if we want to display 2 sketches at the same time within 1 window?  Can’t be done!!!! Oh wait, yes it can.  On to another post….

4 to “One Sketch to Rule Them All (Part 2)”

Trackbacks/Pingbacks

  1. […] In my last post, I showed how you can flip between 2 Processing sketches within the same window.  This might be useful if you have a library of sketches you want to demo, but don’t want to close/open each sketch by hand. […]

  1. makingthenoise.com says...

    […] In my last post, I showed how you can flip between 2 Processing sketches within the same window.  This might be useful if you have a library of sketches you want to demo, but don’t want to close/open each sketch by hand. […]

  2. appas says...

    Hi,
    I’m trying to reproduce what you’ve done here, but am stuck at the stage where child sketches are loaded into the master sketch: Processing can’t find the sketches I’m trying to load.
    In the load method, I get java.lang.ClassNotFoundException. If I try myself with PApplet sketch = new Sketchname(), I get “Cannot find class or type Sketchname”.
    I think this might be due to the sketches not being in the same package. Could this be? I don’t have a package declaration at all, because when I tried to add package org.myorg.something (as the very first line), I got “syntax error on “package”, delete this token”

    Any idea what this could be caused by? And how to fix it?

    Many thanks. May you be happy and well.

  3. cjsimon says...

    The link seems to be dead. Can you re upload the code?

Leave a Reply