controlP5 items outside setup() function
              in 
             Contributed Library Questions 
              •  
              2 years ago    
            
 
           
             hi everybody!
            
             
            
            
             
            
            
             
            
            
             
            
            
             
            
            
             
              
             
             
               
             
             
               
             
             
              
             
             
                 
             
             
                 
             
             
                 
             
             
                 
             
             
              
             
             
                  
             
             
              
             
             
              
             
             
               
             
             
              
             
             
              
             
             
              
             
              
           
 
            
           
             First of all, I am new to the forum and I would like to thank everybody for the great work!
            
            
             and here is my question...
            
            
             I am trying to develop a multitouch application that make use of fiducials as well. I would like to create a menu with controlP5 every time I add a fiducial to my application. For developing I am using the TUIO simulator. As starting point I am using the TUIO demo sketch that comes with the TUIO library.
            
            
             the problem is that most of the time (but not always!) when I initiate a new controller inside the addTuioObject() function I get a 'java.lang.NullPointerException'
            
            
             my doubt is if it's possible to create a controller outside the setup() function and if I am doing it the right way...
            
            
             moreover, after I've added a certain number of controllers to my sketch I also get this other exception 'Exception in thread "Animation Thread" java.lang.RuntimeException: pushMatrix() cannot use push more than 32 times'.
            
            
             I am not sure, but I think those two errors are connected.
            
            
             Here is the code, hope someone could help me out!
            
            
             thanx in advance
            
            
              import TUIO.*;
             
             
              import controlP5.*;
             
             
               TuioProcessing tuioClient;
             
             
               ControlP5 controlP5;
             
             
               Button menu;
             
             
               ListBox lista;
             
             
               // these are some helper variables which are used
             
             
               // to create scalable graphical feedback
             
             
               float cursor_size = 15;
             
             
               float object_size = 60;
             
             
               float table_size = 760;
             
             
               float scale_factor = 1;
             
             
               PFont font;
             
             
               void setup()
             
             
               {
             
             
                 //size(screen.width,screen.height);
             
             
                 size(640,480);
             
             
                 noStroke();
             
             
                 fill(0);
             
             
                 loop();
             
             
                 frameRate(30);
             
             
                 //noLoop();
             
             
                 hint(ENABLE_NATIVE_FONTS);
             
             
                 font = createFont("Arial", 18);
             
             
                 scale_factor = height/table_size;
             
             
                 // we create an instance of the TuioProcessing client
             
             
                 // since we add "this" class as an argument the TuioProcessing class expects
             
             
                 // an implementation of the TUIO callback methods (see below)
             
             
                 tuioClient  = new TuioProcessing(this);
             
             
                 controlP5 = new ControlP5(this);
             
             
               }
             
             
               // within the draw method we retrieve a Vector (List) of TuioObject and TuioCursor (polling)
             
             
               // from the TuioProcessing client and then loop over both lists to draw the graphical feedback.
             
             
               void draw() {
             
             
                 background(255);
             
             
                 textFont(font,18*scale_factor);
             
             
                 float obj_size = object_size*scale_factor; 
             
             
                 float cur_size = cursor_size*scale_factor; 
             
             
                 Vector tuioObjectList = tuioClient.getTuioObjects();
             
             
                 for (int i=0;i<tuioObjectList.size();i++) {
             
             
                    TuioObject tobj = (TuioObject)tuioObjectList.elementAt(i);
             
             
                    stroke(0);
             
             
                    fill(0);
             
             
                    pushMatrix();
             
             
                    translate(tobj.getScreenX(width),tobj.getScreenY(height));
             
             
                     
                    rotate(tobj.getAngle());
             
             
                    rect(-obj_size/2,-obj_size/2,obj_size,obj_size);
             
             
                    popMatrix();
             
             
                    fill(255);
             
             
                    text(""+tobj.getSymbolID(), tobj.getScreenX(width), tobj.getScreenY(height));
             
             
                  }
             
             
               }
             
             
               // these callback methods are called whenever a TUIO event occurs
             
             
               // called when an object is added to the scene
             
             
               void addTuioObject(TuioObject tobj) {
             
             
                      println("add object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle());
             
             
                      menu = controlP5.addButton("menu"+tobj.getSymbolID(), 0, (int)(width*tobj.getX())+35, (int)(height*tobj.getY())-30,20,60);
             
             
               menu.setId(tobj.getSymbolID());
               
             
             
               lista = controlP5.addListBox("video"+tobj.getSymbolID(),(int)(width*tobj.getX())+35, (int)(height*tobj.getY())-30,100,100);
             
             
               lista.setId(tobj.getSymbolID());
               
             
             
               for(int i=0;i<5;i++) {
             
             
                 lista.addItem("video"+(i+1),i);
             
             
               }
             
             
              }
             
             
               // called when an object is removed from the scene
             
             
               void removeTuioObject(TuioObject tobj) {
             
             
                 println("remove object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")");
             
             
              //          menu.remove();
             
             
              //          lista.remove();
             
             
               }
             
             
               // called when an object is moved
             
             
               void updateTuioObject (TuioObject tobj) {
             
             
                 println("update object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()
             
             
                         +" "+tobj.getMotionSpeed()+" "+tobj.getRotationSpeed()+" "+tobj.getMotionAccel()+" "+tobj.getRotationAccel());
             
             
               }
             
             
               // called after each message bundle
             
             
               // representing the end of an image frame
             
             
               void refresh(TuioTime bundleTime) { 
             
             
                 redraw();
             
             
               }
             
             
              
              1  
            
 
            
 
 
           
 
             
 
            