How to pass arguments in PApplet main method?

edited September 2015 in How To...

Hello. I'm working processing on Eclipse IDE.

Currently, I have 2 main thread, one from normal Java Application, and the other from PApplet. Problem is that, when I call PApplet main method , I need to pass Objects, for example:

public static void main(String args[],Object obj) { PApplet.main(Object obj, new String[] {"Visualization.Test"}); }

Or, is there a possible way to pass references to PApplet not using static variable?

Answers

  • edited September 2015

    PApplet's "igniter" main() can at most pass the extra String[]'s elements after "sketch_name" as args[]:

    //forum.processing.org/two/discussion/12483/
    // how-to-pass-arguments-in-papplet-main-method
    
    // GoToLoop 2015-Sep-12
    
    public static void main(String[] args) {
      String[] mainSketch = concat(new String[] { getSketchClassName() }, args);
      PApplet.main(mainSketch);
    
      String[] sketches = getSketchNestedClassNames();
      String[] myArgs = { getSketchClassName(), sketches[0], str(PI) };
      for (String sketch : sketches)  main(sketch, myArgs);
    }
    
    public static final String getSketchClassName() {
      return Thread.currentThread().getStackTrace()[1].getClassName();
    }
    
    public static final String[] getSketchNestedClassNames() {
      Class[] nested;
    
      try {
        nested = Class.forName(getSketchClassName()).getClasses();
      }
      catch (ClassNotFoundException cause) {
        throw new RuntimeException(cause);
      }
    
      int idx = 0, len = max(0, nested.length - 2);
      String[] classes = new String[len];
    
      while (idx != len)  classes[idx] = nested[idx++].getName();
      return classes;
    }
    
    public void setup() {
      size(300, 200, JAVA2D);
      noLoop();
      background(#00FFFF);
      printArray(args);
    }
    
    public static class Test extends PApplet {
      public void setup() {
        size(200, 300, JAVA2D);
        noLoop();
        background(#FFFF00);
        printArray(args);
      }
    }
    
  • edited September 2015 Answer ✓

    To pass references rather than simple String args[], you need to replace main() w/ runSketch().
    Using the latter, we pass a PApplet object. Therefore that PApplet can have a constructor w/ parameters!

    http://forum.Processing.org/two/discussion/12319/using-papplet-runsketch-to-create-multiple-windows-in-a-ps3-sketch

    /**
     * Multi-Monitor Sketch (v1.61)
     * by GoToLoop (2015/Jun/28)
     *
     * forum.processing.org/two/discussion/12483/
     * how-to-pass-arguments-in-papplet-main-method
     *
     * forum.processing.org/two/discussion/12319/
     * using-papplet-runsketch-to-create-multiple-windows-in-a-ps3-sketch
     */
    
    void setup() {
      size(300, 300, JAVA2D);
      smooth(4);
      noLoop();
      frameRate(60);
      stroke(-1);
      strokeWeight(1.5);
    
      runSketch( new String[] { "--display=1",
                                "--location=0,0",
                                "--sketch-path=" + sketchPath,
                                "" },
                 new ProjectorSketch() );
    
      println("Main's  sketchPath: \t\"" + sketchPath + "\"");
      println("Main's  dataPath: \t\"" + dataPath("") + "\"\n");
    }
    
    void draw() {
      background(0);
      line(0, 0, width, height);
    }
    
    class ProjectorSketch extends PApplet {
      void setup() {
        size(displayWidth>>1, displayHeight>>1, JAVA2D);
        smooth(4);
        frameRate(1);
        stroke(#FFFF00);
        strokeWeight(5);
    
        println("Inner's sketchPath: \t\"" + sketchPath("") + "\"");
        println("Inner's dataPath: \t\"" + dataPath("") + "\"\n");
      }
    
      void draw() {
        background((color) random(#000000));
        line(width, 0, 0, height);
    
        saveFrame( dataPath("screen-####.jpg") );
      }
    }
    
Sign In or Register to comment.