Found out too many { characters with a } to match it

Im a processing user, and in one project im working in, my teacher ask me to start using tabs.But when i try to do so, when i draw all the figures it works with out errors, but im using buttons to change the colours of them in certain range, with the help of "import controlP5.*;" an aleat appears when i run the project "Found out too many { characters with a } to match it", but as far as i know all the { do have a match }. So my question is why is this happening?

Main Tab

yoshi yoshi;
Tree tree;
void setup() {
  size(900, 650);
  smooth();
  yoshi = new yoshi();
  tree= new Tree();
}
void draw() {
  background(255, 255, 255);
  yoshi.run();
  tree.run();
}

Yoshi tab

class yoshi {

  import controlP5.*;
  PShape bot, bot1, bot2;
  PShape bot3, bot4, bot5;
  PShape bot6, bot7, bot8;
  PShape bot9, bot10, bot11;
  PShape bot12, bot13, bot14;
  PShape bot15, bot16;
  ControlP5 cp5;
  int c, c1, c2, c3, c4, c5;
  void yoshi() {
  }
  void run() {
    setup2();
  }
  void setup2() {
    bot=loadShape("cabezaverde.svg");
    bot1=loadShape("ojosblancos.svg"); 
    bot2=loadShape("ojosnegros.svg");
    bot3=loadShape("bocablanco.svg");
    bot4=loadShape("cuerpoblanco.svg");
    bot5=loadShape("cuerpoverde.svg");
    bot6=loadShape("espinasrojas.svg");
    bot7=loadShape("lineacaparazon.svg");
    bot8=loadShape("caparazonrojo.svg");
    bot9=loadShape("pieder.svg");
    bot10=loadShape("suelader.svg");
    bot11=loadShape("piernaizq.svg");
    bot12=loadShape("pieizq.svg");
    bot13=loadShape("suelaizq.svg");
    bot14=loadShape("brazoizq.svg");
    bot15=loadShape("brazoder.svg");
    cp5 = new ControlP5(this);

    cp5.addButton("figureA")
      .setValue(0)
        .setPosition(10, 10)
          .setSize(40, 19)
            ;
    cp5.addButton("figureB")
      .setValue(100)
        .setPosition(55, 10)
          .setSize(40, 19)
            ;
    cp5.addButton("figureC")
      .setPosition(100, 10)
        .setSize(40, 19)
          .setValue(0)
            ;
    cp5.addButton("figureD")
      .setValue(0)
        .setPosition(10, 35)
          .setSize(40, 19)
            ;
    cp5.addButton("figureE")
      .setValue(100)
        .setPosition(55, 35)
          .setSize(40, 19)
            ;
    display();
  }

