Force screen to update

My question is if I can force processing to update the display window after I call functions like rect() or arc(). So those functions would be called, then I could do something to have them display on screen before draw() finishes. As an example, I have

void draw() {
  fill(255, 0, 0);
  rect(0, 0, 200, 400);
  while (true) {
    //force update here
  }
}

And at the //force update here I want the rectangle to display on screen, but without draw() finishing. I have found that minimizing and reopening the window causes it to display the rectangle. I tried using loadPixels() and updatePixels() but those dont cause it to update.

Hello,

Read this to understand draw() and the related references also:
draw() \ Language (API) \ Processing 3+

There are resources (tutorials, references, examples and more) here:
processing.org

:)

From the draw() docs:

All Processing programs update the screen at the end of draw(), never earlier.

Im assuming this means I cant update in the middle of draw. However, could I force it to minimize and reopen the window?

I don’t know why that would help.

I don’t think you understand how draw() works. You put some code in it to draw things, and the things appear, right?

Well no. It doesn’t stop after it has shown you the things it drew. It starts over again almost immediately. It’s fast! SUPER FAST! It draws things 60 times in one second!

So everything you draw in draw() is only shown to you for 1/60th of a second before it draws those things again.


If you want it to only draw so many things, you can limit the things it draws by other means. One simple - and perhaps the best - way is depending on time. If you call millis(), you can determine how many milliseconds the sketch has been running for. Consider:

void setup(){
  size(600,400);
}

void draw(){
  background(0);
  fill(0,200,0);
  noStroke();
  rect(10, 10, 20, 20);
  if( millis() > 10000 ){ // Ten seconds have passed.
    rect(40, 40, 20, 20);
  }
  if( millis() > 20000 ){ // Twenty seconds have passed.
    rect(70, 70, 20, 20);
  }
  fill(255);
  stroke(255);
  text("" + millis(), 20, 300);
}

Here, initially, one square is being drawn. After ten thousand milliseconds (ten seconds) have gone by, a second square starts being drawn.

A third square appears after twenty seconds.

But the screen is being redrawn 60 times a second! EVERY SECOND! Look how fast the millis() counter goes up!

1 Like

Hello,

The Coding Train has some helpful videos:

And there is Happy Coding:

Other references:

:)

similar idea here

instead of showing the screen you can have different versions of draw() that show different rectangles. Here the variable state is used to distinguish between the states.

The next state is initiated after 5 seconds.

draw() still runs 60 times per second but the screen changes slowly.

Warm regards,

Chrisir

int state;
int timer; 

void setup() {
  size(600, 400);
  timer=millis();
}

void draw() {
  background(0);

  switch(state) {

  case 0: 
    fill(0, 200, 0);
    noStroke();
    rect(10, 10, 20, 20);
    // show text
    fill(255);
    stroke(255);
    text("" + millis(), 20, 300);
    break; 

  case 1: 
    fill( 250, 0, 0);
    rect(40, 40, 20, 20);
    break; 

  case 2:
    fill( 0, 0, 255);
    rect(70, 70, 20, 20);
    break;
    //
  }//switch

  if ( millis()-timer > 5000 ) { // 5 seconds have passed.
    state++;
    timer=millis();
  }//if
}

These replies are very helpful, but not what im looking for. Heres a better example to show what im trying to achieve:

void setup() {
  size(300, 300);
}
void draw() {
  fill(255, 0, 0);
  rect(100, 100, 40, 40);
  //here
  delay(5000); //or some function that takes a while to process
  fill(0, 255, 0);
  rect(120, 120, 40, 40);
}

If you run this, the red and green rects appear at the same time. What im looking for is a way to at the //here make the red rectange appear, then 5000 millis later the green one does. In the code im working with, I dont have delay() but I have a function that takes a few seconds and I want to be able to display the things, then run the function and the things would display without having to wait on the function to finish. In the examples from @TfGuy44 and @Chrisir, draw() finishes over and over, but I have a function that takes a few second and prevents draw() from ending.
I need to force the rect to display before the delay() or function runs., rather then when draw() finishes.

1 Like

Take a look at this for that function:
thread() / Reference / Processing.org

When your thread() is complete you can set a flag (true\false) and use that in draw() as you wish.

References:

I tried it and this worked.

:)