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;
}
}
}
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;
}
}