Loading a sketch within a sketch

edited May 2015 in How To...

Hi, I'm trying to load some sketches within a "master" sketch. I found a neat blog post about it, 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. Here's the relevant code. Mine is effectively identical. 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 found a clever way of loading sketches via reflection, but when I try

Class[] classes = getClass().getDeclaredClasses();
int n = classes.length;

I get n = 0. 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”. My sketches are located in the file system like sketchbook/Sketchname.

Any idea what this could be caused by? And how to fix it? I'm using Processing 2.2.1.

Answers

  • edited May 2015

    Perhaps my multi-instance PApplet instantiation example might be of some help?
    http://forum.processing.org/two/discussion/7036/multiple-screens

    static final void main(final String[] args) {
      final String sketch = Thread.currentThread()
        .getStackTrace()[1].getClassName();
    
      final Class[] nested;
      try {
        nested = Class.forName(sketch).getClasses();
      }
      catch (final ClassNotFoundException cause) {
        throw new RuntimeException(cause);
      }
    
      println(nested);
      for (int i = 0, ii = max(0, nested.length-2); i != ii; ++i)  try {
        PApplet.main(nested[i].getName(), args);
      }
      catch (final Exception cause) {
        println(nested[i] + " isn't a PApplet or isn't public static!");
      }
    }
    
  • Perhaps my multi-instance PApplet instantiation example might be of some help?

    It is, thanks, but the problem persists. In your example, I take it you have all the classes defined in the same .pde file? I'd like to have one class per file. When I did

          final String sketch = Thread.currentThread()
            .getStackTrace()[1].getClassName();
         
          final Class[] nested;
          try {
            nested = Class.forName(sketch).getClasses();
           println(nested);      
          }
    

    I got class processing.core.PApplet$RendererChangeException class java.awt.Component$BaselineResizeBehavior.

    I tried changing my class signature to public static class Classname, but to no avail. My sub-sketches are still not visible to the master sketch. I think it's a scoping problem, but I don't know what to do about it.

  • edited May 2015

    I take it you have all the classes defined in the same .pde file?

    If we wish, we can place whole classes in separate ".pde" tabs.
    Later PDE's pre-processor will concatenate everything as 1 ".java" file before compilation.

    Take notice that PDE's pre-processor got its own template for static void main(String[] args) {}
    What I did was replacing it w/ my own which instantiates not only the main sketch, but also any nested public static class which extends PApplet. :-B

  • edited May 2015 Answer ✓

    I'll leave you w/ a more complete example here from: http://forum.processing.org/two/discussion/7202/controlp5-controlwindow-and-controlp5frame

    It's got a main sketch plus 2 nested sketches: :bz

    static final void main(final String[] args) {
      String sketch = Thread.currentThread().getStackTrace()[1].getClassName();
    
      main(sketch, args);
    
      Class[] nested;
      try {
        nested = Class.forName(sketch).getClasses();
      }
      catch (ClassNotFoundException cause) {
        throw new RuntimeException(cause);
      }
    
      println(nested);
      println();
    
      for (int i = 0, ii = max(0, nested.length - 2); i != ii; ++i)
        try {
          main(nested[i].getName(), args);
        }
        catch (Exception cause) {
          println("Either " + nested[i] + " isn't a PApplet or it isn't public static!");
        }
    }
    
    import static processing.opengl.PGraphicsOpenGL.*;
    
    void setup() {
      size(600, 400, P2D);
      smooth(8);
      noLoop();
      background( (color) random(#000000) );
    
      println( "\n" + isGL() + '\n' );
    
      println( javaVersionName );
      println( System.getProperty("java.home")  + '\n' );
    
      println( System.getProperty("os.arch") );
      println( System.getProperty("os.name") );
      println( System.getProperty("os.version") + '\n' );
    
      println( System.getProperty("user.home") );
      println( System.getProperty("user.dir")   + '\n' );
    
      println( sketchPath );
      println( dataPath("") );
      println( dataFile("") );
    
      println( "\n===[ General Info ]===\n" );
      println( "OPENGL_VENDOR: " + OPENGL_VENDOR );
      println( "OPENGL_RENDERER: " + OPENGL_RENDERER );
      println( "OPENGL_VERSION: " + OPENGL_VERSION );
      println( "GLSL_VERSION: " + GLSL_VERSION );
    
      println( "\n===[ Supported Features ]===\n" );
      println( "anisoSamplingSupported: " + anisoSamplingSupported );
      println( "autoMipmapGenSupported: " + autoMipmapGenSupported );
      println( "blendEqSupported: " + blendEqSupported );
      println( "fboMultisampleSupported: " + fboMultisampleSupported );
      println( "npotTexSupported: " + npotTexSupported );
      println( "packedDepthStencilSupported: " + packedDepthStencilSupported );
      println( "maxSamples: " + maxSamples );
      println( "maxTextureSize: " + maxTextureSize + '\n' );
    }
    
    public static final class MyFrame extends PApplet {
      void setup() {
        noLoop();
        println("MyFrame");
      }
    
      void draw() {
        background(0);
      }
    }
    
    public static final class MyApp extends PApplet {
      void setup() {
        size(300, 200, JAVA2D);
        noLoop();
        println("MyApp");
      }
    
      void draw() {
        background(-1);
      }
    }
    
Sign In or Register to comment.