Update 24 May 2010: Version 0.3.0. Mousewheel support, nested napplets, and other goodies. See wiki/changelog at github for more details.
Update 6 May 2010: Windowed napplets! Version 0.2.0 is now up for download and supports windowed napplets as well as the old embedded ones. Windowed napplets function just like embedded napplets with the same easy-to-use code structure, etc.
Currently the library supports creation and destruction of windowed napplets, with the option of allowing the user to close a window (and kill the napplet) with the usual window controls (e.g., the "X" button on Windows, etc.)
There's no support as yet for resizing windows (I'm working on it.)
Aboutnapplet is a lightweight library that allows you to "embed" one Processing sketch inside another. This is potentially useful for a lot of things:
- displaying data in different ways within the same window without having one display muck up another
- creating a "control panel" in a portion of a sketch with buttons, etc. that control behavior elsewhere.
- importing an existing sketch into a new one (source code transfers easily).
The library is available for download
here. Source code can be found
here.The API and coding is straightforward; an embedded sketch is simply enclosed in its own class that extends the NApplet class (which itself extends PApplet). The NApplet class contains setup() and draw() methods, global variables, etc, and in pretty much all ways behaves like a standalone PApplet (i.e., sketch).
To create and display NApplets, you simply create a NAppletManager object in the setup() method of the main sketch, and call NAppletManager.createNApplet() to create each sub-sketch and tell it where to draw in the main sketch. The NAppletManager then handles everything else "behind the scenes".
As an example,
here's a demo that runs eight of the example sketches that come with Processing. I wrote this demo in the PDE. Here's the code for the main sketch:
Code:import napplet.*;
PFont mainFont;
void setup() {
size(800, 400);
mainFont = loadFont("ArialMT-18.vlw");
textMode(SCREEN);
textAlign(CENTER, TOP);
NAppletManager nappletManager = new NAppletManager(this);
nappletManager.createNApplet("Scrollbar", 0, 0);
nappletManager.createNApplet("Pattern", 200, 0);
nappletManager.createNApplet("Convolution", 400, 0);
nappletManager.createNApplet("Animator", 600, 0);
nappletManager.createNApplet("BouncyBubbles", 0, 200);
nappletManager.createNApplet("FireCube", 200, 200);
nappletManager.createNApplet("Tickle", 400, 200);
nappletManager.createNApplet("UnlimitedSprites", 600, 200);
}
void draw() {
background(50);
}
Each sub-sketch was contained in its own tab in the PDE (you can see the source at the openprocessing.org link above). All of them were created by cutting-and-pasting the source code straight from the standalone sketches (and copying any necessary files from the sketch's data/ folder, of course.) The only change made to any of the sketches was adjusting the size() to fit in the gallery.
NApplet sketches have access to all of the variables and methods of the parent sketch, so they can potentially be parts of a larger application.
Let me know if you find this at all useful.