We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey everyone,
I'm trying to redraw my window after a certain delay and I've already written a working method: (Shortened to simplify it)
void setup() {
size(1000, 100);
fill(0, 0, 0);
show();
delay(1000);
fill(255, 0, 0);
show();
delay(1000);
fill(0, 255, 0);
show();
delay(1000);
fill(0, 0, 255);
show();
}
void show() {
rect(0, 0, width, height);
}
void delay(int wait) {
int startTime = millis();
while(millis() - startTime < wait) {
}
}
The only problem is that the program won't redraw properly after each delay but only shows the end result. How do I fix this?
Thanks in advance
PS: Sorry if there are any grammar mistakes :)
Answers
https://forum.processing.org/two/discussion/8087/what-are-setup-and-draw
https://forum.processing.org/two/discussion/8085/i-display-images-in-sequence-but-i-see-only-the-last-one-why
@Kaes3kuch3n --
In Processing the normal method for redrawing after a set delay (as in animation) is to use
draw()
and specify the delay in setup withframeRate()
.If you have a sketch with no draw() loop the screen will be updated when the sketch returns. If you have a draw() loop the screen will update each time draw() returns.
While I don't think you want to do animation by specifying every single frame, here is an example of your colored rectangles only using frame timing and loading of per-frame colors:
The problem is that I need the delays to be a) variable, and b) exact so I can't use frameRate() (at least as far as I know).
Is there any way to draw something and change it's color after a certain amount of time multiple times in a row?
If you need variable delays: embed your drawing code in draw() running at a high frameRate(), and skip frames until you get to your next frame using a timer. You still need for draw() to return in order to update the screen.
Why does my code work when I remove a delay? (It shows the red rectangle when I remove the first delay and so on)
Okay, this is my whole project: I'm trying to build (kind of) a simulator for an rgb led strip which uses a specific library. I don't own this led strip but I'm trying to write a program for it.
1st tab:
2nd tab:
3rd tab:
My goal is to write a program which can use almost the same code like the led library but instead of lighting up leds it's supposed to show the colors on my screen
As others have tried to communicate, draw() only updates the screen when the function ends. So using delay() (even your version of it) in draw() is not what you want. Instead, have a state variable that determines which color should be drawn, and change the value stored in that state variable by checking the elapsed time (using millis()). Like so: