How to use the draw() function in multiple tabs

edited August 2017 in How To...

So I'm trying out using multiple tabs for the first time ever, and I'm not sure proper tab etiquette, but is there a way to make it so I can use functions like draw() in multiple tabs? It gives me the error "Duplicate method draw() in tab1".

Answers

  • Answer ✓

    all tabs are just concatenated together before the while thing is compiled.

    if you have draw() in two different tabs then you have two draw()s, which is why you get the error.

    perhaps you mean to use classes?

  • I'm not exactly sure. I wanted my main tab to get everything basic out of the way, a second tab to handle the aesthetics of what I was making, and a third tab to handle all the little objects and whatnot.

  • I'm not super familiar with javascript and processing coding. Most of what I've done in the past has been in C# on Unity so, pardon if there's obvious things I'm missing.

  • So I put it into a class function, specifically class Button {}, and it gets rid of the error but it doesn't draw anything. So how would I instantiate a class?

  • Nevermind! I did it! I am a genius! Go me!! For those curious, I wrapped my code in class Button {}, then in my main script I wrote Button button = new Button(); button.draw(); and it works. Thanks!

  • edited August 2017 Answer ✓

    As has been said, internally all tabs are stitched together and make one code

    The tabs are just convenient for the programmer not for processing

    So you might as well copy all your tabs here, so we can take a closer look

    Anyway the thing is no matter if you use tabs or not you can have only one setup and one draw and in fact all function names must be unique.

    So in your case, a simple name change like drawButton would have done the trick

    But the usage of classes is best of course.

    Besides I use tabs all the time

    Chrisir

  • edited August 2017 Answer ✓

    @Tallen121 -- Congratulations on solving your problem!

    If you are getting into objects and classes in Processing(Java) for the first time, this may be helpful:

  • A more advanced technique is using undocumented PApplet::registerMethod() in order to hook your class' own draw() method as a sketch's callback too: :ar!

    /** 
     * Registered Class (v1.0)
     * GoToLoop (2017/Aug/28)
     *
     * Forum.Processing.org/two/discussion/23950/
     * how-to-use-the-draw-function-in-multiple-tabs#Item_8
     */
    
    void setup() {
      size(600, 600);
      smooth(3);
      frameRate(1);
    
      colorMode(RGB);
      blendMode(REPLACE);
      ellipseMode(CENTER);
    
      fill(-1);
      stroke(0);
      strokeWeight(2.5);
    
      new Button();
    }
    
    void draw() {
      background((color) random(#000000));
    }
    
    public class Button {
      Button() {
        registerMethod("draw", this);
      }
    
      void draw() {
        ellipse(width>>1, height>>1, width>>1, height>>1);
      }
    }
    

    Although doing so, your sketch automatically becomes incompatible w/ Pjs library's API and can't be deployed to the web! :o3
    http://ProcessingJS.org/reference/

  • edited August 2017 Answer ✓

    ... so I can use functions like draw() in multiple tabs?

    All ".pde" tabs are concatenated together and wrapped up as members of a PApplet subclass before compilation. $-)

    However, if you rename ".pde" to ".java", that tab isn't a PApplet subclass anymore and follows Java's full syntax now! ~O)

Sign In or Register to comment.