Class Constructor Block not working

So I’m just starting out on processing and I run into this error:
Unexpected token: {
Here is my code:

Person joe;

void setup(){
  size(500,500);
  background(0);
  Person joe = new Person();
}

void draw(){
  joe.display();
}

Class Person(){
  String shape;
  color c;
  Person(){
    c=color((mouseX+mouseY)/4);
    if (mouseX/2<=200){
      shape="rectangle";
    }else{
      shape="ellipse";
    }
  }
  
  void display(mouseX,mouseY){
    stroke(c);
    fill(c);
    if(shape=="rectangle"){
      rect(mouseX,mouseY,10,10);
    }else if(shape=="ellipse"){
      ellipse(mouseX,mouseY,10,10);
    }
  }
}

Can anyone explain this? I thought I followed the tutorial pretty well. Error occurs within the class, at

Person(){
1 Like

Sorry about the indenting, I’m new. How do I get it to show the correct indentation?

You can use the </> sign at the top when posting/editing.

As for the error, that generally means you are missing a } or you have too many {. Though you should have gotten a line number with that error Message i think…

Nevermind, you forgot a ; in void display() after drawing the rect.

Damn semicolons! Still didn’t fix the error though.

Person joe;

void setup() {
  size(500, 500);
  joe = new Person();
}

void draw() {
  background(200, 200, 0);
  joe.display();
}

class Person {
  String shape;
  color c;
  int w = 30;

  Person() {
  }

  void display() {
    c = color((mouseX+mouseY)/4, 0, 200);
    if (mouseX <= width/2)    shape="rectangle";
    else                      shape="ellipse";
    stroke(c);
    fill(c);
    if      ( shape.equals("rectangle") ) rect(mouseX, mouseY, w, w);
    else if ( shape.equals("ellipse") )   ellipse(mouseX, mouseY, w, w);
  }
}

from this working version, you please read back / compare to your original
and see some small changes.
https://processing.org/reference/class.html
https://processing.org/reference/String_equals_.html

you can not just write a class free style,
better start from a working example and change line by line to your target function

one main point is to know that that part of a class

Person() {
 }

works like a setup at creation,
there can ?predefine? may things…
but not use dynamic data like mouseX…

4 Likes

@kll copy-pasted your code into my IDE, same result. Beginning to suspect a bug? Will try restarting IDE.

please post a picture of the full PDE window to see the error after start

@kll

you not copy my full code!!!
please try again ( copy / paste ) into a new PDE window

@kll Same result.

this is not my code, why you not open new window and paste my code
instead try repair your code without knowing what lines are wrong ( 14 )

@kll Sorry, new to processing, that works now. Thanks! (Still struggling to find the effective difference)

1 Like

Well, there are a lot of changes here, and it can be difficult to understand when they aren’t presented one at a time.

As a general strategy, you can use any programming text editor that shows diff to look at the differences between the two. It adds a + in front of added lines and - in front of removed lines.

Diffs can still be hard to read if lots of whitespace changes have been made, so I manipulated things slightly to line them up so you could more easily see some of the things going on in the changes:

Person joe;
 
void setup() {
   size(500, 500);
-  background(0);
-  Person joe = new Person();
+  joe = new Person();
 }
 
void draw() {
+  background(200, 200, 0);
   joe.display();
 }

Okay, move your background command into draw, so that it clears the screen every frame for redrawing. Also, in setup assign joe as the global variable – don’t declare a new local variable Person joe, which only exists inside setup.

-Class Person(){
+class Person {
   String shape;
   color c;
+  int w = 30;

Next, declare a class with the class keyword – lowercase, Class isn’t a thing. Classes also don’t take method arguments, instead have constructors, so drop the (). Add w as a class object variable – this will mean that objects aren’t hard-coded to 10, their size can be changed.

-  Person(){
-    c=color((mouseX+mouseY)/4);
-    if (mouseX/2<=200){
-      shape="rectangle";
-    }else{
-      shape="ellipse";
-    }
+  Person() {
   }

-  void display(mouseX,mouseY){
+  void display() {
+    c = color((mouseX+mouseY)/4, 0, 200);
+    if (mouseX <= width/2)    shape="rectangle";
+    else                      shape="ellipse";
     stroke(c);
     fill(c);

Move all that stuff from the constructor into the display method. mouseX and mouseY are already global variable in Processing, so you don’t need to pass them in, you can just use them.

-    if(shape=="rectangle"){
-      rect(mouseX,mouseY,10,10);
+    if(shape.equals("rectangle")){
+       rect(mouseX,mouseY,w,w);
-    }else if(shape=="ellipse"){
-      ellipse(mouseX,mouseY,10,10);
+    } else if(shape.equals("ellipse")){
+        ellipse(mouseX,mouseY,w,w);
     }
   }
}

Express dimensions in terms of w, not hard-coded with 10. Also, check string equality .equals() – don’t use ==.

4 Likes