how to use a ClientEvent in a class with registerMethod

I would like to use the ClientEvent in my own class. I tried this with mouseEvent (see the code below) and that works. However adding clientEvent doesn't work, I get this error: ReadEEG.pde:18:0:18:0: RuntimeException: There is no public clientEvent() method in the class research_recorder_v2$ReadEEG

Any idea how I can use this event in a class?

import processing.net.*;

    public class ReadEEG 
    {
        Client c;
        String input;
        PApplet app;

        public ReadEEG (PApplet p) {

             //app = p; 
             c = new Client(p, "127.0.0.1", 12111); 

             p.registerMethod("clientEvent", this);
             p.registerMethod("mouseEvent", this);

             c.write("ADD ID,dev1 dev1ADC");
        }

        public void clientEvent(Client c) {
          input = c.readString();
          //println(input);
          println("clientEvent called");
        }

        public void mouseEvent(MouseEvent e)
        {
            println("mouseEvent called");
        }

    }

Answers

  • edited March 2014 Answer ✓

    I've got no idea about "processing.net" library. However I think registerMethod() was meant exclusively for the Processing's
    built-in event callbacks, like keyPressed(), mouseMoved(), etc. Since they got no other means to specify a parent class. :(|)

    Seems like the Client's constructor has a parent class parameter that you should use!
    Have you tried -> new Client(this, "127.0.0.1", 12111);? :-??

  • If I do that I get an error as well: The constructor Client(research_recorder_v2.ReadEEG, String, int) is undefined.

    However it works when I extend the class from PApplet: public class ReadEEG extends PApplet

    Another workaround is to call a function in the class by the event in the "main" code.

    public void clientEvent(Client c) 
    {     readEEG.clientEvent(c);   
    }
    
  • I'm curious if extending as PApplet is a good habit. I see that the Arduino library does it, so probably it's a good way to do it.

    I also noticed when creating a Printwriter (this is a bit a different subject) you need to get it from the main PApplet, otherwise it can't write to the right directory.

    outputCSV = createWriter("test.csv"); gives the error:

    Couldn't create a writer for c:\program files\processing-2.1.1-windows64\test.csv

    Solution: outputCSV = p.createWriter("test.csv");

  • edited March 2014

    So it seems like class Client accepts a reference to a PApplet only for registering! :(
    Extending PApplet effectively turns your class a valid reference for it. Although it's a little over-the-top doing so! :-\"

    Anyway, I find the 2nd option more streamlined on how Processing's framework operates.
    Leave callbacks to the sketch's top-class, and let them be responsible to coordinate the external calls from other classes! B-)

  • edited March 2014

    I'm curious if extending as PApplet is a good habit. I see that the Arduino library does it, so probably it's a good way to do it.

    IMO, only extend classes if you're gonna mirror many of its resources or it's a natural evolution from it. L-)
    Otherwise, use composite inheritance.
    Or leave that to the sketch's main top-class, which is the effective PApplet, to take care of it! ~:>

  • edited March 2014

    registerMethod accepts a second argument which can be the instance of the class wanting to be notified (this if the class registers itself).

  • @PhiLho I don't get what you mean. Do I have use registerMethod in my "main" code?

    What do I need to change in this call: p.registerMethod("clientEvent", this);

  • Answer ✓

    Ignore my remark, I fear I based my answer on part of GoToLoop's answer more than on your original message.

    Now, he is right: only Processing's built-in callbacks (mouseEvent, keyEvent, touchEvent) can be called with a parameter. Other callbacks can be registered, but only if they have no parameters.

Sign In or Register to comment.