We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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:
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:
It is not going to work. Above the keyword
this
refers to the current instantiated object, which is of typeYoshi
. 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:Then in your main sketch, when you instantiate a Yoshi type of object, you do it this way:
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:
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:
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 forFigureA(...)
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.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
This is the code i have so far class Yoshi {
MainTab
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();
indraw()
. 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