  void display() {
    bot.disableStyle();
    bot1.disableStyle();
    bot2.disableStyle(); 
    bot3.disableStyle();
    bot4.disableStyle(); 
    bot5.disableStyle();
    bot6.disableStyle(); 
    bot7.disableStyle();
    bot8.disableStyle(); 
    bot9.disableStyle();
    bot10.disableStyle(); 
    bot11.disableStyle();
    bot12.disableStyle(); 
    bot13.disableStyle();
    bot14.disableStyle(); 
    bot15.disableStyle();
    stroke(0, 0, 0);
    strokeWeight(3);
    fill(c2);
    shape(bot, 404, 261, 100, 100);//cabeza-green
    fill(c4);
    shape(bot1, 424, 265, 100, 100);//ojos-white
    fill(c1);
    shape(bot2, 430, 268, 100, 100);//ojos-black
    fill(c4);
    shape(bot4, 436, 305, 100, 100);//cuerpo-white
    fill(c2);
    shape(bot5, 449, 303, 100, 100);//cuerpo-green
    fill(c3);
    shape(bot6, 452, 297, 100, 100);//espinas-red
    fill(c4);
    //shape(bot3, 20, -142, 100, 100);//boca-white
    shape(bot3, 468, 284, 100, 100);
    fill(c4);
    shape(bot7, 467, 314, 100, 100);//caparazon-white
    fill(c3);
    shape(bot8, 470, 312, 100, 100);//caparazon-red
    shape(bot9, 427, 336, 100, 100);//pieder-red
    fill(c5)
    shape(bot10, 427, 349, 100, 100);//suelader-golden
    fill(c2);
    shape(bot11, 457, 328, 100, 100);//piernaizq-green
    fill(c3);
    shape(bot12, 442, 340, 100, 100);//pieizq-red
    fill(c5)
    shape(bot13, 444, 354, 100, 100);//suelaizq-golden
    fill(c2);
    shape(bot14, 444, 311, 100, 100);//brazoizq-green
    shape(bot15, 428, 320, 100, 100);//brazoder-green
  }
  public void figureA(int value) {
    c1 = color(random(0, 27), random(0, 26), random(0, 26));//negro
  }
  public void figureB(int value) {
    c2 = color(random(21, 63), random(198, 208), random(4, 54));//verde
  }
  public void figureC(int value) {
    c3 =color(random(165, 255), random(0, 33), random(0, 33));//rojo
  }
  public void figureD(int value) {
    c4 = color(random(230, 255), random(215, 255), random(215, 255));//blanco
  }
  public void figureE(int value) {
    c5=color(random(203, 255), random(184, 222), random(0, 61));//dorado
  }
}

Tree tab

class Tree {
  int xpos=0, ypos=0;
  int len;
  Tree() {
  }

  void run() {
    display();
  }
  void display() {
    // Start the tree from the bottom of the screen
    translate(width/2, height);
    stroke(0);
    branch(60);
    //translate(width/3, height);
    //stroke(0);
    //branch(60);
    noLoop();
  }

  void branch(float len) {
    strokeWeight(2);

    line(0, 0, 0, -len);
    // Move to the end of that line
    translate(0, -len);

    len *= 0.66;
    // All recursive functions must have an exit condition!!!!
    // Here, ours is when the length of the branch is 2 pixels or less
    if (len > 2) {
      pushMatrix();    // Save the current state of transformation (i.e. where are we now)
      rotate(PI/5);   // Rotate by theta
      branch(len);       // Ok, now call myself to draw two new branches!!
      popMatrix();     // Whenever we get back here, we "pop" in order to restore the previous matrix state

      // Repeat the same thing, only branch off to the "left" this time!
      pushMatrix();
      rotate(-PI/5);
      branch(len);
      popMatrix();
    }
  }
}

