[controlP5] RESOLVED - simple object counting in textarea

edited December 2014 in Library Questions

Hi all.

I need to display in a textarea the number of objects in canvas. These objects change often in time.

I tried with Behaviour, but Textarea class doesn't inherit setBehavior() method like e.g. the Slider class (why!?).

I tried with something like

void updateObjectNumber(){ Textarea textarea = ((Textarea)controlP5.getController("objectNumber")); textarea.setText(objects.size()); }

but it raise a casting error.

How can i do?

Thanks a lot!

Tagged:

Answers

  • code in previous post fixed:

    void updateBoidsNumber(){
        Textarea text = ((Textarea)gui.getController("boidsNumber"));
        text.setText(flock.boids.size()+" BOIDS");
    }
    
  • Answer ✓

    Hi, I posted a working example below. To access the Textarea, use gui.get(Textarea.class, "boidsNumber") this will make ControlP5 return a controller of a particular class (if matching the original). Instead of using a Textarea you could also use a Textlabel.

    import controlP5.*;
    
    ControlP5 gui;
    
    void setup() {
      size(800,400);
      gui = new ControlP5(this);
      gui.addTextarea("boidsNumber").setPosition(100,100).setSize(100,50);
      // instead of using a Textarea you can also use a textlabel
      gui.addTextlabel("boidsNumberLabel").setPosition(100,200);
    }
    
    void draw() {
      background(40);
      updateBoidsNumber(frameCount);
    }
    
    void updateBoidsNumber(int theValue){
        Textarea text = (gui.get(Textarea.class, "boidsNumber"));
        text.setText(theValue+" BOIDS");
        gui.get(Textlabel.class, "boidsNumberLabel").setText(theValue+" BOIDS");
    }
    
  • Yes! Thanks so much.

    Resolved.

  • @calcolo

    If your question has been answered then please mark the discussion as 'Answered' when prompted to do so. This makes it clearer in the listings. :)

Sign In or Register to comment.