Is it possible to write a text-only application?

edited May 2016 in How To...

Even without a draw method, the default program opens a 200x200 window. Is there any way to create a sketch that only prints text?

Answers

  • I thought there used to be something like a noDraw() function, but I can't find it now so I could be wrong.

    You could try to put whatever print statements you want in your setup() function and then call the exit() function after everything is done, but before the draw() function is called.

    However, at this point, why use Processing at all, if you aren't using any of its visualization features? Just use pure Java, and this is trivial.

  • If in your sketch you handle command line input you should be able to launch the applet from command line and hiding the window/JFrame would be a matter of doing this in setup(): frame.setVisible(false);.

    However, as Kevin mentions, it might be easier to write your app as a plain Java command line application and if you need Processing functionalities you can use the Processing library(core.jar) with your Java class.

    This should be fairly simple using eclipse for example.

  • edited December 2013

    ... the default program opens a 200x200 window.

    AFAIK, default size() is 100x100!?



    Processing doesn't create a canvas until setup() is finished! :))
    So if you divert your program to other functions invoked from setup(),
    you'll never see the canvas until your app is finished!!! \m/
    Just slap an exit() call at the end of setup() to get rid of that undesired canvas window! >:)

  • edited December 2013

    @orgicus advise would only work if that statement would be run after setup() is done completely!
    B/c Processing would issue a frame.setVisible(true); right after the end of setup() anyways,
    overriding our own frame.setVisible(false);. :-SS

    If you prefer that approach, I've got another idea-> Have some thread("") function,
    invoked at the end of setup(), to turn the canvas off after about delay(100);:

    /** 
     * Hide Canvas Frame (v1.0)
     * by  GoToLoop (2013/Dec)
     * 
     * forum.processing.org/two/discussion/1967/
     * is-it-possible-to-write-a-text-only-application
     */
    
    void setup() {
      size(800, 600);
      frameRate(60);
      smooth();
    
      // rest of init block...
    
      thread("noFrame");
    }
    
    void draw() {
      println(frameCount);
    }
    
    void noFrame() {
      delay(100); // aw8 for draw() to start.
      frame.setVisible(false); // now we can hide that!
    
      delay(10*1000); // alternative to shutdown application
      exit();         // after a specified # of milliseconds!
    }
    
  • Keep in mind that that thread approach is a hack at best. You're relying on the execution order of threads, which you should never do. And in the best case scenario, you still have a frame pop up for a split second.

    And assuming Processing uses Java's EDT, you shouldn't be messing with the frame off of it anyway.

    Why not just put an exit() where you have thread("noFrame") and not worry about any of the above? :D

  • edited December 2013

    You're relying on the execution order of threads, which you should never do.

    Well, just raise delay(100); to something higher like delay(200)!
    I'm pretty sure draw() woulda already started after that! :D

    And in the best case scenario, you still have a frame pop up for a split second.

    Well, as you said, it's a tad hackish! :ar!
    But as a 3rd option, we can place if (frame.isVisible()) frame.setVisible(false); at the beginning of draw() instead!

    Why not just put an exit() where you have thread("noFrame"); and not worry about any of the above?

    Well, that was my original approach-> never let setup() reach its end until your app is finished!
    The 2nd approach however, allows us to place our code inside draw() and get a free infinite loop! :bz

  • println("user.dir: " + System.getProperty("user.dir"));
    println("user.home: " + System.getProperty("user.home"));
    println("sketchPath: " + sketchPath);
    exit();
    

    for example... Output is in the console, of course.

  • edited December 2013

    Found a much better way while I was looking around PApplet source code! \m/
    Just override displayable() to always return false! =))
    However, now we gotta have an alternative feature to exit() a windowless app! b-(

    /** 
     * Hide Canvas Frame II (v1.0)
     * by  GoToLoop (2013/Dec)
     * 
     * forum.processing.org/two/discussion/1967/
     * is-it-possible-to-write-a-text-only-application
     */
    
    void setup() {
      size(800, 600);
      frameRate(60);
      smooth();
    }
    
    void draw() {
      println(frameCount);
    }
    
    @ Override boolean displayable() {
      return false;
    }
    
Sign In or Register to comment.