Answers

  • First: Imports are always placed in a global scope. In your case, you can place all the imports in your first/main tab. Notice you have an import statement inside one of your classes.

    Second, you need to fix a couple of semi-colon missing: fill(c5)

    Third: Class naming conventions dictates your class names shall follow TitleCase. Can you spot the difference between yoshi vs. Yoshi?

    Now, when you state a constructor for a class, it does not need to specify the return type:

    class Yoshi{
    
       //Contructor
       Yoshi(){ ...}
       //Incorrect: void Yoshi(){..}
    }
    

    There is another problem which I am sure it should have appeared before but... oh well, it needs to be addressed sooner than later. The following statement:

    class Yoshi{
    
       ControlP5 cp5;
       Yoshi(){
         ...
    
          cp5 = new ControlP5(this);
       } 
    
        ....
    }
    

    It is not going to work. Above the keyword this refers to the current instantiated object, which is of type Yoshi. However, when you instantiate a ControlP5 object, you need a reference to a sketch, which in most cases is your current sketch (your main sketch). How to solve the problem? Easy. I show you and then I will explain:

    class Yoshi{
    
       ControlP5 cp5;
       PApplet p;    // NEW
       Yoshi(PApplet parent){
         ...
          p=parent;     //NOTICE
          cp5 = new ControlP5(p);  //NOTICE
       } 
    
        ....
    }
    

    Then in your main sketch, when you instantiate a Yoshi type of object, you do it this way:

    Yoshi masterObj;
    
    void setup(){
       ...
       masterObj=new Yoshi(this);
    }
    

    Explanation: Your class Yoshi now has a handle to your main sketch. Every time you create a new Yoshi object, you pass the reference to your current sketch through the constructor. Inside the constructor you create a ControlP5 object which uses this handle. Now your code should work (Untested... you might have more issues).

    Kf

  • As you said kfrajer i do have one more issue when i run the sketch it shows me this, with figureA, figureB, figureC, figureD and figureE. Advertencia: Controller with name "/figureA" already exists. overwriting reference of existing controller. Do you know why is happening this?

  • this:

      void run() {
        setup2();
      }
    
      void setup2() {
    

    setup2 shouldn't be callled by run but by the constructor Yoshi (formerly yoshi)

  • Can you post your code?

    Not sure why you have the error. I prefer to see what you have now.

    In the meantime, there is another subtle design aspect that you need to become aware of. In short:

    • Class Yoshi has a cp5 object.
    • Inside the class you created a button "FigureA"
    • You also have a function called FigureA(...)

    This is the intended designed of ControlP5 if you are working within a Processing sketch where cp5 and functions like FigureA(...) are exposed to each other. In your current design this is not the case. FigureA(...) resides inside your class. When you click on the button, cp5 is looking for FigureA(...) within the PApplet cp5 is associated to. There are two solutions. I demonstrate the problem and one solution next. Checked the two tag labels below in the comments.

    import controlP5.*;
    
    ControlP5 cp5;
    int myColor = color(255);
    ForeignClass realObj;
    
    
    
    void setup() {
      size(400, 600);
      //fullScreen();
      //orientation(LANDSCAPE);
      noStroke();
    
      realObj=new ForeignClass(this);
    }
    
    void draw() {
      background(myColor);
    }
    
    //public void controlEvent(ControlEvent theEvent) {
    //  println(theEvent.getController().getName());
    //}
    
    
    //  TAG1: THIS WORKS, colorA() and play() are visible by cp5  
    public void colorA(int theValue) {
      println("a button event from colorA: "+theValue);
      myColor = color(random(100, 220));
    }
    
    public void play(int theValue) {
      println("a button event from buttonB: "+theValue);
      myColor = color(250, 0, 200);
    }
    
    
    class ForeignClass {
    
      PApplet p;
    
      ForeignClass(PApplet pa) {
        p=pa;
        setup();
      }
    
      void setup() {
    
        cp5 = new ControlP5(p);
    
        // create a new button with name 'buttonA'
        cp5.addButton("colorA")
          .setValue(0)
          .setPosition(100, 100)
          .setSize(200, 19)
          ;
    
    
        PImage[] imgs = {loadImage("button_a.png"), loadImage("button_b.png"), loadImage("button_c.png")};
        cp5.addButton("play")
          .setValue(128)
          .setPosition(140, 300)
          .setImages(imgs)
          .updateSize()
          ;
      }
    
      //  TAG2: THIS NEXT DOESN'T WORK, colorA() and play() are hidden inside the class. ControlP5 won't be able to find them. 
      //public void colorA(int theValue) {
      //  println("a button event from colorA: "+theValue);
      //  myColor = color(random(100, 220));
      //}
    
      //public void play(int theValue) {
      //  println("a button event from buttonB: "+theValue);
      //  myColor = color(250, 0, 200);
      //}
    }
    

    A second solution is to keep your functions like FigureA(...) inside your class and then use plug methods as shown in this example:

    http://www.sojamo.de/libraries/controlP5/examples/use/ControlP5plugTo/ControlP5plugTo.pde

    Kf

  • edited April 2018

    This is the code i have so far class Yoshi {

      PShape bot, bot1, bot2;
      PShape bot3, bot4, bot5;
      PShape bot6, bot7, bot8;
      PShape bot9, bot10, bot11;
      PShape bot12, bot13, bot14;
      PShape bot15, bot16;
      PApplet p;
      ControlP5 cp5;
      int c, c1, c2, c3, c4, c5;
      Yoshi(PApplet parent) {
        p=parent;     //NOTICE
        cp5 = new ControlP5(p);
      }
      void run() {
        setup2();
      }
      void setup2() {
        bot=loadShape("cabezaverde.svg");
        bot1=loadShape("ojosblancos.svg"); 
        bot2=loadShape("ojosnegros.svg");
        bot3=loadShape("bocablanco.svg");
        bot4=loadShape("cuerpoblanco.svg");
        bot5=loadShape("cuerpoverde.svg");
        bot6=loadShape("espinasrojas.svg");
        bot7=loadShape("lineacaparazon.svg");
        bot8=loadShape("caparazonrojo.svg");
        bot9=loadShape("pieder.svg");
        bot10=loadShape("suelader.svg");
        bot11=loadShape("piernaizq.svg");
        bot12=loadShape("pieizq.svg");
        bot13=loadShape("suelaizq.svg");
        bot14=loadShape("brazoizq.svg");
        bot15=loadShape("brazoder.svg");
        cp5.addButton("figure1")
          .setValue(0)
            .setPosition(30, 30)
              .setSize(40, 19)
                ;
        cp5.addButton("figure2")
          .setValue(100)
            .setPosition(75, 30)
              .setSize(40, 19)
                ;
        cp5.addButton("figure3")
          .setPosition(120, 30)
            .setSize(40, 19)
              .setValue(0)
                ;
        cp5.addButton("figure4")
          .setValue(0)
            .setPosition(30, 55)
              .setSize(40, 19)
                ;
        cp5.addButton("figure5")
          .setValue(100)
            .setPosition(75, 55)
              .setSize(40, 19)
                ;
        display();
        //keyPressed();
      }
    
      void display() {
        bot.disableStyle();
        bot1.disableStyle();
        bot2.disableStyle(); 
        bot3.disableStyle();
        bot4.disableStyle(); 
        bot5.disableStyle();
        bot6.disableStyle(); 
        bot7.disableStyle();
        bot8.disableStyle(); 
        bot9.disableStyle();
        bot10.disableStyle(); 
        bot11.disableStyle();
        bot12.disableStyle(); 
        bot13.disableStyle();
        bot14.disableStyle(); 
        bot15.disableStyle();
        figura(); 
        //fill(0, 128, 0);
        //shape(bot, 404, 261, 100, 100);//cabeza-green
      }
      void figura() {
        stroke(0, 0, 0);
        strokeWeight(3);
        //fill(0, 128, 0);
        fill(c2);
        shape(bot, 404, 261, 100, 100);//cabeza-green
        //fill(255, 255, 255);
        fill(c4);
        shape(bot1, 424, 265, 100, 100);//ojos-white
        //fill(0, 0, 0);
        fill(c1);
        shape(bot2, 430, 268, 100, 100);//ojos-black
        //fill(255, 255, 255);
        fill(c4);
        shape(bot4, 436, 305, 100, 100);//cuerpo-white
        //fill(0, 128, 0);
        fill(c2);
        shape(bot5, 449, 303, 100, 100);//cuerpo-green
        //fill(255, 0, 0);
        fill(c3);
        shape(bot6, 452, 297, 100, 100);//espinas-red
        //fill(255, 255, 255);
        fill(c4);
        shape(bot3, 20, -142, 100, 100);//boca-white
        shape(bot3, 468, 284, 100, 100);
        //fill(255, 255, 255);
        fill(c4);
        shape(bot7, 467, 314, 100, 100);//caparazon-white
        //fill(255, 0, 0);
        fill(c3);
        shape(bot8, 470, 312, 100, 100);//caparazon-red
        shape(bot9, 427, 336, 100, 100);//pieder-red
        //fill(255, 255, 0);
        fill(c5);
        shape(bot10, 427, 349, 100, 100);//suelader-golden
        //fill(0, 128, 0);
        fill(c2);
        shape(bot11, 457, 328, 100, 100);//piernaizq-green
        //fill(255, 0, 0);
        fill(c3);
        shape(bot12, 442, 340, 100, 100);//pieizq-red
        //fill(255, 255, 0);
        fill(c5);
        shape(bot13, 444, 354, 100, 100);//suelaizq-golden
        //fill(0, 128, 0);
        fill(c2);
        shape(bot14, 444, 311, 100, 100);//brazoizq-green
        shape(bot15, 428, 320, 100, 100);//brazoder-green
      }
      public void figure1(int value) {
        c1 = color(random(0, 27), random(0, 26), random(0, 26));//negro
      }
      public void figure2(int value) {
        c2 = color(random(21, 63), random(198, 208), random(4, 54));//verde
      }
      public void figure3(int value) {
        c3 =color(random(165, 255), random(0, 33), random(0, 33));//rojo
      }
      public void figure4(int value) {
        c4 = color(random(230, 255), random(215, 255), random(215, 255));//blanco
      }
      public void figure5(int value) {
        c5=color(random(203, 255), random(184, 222), random(0, 61));//dorado
      }
    }
    

    MainTab

    import controlP5.*;
    
    Yoshi masterObj;
    
    void setup() {
      size(900, 650);
      smooth();
      masterObj=new Yoshi(this);
    }
    void draw() {
      background(255, 255, 255);
      masterObj.run();
    }
    
  • Unfortunately I don't have the shapes so I can run your code.

    However, there is one change you need to implement, which Chrisir already mentioned before: Move setup2() from `run() to Yoshi's constructor.

    As a matter of fact, you do not need to call masterObj.run(); in draw(). You should be able to see the buttons this time. However the buttons will not work as you need to address the technicality I mentioned about cp5 and its functions. As I can foresee going back and forth related to this topic, I suggest you abandon the concept of Yoshi class for now and get the code working using a more simple concept. Check it for yourself below. If you still want to use the Yoshi class, in case you want to build multiple characters, then you need to explore the concept of plugs offered by ControlP5. However, for testing, do not use your Yoshi class but a simple example. For instance, the one I provided immediately above where it doesn't use any external resources.

    Kf

    import controlP5.*;
    
    PShape bot, bot1, bot2;
    PShape bot3, bot4, bot5;
    PShape bot6, bot7, bot8;
    PShape bot9, bot10, bot11;
    PShape bot12, bot13, bot14;
    PShape bot15, bot16;
    
    ControlP5 cp5;
    int c, c1, c2, c3, c4, c5;
    
    void setup() {
      size(900, 650);
      smooth();
      setup2();
    }
    void draw() {
      background(255, 255, 255);
    }
    
    void setup2() {
      bot=loadShape("cabezaverde.svg");
      bot1=loadShape("ojosblancos.svg"); 
      bot2=loadShape("ojosnegros.svg");
      bot3=loadShape("bocablanco.svg");
      bot4=loadShape("cuerpoblanco.svg");
      bot5=loadShape("cuerpoverde.svg");
      bot6=loadShape("espinasrojas.svg");
      bot7=loadShape("lineacaparazon.svg");
      bot8=loadShape("caparazonrojo.svg");
      bot9=loadShape("pieder.svg");
      bot10=loadShape("suelader.svg");
      bot11=loadShape("piernaizq.svg");
      bot12=loadShape("pieizq.svg");
      bot13=loadShape("suelaizq.svg");
      bot14=loadShape("brazoizq.svg");
      bot15=loadShape("brazoder.svg");
    
      cp5 = new ControlP5(this);
    
      cp5.addButton("figure1")
        .setValue(0)
        .setPosition(30, 30)
        .setSize(40, 19)
        ;
      cp5.addButton("figure2")
        .setValue(100)
        .setPosition(75, 30)
        .setSize(40, 19)
        ;
      cp5.addButton("figure3")
        .setPosition(120, 30)
        .setSize(40, 19)
        .setValue(0)
        ;
      cp5.addButton("figure4")
        .setValue(0)
        .setPosition(30, 55)
        .setSize(40, 19)
        ;
      cp5.addButton("figure5")
        .setValue(100)
        .setPosition(75, 55)
        .setSize(40, 19)
        ;
      display();
      //keyPressed();
    }
    
    void display() {
      bot.disableStyle();
      bot1.disableStyle();
      bot2.disableStyle(); 
      bot3.disableStyle();
      bot4.disableStyle(); 
      bot5.disableStyle();
      bot6.disableStyle(); 
      bot7.disableStyle();
      bot8.disableStyle(); 
      bot9.disableStyle();
      bot10.disableStyle(); 
      bot11.disableStyle();
      bot12.disableStyle(); 
      bot13.disableStyle();
      bot14.disableStyle(); 
      bot15.disableStyle();
      figura(); 
      //fill(0, 128, 0);
      //shape(bot, 404, 261, 100, 100);//cabeza-green
    }
    
    
    void figura() {
      stroke(0, 0, 0);
      strokeWeight(3);
      //fill(0, 128, 0);
      fill(c2);
      shape(bot, 404, 261, 100, 100);//cabeza-green
      //fill(255, 255, 255);
      fill(c4);
      shape(bot1, 424, 265, 100, 100);//ojos-white
      //fill(0, 0, 0);
      fill(c1);
      shape(bot2, 430, 268, 100, 100);//ojos-black
      //fill(255, 255, 255);
      fill(c4);
      shape(bot4, 436, 305, 100, 100);//cuerpo-white
      //fill(0, 128, 0);
      fill(c2);
      shape(bot5, 449, 303, 100, 100);//cuerpo-green
      //fill(255, 0, 0);
      fill(c3);
      shape(bot6, 452, 297, 100, 100);//espinas-red
      //fill(255, 255, 255);
      fill(c4);
      shape(bot3, 20, -142, 100, 100);//boca-white
      shape(bot3, 468, 284, 100, 100);
      //fill(255, 255, 255);
      fill(c4);
      shape(bot7, 467, 314, 100, 100);//caparazon-white
      //fill(255, 0, 0);
      fill(c3);
      shape(bot8, 470, 312, 100, 100);//caparazon-red
      shape(bot9, 427, 336, 100, 100);//pieder-red
      //fill(255, 255, 0);
      fill(c5);
      shape(bot10, 427, 349, 100, 100);//suelader-golden
      //fill(0, 128, 0);
      fill(c2);
      shape(bot11, 457, 328, 100, 100);//piernaizq-green
      //fill(255, 0, 0);
      fill(c3);
      shape(bot12, 442, 340, 100, 100);//pieizq-red
      //fill(255, 255, 0);
      fill(c5);
      shape(bot13, 444, 354, 100, 100);//suelaizq-golden
      //fill(0, 128, 0);
      fill(c2);
      shape(bot14, 444, 311, 100, 100);//brazoizq-green
      shape(bot15, 428, 320, 100, 100);//brazoder-green
    }
    public void figure1(int value) {
      c1 = color(random(0, 27), random(0, 26), random(0, 26));//negro
    }
    public void figure2(int value) {
      c2 = color(random(21, 63), random(198, 208), random(4, 54));//verde
    }
    public void figure3(int value) {
      c3 =color(random(165, 255), random(0, 33), random(0, 33));//rojo
    }
    public void figure4(int value) {
      c4 = color(random(230, 255), random(215, 255), random(215, 255));//blanco
    }
    public void figure5(int value) {
      c5=color(random(203, 255), random(184, 222), random(0, 61));//dorado
    }
    
Sign In or Register to comment.