Exit sketch but preserve parent process

edited November 2015 in How To...

Hi, Im new to processing and I have just got started using it with java. At the minute I have a java swing application which spawns a separate sketch window using PApplet.runSketch(). When the window is closed either by pressing escape, pressing the X or calling .exit() on the PApplet, the entire program including the swing main program closes with it.

Is there a way I can make it so when sketch is closed it does not kill off my entire program and just closes the sketch window?

Thanks.

Answers

  • Dunno how to tame exit() yet. My workaround is simply use noLoop() and setVisible(false) on target PApplet. A Processing 2 example below. Easy to convert to Processing 3 though:

    https://forum.processing.org/two/discussion/comment/43044/#Comment_43044

  • There is no such setVisible() method in PApplet or PApplet.frame, digging through the source the frame variable is just a dummy, according to the comments. I tried both anyway.

    It looks like PApplet isn't part of awt.Frame anymore or something, so the methods used to manipulate frames isn't there...

  • edited November 2015

    There is no such setVisible() method in PApplet or PApplet.frame.

    ... digging through the source the frame variable is just a dummy, ...

    It looks like PApplet isn't part of awt.Frame anymore or something,

    • That's Processing 3's main "feature" btW! >:)
    • Actually it extended class Applet; thus its name.
    • Class name was kept although it's senseless now! 8-}
  • I assume that you are using Processing 3. If not you can stop reading. ;)

    Is there a way I can make it so when sketch is closed it does not kill off my entire program and just closes the sketch window?

    There are 2 solutions, the hard one or the not so hard one.

    IMPORTANT: In both cases you may get errors when closing additional windows on Linux systems.


    I am going to start with the not so hard solution and that is to use G4P. G4P enables the user to create multiple windows using the JAVA2D (default), P2D or P3D renderers. With G4P you can choose what happens when someone attempts to close a window, the 3 options are
    1) Keep the window open, in other words ignore window close requests.
    2) Close the window but leave the main application running
    3) Exit the application, which is what happens now.

    The default is (1) but you can change it to (2)
    G4P windows also have methods to make the window visible/invisible, set the window's screen location, force the window to be always on top and change the title bar text.

    More information on this can be found on my website here, there are 2 guides to using multiple windows.


    The hard way is to do what I did for G4P.
    1) Create a class that extends PApplet and in this class override the settings and setup methods.
    2) Create your own class to extend the WindowAdapter class and use this to close the window leaving the main sketch running.
    3) In the setup method remove any WindowAdapter(s) added to the window , then and add an instance of this adapter (from 2) to the window.
    4) This has to be done twice, once for JAVA2D windows and again for P2D/P3D (OpenGL) windows.
    5) Pull your hair out, scream, drink coffee and wear your fingers out on the keyboard trying to get it all to work properly.

    Of course you could look at the source code from G4P and hack it to suit your personal requirements. The main classes are GWindow, GWindowAWT (JAVA2D) and GWindowNEWT (P2D/P3D).

    If you go for the hard way - good luck ;)

  • Ive been playing around with G4P to try and make it behave as it does but in fullscreen. At the minute I am calling fullScreen() in the settings() method of the PApplet but it seems to capture applet and plop it in a window as well as stretching a separate instance to my screen size. Does G4P support fullscreen mode?

    Unfortunately hiding and showing the window isn't good enough as each time it is shown it may be on a different screen, i.e fullScreen(x) might be called with a different screen index parameter. Ill have a tinker around with the source and maybe do it the hard way!

  • I can't do much about the multiple screens because I only have the one.

    As to G4P and fullscreen mode, can you post a basic sketch showing what you mean.

  • edited November 2015

    Main

    TestSketch sketch = new TestSketch(); PApplet.runSketch(sketchArgs, sketch); GWindow window = GWindow.getWindow(sketch, null, 0, 0, 400, 400, GWindow.JAVA2D);

    TestSketch

    public void draw(EffectCanvas canvas){ canvas.ellipse(56, 46, 55, 55); } public void settings(EffectCanvas canvas) { fullScreen(1); }

    This is what i'm getting: https://imgur.com/BKPNVYZ

    When the window on the left is closed it behaves wonderfully and disappears without killing everything, hit escape on the fullscreen one and everything goes. I'm aiming for a fullscreen PApplet that just closes itself - if that makes any sense.

    Edit: the left screen is the one generated by G4P in screenshot

    Edit2: I think im getting two windows because runSketch is called again from within GWindow. If I allow GWindow to start the sketch alone I get a NullPointerException

  • I think im getting two windows because runSketch is called again from within GWindow

    You are getting 2 windows. GWindow does not replace the main sketch window it allows you to have extra windows.

    When the main sketch window is closed then the whole application (sketch) including additional windows are closed. This is the normal behaviour of the OS. In G4P if you close an extra window (GWindow) then the application can be left running.

    Your code is incomplete so I can't run it therefore I will have to assume that you have created a class called TestSketch and that it extends PApplet.

    When you call PApplet.runSketch(sketchArgs, sketch); Processing will create a window and Processing adds a WindowAdapter that closes the whole application when this window is closed. AFAIK Processing does not provide methods to change this behaviour!

    In G4P I overrode this behaviour by removing the existing WindowAdapters and adding my own.

  • edited November 2015

    If you wanna get rid of some PApplet canvas, just noLoop() it and then setVisible(false) it.

  • Found a little ghetto solution that will do for now, ended up hiding the PApplet using papplet.getSurface().setVisible(false); to fake closing it. To fake opening it on a different screen I just setpapplet.getSurface().setLocation(x,y) with negative values to push it onto the other monitors.

    Thanks for the help!

  • edited November 2015

    @munkey, don't forget to noLoop() it as well for setVisible(false).
    There's no reason to waste CPU cycles over a hidden PApplet.

    In order to reactivate it, use loop() then setVisible(true). *-:)

  • edited April 2017

    Hi Guys! I've a JavaFX UI with some control buttons (play, pause, stop). I want it to start/run a fullscreen Processing 3.2.1 sketch on PLAY. Pause the sketch on PAUSE, but keeping its state so when I hit PLAY again it continues from where it left. And close the window on STOP, without closing the JavaFX UI window.

    Using noLoop() works fine for PAUSE.

    However, using getSurface().setVisible(false) hides the screen, but leaves a small bar visible (and an item on my taskbar). Worst, when I call setVisible(true), it creates another screen (and another taskbar item), and it goes on if I keep pressing STOP and PLAY. If I manually close any of the taskbar items, all of them and the JavaFX app are closed.

    My JavaFX calls:

    PApplet.runSketch(new String[] {Performance.class.getName()}, this.performance);

    Since it's been more than 1 year from last reply, I was wondering whether there is any new built in feature which allows to make it easier without relying on third party libs, such as quark's G4P, or having to deal with all the complexity (as very well explained by quark).

    Thanx!

  • @GoToLoop

    There's no reason to waste CPU cycles over a hidden PApplet.

    It depends on the usage :D

  • When the main sketch window is closed then the whole application (sketch) including additional windows are closed. This is the normal behaviour of the OS. In G4P if you close an extra window (GWindow) then the application can be left running.

    @quark, did you manage to get this working with the OpenGL renderer? I found a way to do it with the default renderers but no luck with OpenGL. Either the second window stays open when the close button is clicked or both windows disappear and the sketch stops running. Wondering if there's anything that I missed. (I'm not using g4p).

  • In G4P if you close an extra window (GWindow) then the application can be left running

    did you manage to get this working with the OpenGL renderer?

    No, I didn't, closing the second window sometimes leaves the main app running and sometimes it crashes the sketch. :( No problem with JAVA2D :)

Sign In or Register to comment.