The screen is updating after while loop is completed ? need help

I am using " delay() " inside while loop and making some changes in the screen. I was hoping the changes will be visiable since I am using delay(1000) in every while loop but only the final result is printed on screen . I am not doing anything inside draw function. I am trying to make a event based system where user can interact with just mouse and keyboard.

I want to run while loop such that it will show me the changes I made on screen after every iteration for 1sec then proceed forward.

my code is something like this

while(){
…
…


delay(1000);

..
..

}
2 Likes

The rects(),and every shape,text… is displayed everytime draw() is called, this means everything u change in your while, cant be seen until your while terminates and draw() is called once again.
" draw() is called automatically and should never be called explicitly. All Processing programs update the screen at the end of draw(), never earlier. "
https://processing.org/reference/draw_.html

“The screen only updates when the end of draw() is reached, so delay() cannot be used to slow down drawing. For instance, you cannot use delay() to control the timing of an animation.”
https://processing.org/reference/delay_.html

If you want to “slow” the draw() function, you can use frameRate(num_of_frame_per_sec)
(Default is 60fps)
https://processing.org/reference/frameRate_.html

(I hope I didnt misunderstood your problem)

2 Likes

here a example using a millis timer:

long startT, stopT=1000;
float ang = 0;
float colc;
color rf;

void setup() {
  size(500, 500);
  colc=random(255);
  rf=color(colc, 0, 255-colc);
}

void myTimer() {
  if ( millis() > startT + stopT ) {
    startT = millis();
    println("_next loop_");
    // what else you need to be done now?
    ang++;
    colc=random(255);
    rf=color(colc, 0, 255-colc);
  }
} 

void draw() {
  background(255, 244, 206);  
  myTimer();
  translate(width/2, height/2);
  rotate(ang *TWO_PI/60.0);
  draw_object();
}

void draw_object() {
  stroke(0, 0, 200);
  line(0, 0, 50, 50);
  fill(rf);
  rect( 50, 50, 20, 20);
}

1 Like

@Sky

Sorry for replying late , I was busy implementing max heap and some other stuff in processing. I didn’t knew that screen was updated after draw() xD .

The problem is now solved, thanks you.

No problem, I’m glad you solved the problem