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 ==.