We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Background() not updating
Page Index Toggle Pages: 1
Background() not updating (Read 582 times)
Background() not updating
Dec 15th, 2006, 8:01am
 
Hello
I'm trying to write a sketch that when I press the 'z' key the background fades to a random color. The problem is that when I  iterate through the for loop to in/decrement the rgb values and update the background color it does not change until the for loop is over. Here is my code:

Code:
float floatred = random(0, 255);
int ired = int(floatred);
float floatgreen = random(0, 255);
int igreen = int(floatgreen);
float floatblue = random(0, 255);
int iblue = int(floatblue);
int[] currColor = new int[3];
int totalIter = 765;
int incrementby = 20;
int r = 0;
int g = 0;
int b = 0;
int delayms = 2000;

void setup()
{
size(40, 40);
noStroke();
background(100, 100, 100);
colorMode(RGB, 255);
}

void draw()
{
}

void keyReleased()
{
if(key == 'z' || key == 'Z') {
currColor[0] = ired;
currColor[1] = igreen;
currColor[2] = iblue;
print(ired + " " + igreen + " " + iblue + " ");
floatred = random(0, 255);
ired = int(floatred);
floatgreen = random(0, 255);
igreen = int(floatgreen);
floatblue = random(0, 255);
iblue = int(floatblue);
println(ired + " " + igreen + " " + iblue + " ");
r = currColor[0];
g = currColor[1];
b = currColor[2];
println("PreFor: " + r + " " + g + " " + b + " ");
totalIter = 765;
for(int i=0; i<totalIter; i++) {
//red
if(ired >= currColor[0]) {
totalIter = 0;
if(ired-currColor[0] > totalIter) {
totalIter = ired-currColor[0];
}
if(r!=ired){
r = r + incrementby;
}
}
else {
totalIter = 0;
if(currColor[0]-ired > totalIter) {
totalIter = currColor[0]-ired;
}
if(r!=ired){
r = r - incrementby;
}
}
//green
if(igreen >= currColor[1]) {
if(igreen-currColor[1] > totalIter) {
totalIter = igreen-currColor[1];
}
if(g!=igreen){
g = g + incrementby;
}
}
else {
if(currColor[1]-igreen > totalIter) {
totalIter = currColor[1]-igreen;
}
if(g!=igreen){
g = g - incrementby;
}
}
//blue
if(iblue >= currColor[2]) {
if(iblue-currColor[2] > totalIter) {
totalIter = iblue-currColor[2];
}
if(b!=iblue){
b = b + incrementby;
}
}
else {
if(currColor[2]-iblue > totalIter) {
totalIter = currColor[2]-iblue;
}
if(b!=iblue){
b = b - incrementby;
}
}
if(r > 255) r = 255;
if(r < 0) r = 0;
if(g > 255) g = 255;
if(g < 0) g = 0;
if(b > 255) b = 255;
if(b < 0) b = 0;
totalIter = totalIter/incrementby;
background(r, g, b);
delay(delayms);
println("InFor " + i + ": " + r + " " + g + " " + b + " ");
}
key = 'a';
println("PostFor: " + r + " " + g + " " + b + " ");
}
}
Re: Background() not updating
Reply #1 - Dec 15th, 2006, 10:51am
 
Unfortunately that's how it works.
Nothing is drawn to the screen until draw() (and keypressed/released etc) have all finnished.

So to get the effect you want, in keyReleased(), and then in draw() move one step towards it each time through.
Re: Background() not updating
Reply #2 - Dec 16th, 2006, 1:00am
 
I don't understand what you mean about how to get it to work the way I want.
Re: Background() not updating
Reply #3 - Dec 16th, 2006, 7:47am
 
This was one of the most puzzling things to me when I started off with Processing. Luckily, John helped me out with understanding how the draw() function works. Thanks again John Smiley

Okay, first things first....
You know that the draw() function keeps looping continuously.
Now, whatever code you put into the draw function, will not be displayed on the screen until the draw function loops.

So, within the draw function if you have code like -

.....
void draw()
{
for(int i=0;i<255;i++)
{
 fill(i);stroke(i);rect(0,0,100,100);
}
}

What happens is that the entire for loop is executed, and every rectangle is actually drawn on....maybe a graphics buffer....and the final result of all superimposed images is displayed on the screen when draw() completes its loop.
So for the above code, all you'll see on the screen is a white rectangle.

