javainputoutputprocessingvrpn

Getting Processing to read Java file output/values


I'm writing a program to read the button values from a VRPN device using Processing with a sample Java file that works perfectly in the command line.

I've already set up my sketch folder with the appropriate library/files, I just need to reconfigure the Java code to send values over to the PDE side of things (currently System.out.println()). I've tried establishing a PApplet object but had trouble configuring it within the ButtonTest class. I've tried researching this sort of functionality within Java but have had trouble finding the right terminology.

import vrpn.*;
import processing.core.PApplet;

public class ButtonTest implements vrpn.ButtonRemote.ButtonChangeListener {


  public void buttonUpdate( ButtonRemote.ButtonUpdate u, ButtonRemote button) {
    System.out.println( "Button message from vrpn: \n" +
  "\ttime:  " + u.msg_time.getTime( ) + "  button:  " + u.button + "\n" +
  "\tstate:  " + u.state );
  }

  public static void main(String[] args) {

    String buttonName = "spaceNav@127.0.0.1:3891";
    ButtonRemote button = null;

    try {
      button = new ButtonRemote( buttonName, null, null, null, null );
    } 

    catch(InstantiationException e) {
      // if fails
      System.out.println( "We couldn't connect to button " + buttonName + "." );
      System.out.println(e.getMessage());
      return;
    }

    ButtonTest test = new ButtonTest(pa);
    button.addButtonChangeListener(test);
  }
}

My file structure: (PDE file is currently empty/ready and waiting)

My file structure

Just for reference I've managed to send values from a Java file to a Processing sketch seen here, I just can't seem to implement it for this project https://forum.processing.org/two/discussion/3677/


Solution

  • You need to get the data from the external class into the Processing sketch. You have two options for that:

    Option One: In your Processing sketch, simply call a getter function whenever you want to get the data from the external class. Here's a simple example:

    ExternalClass ec;
    
    void setup(){
       ec = new ExternalClass();
    }
    
    void draw(){
       println(ec.getData());
    }
    

    The benefit of this is that your external class doesn't have to know anything about Processing. The downside is you have to constantly poll the external class.

    Option Two: Pass a reference to your Processing sketch to your external class, then call functions in your Processing sketch from the external class. You'd do that by first obtaining a reference to an instance of your sketch class, possibly passed in through the constructor. Then you'd call functions of your sketch class using that instance.

    On the Processing side, you can use the this keyword to pass a reference to the sketch itself. It might look like this:

    ExternalClass ec;
    
    void setup(){
       ec = new ExternalClass(this);
    }
    
    void setData(int whatever){
       println(whatever);
    }
    

    Also notice the setData() function, which you can call from your external class.

    Now, the external class would need to reference your sketch. In the Processing editor, you would just use the name of your sketch:

    public class ExternalClass{
       YouSketchName sketch;
    
       public ExternalClass(YourSketchName sketch){
          this.sketch = sketch;
       }
    
       private void dataChanged(){
          sketch.setData(42);
       }
    }
    

    This allows you to set the data when the event occurs instead of constantly polling for it, but it's more complicated.