Help with classes

Hi, so I have a simple sketch of paint to colour in the background of a window. I want to make the colouring tool decrease and increase in size. Why does the code in the function changeSize not work?

Thanks in advance.

Circle c;

void setup() {
  size(600, 400);
  background(255);
  noStroke();
  
  c = new Circle();
}

void draw() {
  
  c.changeSize();
  
  if (mousePressed && (mouseButton == LEFT)) {
  c.display();
  }
  
  if (mousePressed && (mouseButton == RIGHT)) {
    background(255);
  }

}

class Circle {
  
  float d;
  
  Circle() {
    d = 100;
  }
  
  void display() {
    fill(0);
    ellipse(mouseX, mouseY, d, d);
  }
  
  void changeSize() {
    if (d > 100) {
      d = d - 1;
    } else if (d < 10) {
      d = d + 1;
    }
  }
  
}
1 Like

Hi,

You never enter either one of your conditions.

You start with d = 100.

So d > 100 is false and d < 10 is also false.

2 Likes

Hi, thanks for the reply! I understand it now. My code is now the below, but the circle will still not increase in size once d reaches 50?

Circle c;

void setup() {
  size(600, 400);
  background(255);
  noStroke();

  c = new Circle();
}

void draw() {

  if (mousePressed && (mouseButton == LEFT)) {
    c.display();
    c.decrease();
  }

  if (d < 50) {
    c.increase();
  }

  if (mousePressed && (mouseButton == RIGHT)) {
    background(255);
  }
}

float d;

class Circle {
  
  Circle() {
    d = 100;
  }
  
  void display() {
    fill(0);
    ellipse(mouseX, mouseY, d, d);
  }
  
  void decrease() {
    d = d - 1;
  }
  
  void increase() {
    d = d + 1;
  }
  
}
1 Like

Once d >= 50 it can never increase again!

Note the draw method is executed 60 times a second so d starts as 100 but when you press the left mouse button this code is executed

until d == 49 which happens very quickly at which point it is below 50 so is increased to 50 and then stays there for ever.

Note d is the diameter of the circle so should be in the Circle class like this. Note I have changed some of the code to display the diameter in the top left corner so you can experiment with different algorithms and see what works for you.

Circle c;

void setup() {
  size(600, 400);
  background(255);
  noStroke();

  c = new Circle();
}

void draw() {
  // show the circle diameter so we can see what is happening
  fill(255, 255, 0);
  noStroke();
  rect(0, 0, 80, 24);
  fill(0);
  text(c.d, 10, 20); 

  if (mousePressed && (mouseButton == LEFT)) {
    c.display();
    c.decrease();
  }

  if (c.d < 50) { // now d is an attribute of circle
    c.increase();
  }

  if (mousePressed && (mouseButton == RIGHT)) {
    background(255);
  }
}


class Circle {

  float d;

  Circle() {
    d = 100;
  }

  void display() {
    fill(0);
    ellipse(mouseX, mouseY, d, d);
  }

  void decrease() {
    d = d - 1;
  }

  void increase() {
    d = d + 1;
  }
}
2 Likes

Thanks so so much! This is really helpful, although I still wonder if I’ll ever get the hang of this…:grinning:

1 Like

I expect virtually every programmer thought that at one time :smile:

3 Likes