Events with two windows using ControlP5 + Processing 3

edited November 2015 in Library Questions

Hi. I have a project wich runs in two windows. One is intended to control the other. So, in the first one I'm drawing CP5 controls. But I'n not being able to handle the events in the second window. I mean, I don't know how to capture events in a second window.

Default code would be: cp5.addToggle("toggleValue")

Well, that tries to resolve the event in the same window.

I can see you can pass a misterious object as first param, so I tried things like: cp5.addToggle(secondWindow, "toggleValue")

But that wont work either. The error is always this: Exception in thread "Thread-2" java.lang.NullPointerException

I think is previous versions one could create windows using controlWindow. Not sure. I'm using processing 3. And I'm creating the windows myself (in Eclipse context)

So, any clue on how to define a event listener or something like that?

Thanks in advance.

Answers

  • The method I was looking for seems to be .plugTo()

  • Still working on this. I think I managed to make it work. Each PApplet window must declare it's own ControlP5 instance. And the, one creates the controller and renders it in the other window. Cool. However, now there's an error related to P2D renderer. It crashes with something like:

    "cannot run the OpenGL renderer outside the main thread".

    If I try using the default renderer, the screen flashes.

    By the way, this official code won't work with Processin 3.0: http://www.sojamo.de/libraries/controlP5/examples/extra/ControlP5frame/ControlP5frame.pde Error is: the method add(Component) in the type Container is not applicable for the arguments (ControlFrame).

    And I searched the forum, and found info like this, but at a first glance it is chinese for me: https://forum.processing.org/two/discussion/7202/controlp5-controlwindow-and-controlp5frame

    So, I'd be happy if I found a way to use P2D renderer again. Any clue?

  • edited November 2015

    In my experiments w/ multiple PApplet windows, I was only able to have 1 of them use an OpenGL renderer. All the others had to stick w/ JAVA2D renderer. :(

  • Oh, well, good to know. I can manage, but I need to know something unrelated to this. Why the first PApplet under Eclipse opens in fullscreen mode? I don't mean the fullscreen that we can control with processing, but the java window itself. I mean, it's fullscreen while my sketch occupies just 800x600 of the screen. If I can solve that, I don't need a second P2D PApplet :)

  • Never used Eclipse yet. Sorry... 8-|

  • @gperez the ControlFrame example hasn't been updated yet. There is discussion about the Component error here. Just in case, a working example using the P3D and P2D renderer with 2 windows:

    import controlP5.*;
    
    ControlFrame cf;
    float s1;
    
    void settings() {
      size(400, 400,P3D);
    }
    
    void setup() {
      cf = new ControlFrame(this,200,200,"box");
    }
    
    void draw() {
      background(s1*255);
      translate(width/2, height/2);
      rotateY(frameCount*0.05);
      box(100);
    }
    
    class ControlFrame extends PApplet {
    
      int w, h;
      PApplet parent;
      ControlP5 cp5;
    
      public ControlFrame(PApplet _parent, int _w, int _h, String _name) {
        super();   
        parent = _parent;
        w=_w;
        h=_h;
        PApplet.runSketch(new String[]{this.getClass().getName()}, this);
      }
    
      public void settings() {
        size(w, h, P2D);
      }
    
      public void setup() {
        cp5 = new ControlP5(this);
        cp5.addSlider("s1").setPosition(40,40).setRange(0,1).plugTo(parent,"s1");
      }
    
      void draw() {
        background(20);
        pushMatrix();
        translate(width/2, height/2);
        rotate(frameCount*0.05);
        fill(255,0,0);
        rect(0,0,100,100);
        popMatrix();
      }
    
    }
    

    and here an example for an eclipse project to open a processing sketch (non-fullscreen, window only), also see this thread:

    package some.example.package;
    
    import processing.core.PApplet;
    
    /* make sure you have added the latest processing 3 dependencies to 
     * your classpath:
     * core.jar
     * gluegen-rt.jar
     * jogl-all.jar
     * */
    
    public class Two_Windows extends PApplet {
    
        public void settings( ) {
            size( 400 , 400 , P3D );
        }
    
        public void setup( ) {
            noStroke();
        }
    
        public void draw( ) {
            background(200);
            lights();
            fill(0,255,255);
            pushMatrix();
            translate(width/2,height/2);
            rotateY(frameCount*0.05f);
            box(100);
            popMatrix();
                noLights();
        }
    
        static public void main( String[] args ) {
            PApplet.main( Two_Windows.class.getName( ) );
        }
    
    }
    
Sign In or Register to comment.