Even if you place a delay(1000); within the for loop,


void draw()
{
for(int i=0;i<255;i++)
{
 delay(1000);
 fill(i);stroke(i);rect(0,0,100,100);
}
}


you won't see the 254 rectangles being displayed in different shades coz these rectangles are being drawn in the graphics buffer, and not on the screen. Instead, what will happen is,  the delay will be executed 255 times, and you'll have to wait for 255*1 seconds till the for loop completes and all the rectangles to be drawn one over the other in the graphics buffer.

When the for loop is completed, and control reaches the end of the draw() function, whatever has happened in the graphics buffer will be displayed on the monitor.
This functionality works beautifully when you want to superimpose images over one another. You'll love it Smiley

So, if you want to see each rectangle being displayed one by one, you'll have to do something like this...

int i=0;
void draw()
{
stroke(i);fill(i);rect(0,0,100,100);
delay(50);
if (i==255) i=0; else i++;
}

Hope you understood how it works.....I'm sure you'll be able to apply the logic to your program. Smiley
Cheers!! and all the best with your further programming Smiley
Re: Background() not updating
Reply #4 - Dec 16th, 2006, 10:06am
 
Thank you both very much! I got it working. Here is the finished code:
Code:
float floatred = random(0, 255);
int ired = int(floatred);
float floatgreen = random(0, 255);
int igreen = int(floatgreen);
float floatblue = random(0, 255);
int iblue = int(floatblue);
int[] currColor = new int[3];
int totalIter = 765;
int incrementby = 1;
int r = 0;
int g = 0;
int b = 0;
int i = 0;
int done = 1;

void setup()
{
size(400, 400);
noStroke();
colorMode(RGB, 255);
background(100, 100, 100);
}

void draw()
{
totalIter = 765;
if(done==0) {
if(i<totalIter) {
//red
if(ired >= currColor[0]) {
totalIter = 0;
if(ired-currColor[0] > totalIter) {
totalIter = ired-currColor[0];
}
if(r!=ired){
r = r + incrementby;
}
}
else {
totalIter = 0;
if(currColor[0]-ired > totalIter) {
totalIter = currColor[0]-ired;
}
if(r!=ired){
r = r - incrementby;
}
}
//green
if(igreen >= currColor[1]) {
if(igreen-currColor[1] > totalIter) {
totalIter = igreen-currColor[1];
}
if(g!=igreen){
g = g + incrementby;
}
}
else {
if(currColor[1]-igreen > totalIter) {
totalIter = currColor[1]-igreen;
}
if(g!=igreen){
g = g - incrementby;
}
}
//blue
if(iblue >= currColor[2]) {
if(iblue-currColor[2] > totalIter) {
totalIter = iblue-currColor[2];
}
if(b!=iblue){
b = b + incrementby;
}
}
else {
if(currColor[2]-iblue > totalIter) {
totalIter = currColor[2]-iblue;
}
if(b!=iblue){
b = b - incrementby;
}
}
if(r > 255) r = 255;
if(r < 0) r = 0;
if(g > 255) g = 255;
if(g < 0) g = 0;
if(b > 255) b = 255;
if(b < 0) b = 0;
totalIter = totalIter/incrementby;
background(r, g, b);
// println("In \"For\" " + i + ": " + r + " " + g + " " + b + " ");
i++;
if(i==totalIter) {
i = 0;
done = 1;
// println(" Done.");
PFont font;
font = loadFont("Tahoma-12.vlw");
textFont(font, 12);
text("Done.", 20, 20, 70, 70);
}
}
}
}

void keyReleased()
{
if(key == 'z' || key == 'Z') {
currColor[0] = ired;
currColor[1] = igreen;
currColor[2] = iblue;
floatred = random(0, 255);
ired = int(floatred);
floatgreen = random(0, 255);
igreen = int(floatgreen);
floatblue = random(0, 255);
iblue = int(floatblue);
r = currColor[0];
g = currColor[1];
b = currColor[2];
key = 'a';
done = 0;
}
}
Re: Background() not updating
Reply #5 - Dec 16th, 2006, 12:09pm
 
Congrats!! Nice program! Smiley
Page Index Toggle Pages: 1