Fire custom event from PApplet

I'm using Processing under Eclipse, and I have two sketches that would be loaded in two different JPanel inside a JFrame. I need to fire custom events from each sketch to be handled in another class. I'm using the EventObject and the EventListener model. Thanks.

Answers

  • Do you really need events? Or are you just trying to call a method in one sketch from the other?

  • I'm thinking in events, maybe it can be done in other way. What I need is to notify to the "sketch loader", that one sketch ended successfully (or with error), and to try to reload or load the other sketch according to this. Thanks.

  • Either way, you need to pass some references around. You either need to pass your main class into each sketch, or pass the sketches to one another. You would do this via setter functions.

  • edited March 2015

    Quick & dirty solution. Declare a boolean static field and @Override exit() to set it to true: :ar!

    public static boolean i_am_finished;
    
    @ Override public void exit() {
      i_am_finished = true;
      super.exit();
    }
    
  • Dirty is right. This is a misuse of the static keyword, and I wouldn't recommend it.

  • edited March 2015 Answer ✓

    A more elaborate callback hack:

    • Let's say your loader class is called PAppletIgniter and 1 of your sketch's class is called Sketch.
    • We can have 1 static method inside PAppletIgniter which is called back by some PApplet instance when it's exit()ed!
    • It's up to you to handle what to do from there though...

    "PAppletIgniter.java":

    import processing.core.PApplet;
    
    public class PAppletIgniter {
      public static void i_am_finished(PApplet p) {
        PApplet.println("PApplet", p, "has finished...");
      }
    }
    

    "Sketch.java":

    import processing.core.PApplet;
    
    public class Sketch extends PApplet {
      @ Override public void exit() {
        PAppletIgniter.i_am_finished(this);
        super.exit();
      }
    }
    
  • Gross. Why not just use Objects the way they're intended?

    PAppletIgniter.java:

    import processing.core.PApplet;
    
    public class PAppletIgniter {
      public void i_am_finished(PApplet p) {
        PApplet.println("PApplet", p, "has finished...");
      }
    }
    

    Sketch.java:

    import processing.core.PApplet;
    
    PAppletIgniter myPapplet; //set this when you instantiate PAppletIgniter
    
    public class Sketch extends PApplet {
      @ Override public void exit() {
        myPapplet.i_am_finished(this);
        super.exit();
      }
    }
    
  • Thanks for your answers, I'll give it a try and post the results here.

Sign In or Register to comment.