Public void inside draw? Using librarie ControlP5

edited April 2017 in Library Questions

My objective is that when you put the valor it'll draw the function "flor" but, it don't erase the last one that was drawn. How could i do that?

question

Answers

  • Copy the code and paste it here, don't expect us to try and type it all into our own computers. Also, remember to format your code.

  • Was trying to know how i can put the code here with the lines, i'm not that good with foruns

  • Format your code.
    Press the gear icon to edit your post.
    Select your code and press ctrl + o.
    Leave a line above and below the code.

  • import controlP5.*;
    ControlP5 gui;
    
    void setup() {
      size(300, 300);
      gui = new ControlP5(this);
    
      gui.addTextfield("valor")
        .setPosition(20, 20)
        .setSize(120, 20);
    
      gui.addBang("ok")
        .setPosition(150, 20)
        .setSize(30, 20)
        .getCaptionLabel().align(ControlP5.CENTER, 
        ControlP5.CENTER);
    }
    
    void draw() {
    }
    
    void circulo(float x, float y, float r) {
      ellipse(x, y, r, r);
    }
    
    void flor4(float x, float y, float d) {
      circulo(x, y, d);
      circulo(x + (d/2 * sqrt(2)), y - (d/2 * sqrt(2)), d);
      circulo(x - (d/2 * sqrt(2)), y + (d/2 * sqrt(2)), d);
      circulo(x + (d/2 * sqrt(2)), y + (d/2 * sqrt(2)), d);
      circulo(x - (d/2 * sqrt(2)), y - (d/2 * sqrt(2)), d);
    }
    
    void valor(int x ) {
      println(x);
    }
    
    
    public void ok() {
      float x = int(gui.get(Textfield.class, "valor").getText());
      println(x);
      flor4(150, 150, x);
    } 
    ; 
    
  • Done, sorry for trouble

  • edited April 2017 Answer ✓
    public void ok() {
      float x = int(gui.get(Textfield.class, "valor").getText());
      println(x);
      background(128);//or whatever color you use in setup.
      flor4(150, 150, x);
    }   
    

    Rest remains same.

  • Well, that was simply, thank you!! Didn't even thought about that.

  • edited April 2017

    If ok() is called back by ControlP5, there's a risk it's under a different Thread.

    If that is so, we can't directly "draw" anything to sketch's main "canvas" from there! :-O

    Place println(Thread.currentThread()); inside ok() and check its output.

    If it isn't "Thread[Animation Thread,5,main]", ok() is indeed being run under a "foreign" Thread and "drawing" from there may crash the sketch! =;

  • @GoToLoop If that is so, ControlP5 doesn't follow Processing's recommendations, and instead of using Processing's mousePressed() functions, it gets the mouse/key events directly from the JVM. I wouldn't see why a library aimed at beginners would allow such a major problem/flaw to exist. Most beginners wouldn't understand what Threads etc are. No, I believe that the author(s) of ControlP5 would've thought of that much. At the very least, they should've ensured that the functions associated with the different ControlP5 elements would trigger only when it is safe to draw.

  • edited April 2017

    ... If that is so, ControlP5 doesn't follow Processing's recommendations, ...

    I don't think there's any Processing recommendations for 3rd-party libraries but only tips to make them available thru' "Contribution Manager".

    I dunno about ControlP5, but many other libraries, especially those hardware-related, create their own Thread; such as Serial, Movie, etc.

    Probably ControlP5 preferred to hook itself to Processing's via registerMethod() instead.

    If that's the case, they're executed under sketch's own "Animation Thread". And thus it's safe to "draw".

    But just to make sure it is indeed the case, I've advised about checking callbacks out via println(Thread.currentThread());. :-\"

  • edited April 2017

    So none of us really knows what ControlP5 decided.

    Of course Serial and Movie decided to use their own thread, Serial doesn't qualify as "beginner", and not using a seperate thread for Movie would lead to great performance losses.

Sign In or Register to comment.