I am totally new to Processing and I am trying to run a program that draws a circle if the value of a variable is less than 20 and an ellipse if the value is greater than 20. The problem is that the screen is not getting updated if I try to use the value of an array. It just take one value and it does not change. If I just create a random number then it works. I also see that when the screen does not update the values in the terminal do show the correct numbers, so the program runs without an apparent error. (All those println's are just for debugging purposes...)
void draw () {
delay(500);
for (int i = 0; i < totalData.size(); i++) {
record2 = totalData.getJSONObject(i);
distance2 = int(record2.getString("field1"));
//float distance2 = random(15, 25);
println("distance2 is " + distance2);
if (distance2 <= 20) {
println("triangle");
background(500);
triangle(30, 75, 58, 20, 86, 75);
println("distance2 at if " + distance2);
} else{
background(1700);
println("ellipse");
ellipse(56, 46, 55, 55);
println("distance at else is " + distance2);
}
println("distance2 at end " + distance2);
}
}
@John Coleman comment is on spot, but I'll try to give more details so that you understand the problem properly:
First check the draw()
function documentation. It says:
Called directly after setup(), the draw() function continuously executes the lines of code contained inside its block until the program is stopped or noLoop() is called. draw() is called automatically and should never be called explicitly. All Processing programs update the screen at the end of draw(), never earlier.
So you draw()
function is already a loop, what you want to do is to create the i
variable outside of the function and increment it each time draw()
is called:
int i = 0;
void draw () {
record2 = totalData.getJSONObject(i);
i++;
distance2 = int(record2.getString("field1"));
println("distance2 is " + distance2);
if (distance2 <= 20) {
println("triangle");
background(500);
triangle(30, 75, 58, 20, 86, 75);
println("distance2 at if " + distance2);
} else{
background(1700);
println("ellipse");
ellipse(56, 46, 55, 55);
println("distance at else is " + distance2);
}
println("distance2 at end " + distance2);
}
This should give you the expected behavior.
The doc also says:
The number of times draw() executes in each second may be controlled with the frameRate() function.
You most probably want to use that instead of delay()
to control your frame rate.
If you want to get started with processing, reading the doc is always a good start and I would also recommend checking Daniel Shiffman youtube channel this is probably the most complete and didactic resource about processing you'll find online.