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 & HelpPrograms › silly simple program doesn't work
Page Index Toggle Pages: 1
silly simple program doesn't work (Read 785 times)
silly simple program doesn't work
Sep 26th, 2007, 5:59am
 
Working on Mac OS 10.4 Processing 0125


The following program should, in my humble opinion, flash white and then black square for ever and ever within the draw loop.

Characters are printed every second but squares get stuck on black after one iteration. What is going on


Apologies in advance if I am missing something stupid :)

        d



Quote:


void setup()
{
 print("setup \n");
 size(500,500);  
}


void draw()
{
 fill(0);
 rect(0,0,200,300);
 delay(1000);
 print("a");
 
 fill(255);
 rect(0,0,200,300);
 delay(1000);
 print("b");
}


Re: silly simple program doesn't work
Reply #1 - Sep 26th, 2007, 6:31am
 
Try this Wink

Quote:
boolean bw;

void setup()
{
 print("setup \n");
 size(500,500);  
bw = true;
}


void draw()
{
 if(bw){
   fill(0);
   bw = false;
   print("a");
      }
   else if(!bw){
   fill(255);
   bw = true;
   print("b");
     }
 rect(0,0,200,300);
 delay(1000);
}
Re: silly simple program doesn't work
Reply #2 - Sep 26th, 2007, 6:45am
 
thank you!

But really, what is wrong with the original code? I am completely at a loss here Sad
Re: silly simple program doesn't work
Reply #3 - Sep 26th, 2007, 8:48am
 
There is nothing wrong.

The first example i posted in this topic is keeping the same shape, but just changing its color.

The reason why your code is not working as you expect is because draw() is diplaying objects as layer (check the example below).

Quote:
void setup()
{
  size(500,500);  
 print("setup \n");
 frameRate(1);
}
 
void draw()
{
background(255,0,0); // in background, layer 0.
Ellipse();// in middle, layer 1.
Rectangle();// in front, layer 2.
}

void Ellipse()
{
 fill(255);
 ellipse(random(200),200,200,200);
 print("a");
//  delay(1000);
}

void Rectangle()
{
 fill(0);
 rect(0,random(200),200,300);
 print("b");
//  delay(1000);
}



The ellipse() called in draw() will be drawn the first, then the rectangle() will be drawn on top of the ellipse().

When draw() will reach the end, it will restart drawing:

the background first
the ellipse on top of the background
the rectangle on top of the background and the ellipse.

Quote:
   BACK
----------
background()
Ellipse()
Rectangle()
----------
  FRONT


if you want to switch from one shape to another you must use boolean to activate/disactivate the objects or switch their position in draw().

Here is an example of switching objects layer position by using boolean.

Quote:
boolean rectanglefront;

void setup()
{
  size(500,500);  
 frameRate(1);
rectanglefront = true;
}


void draw()
{
background(255,0,0);
if(rectanglefront){
Ellipse();
Rectangle();
rectanglefront = false;
 print("rectangle is in front  ");
}else{
Rectangle();
Ellipse();
rectanglefront = true;
 print("ellipse is in front  ");
}
}
void Ellipse()
{
 fill(255);
 ellipse(random(200),200,200,200);
}

void Rectangle()
{
 fill(0);
 rect(0,random(200),200,300);
}
Re: silly simple program doesn't work
Reply #4 - Sep 26th, 2007, 7:54pm
 
Digi-D wrote on Sep 26th, 2007, 5:59am:
Characters are printed every second but squares get stuck on black after one iteration. What is going on

Apologies in advance if I am missing something stupid :)


8)

jaylfk is right but i think a simpler way of thinking about it is that you only get to see what you've drawn at THE END of the draw function, not in the middle. print something white then overwrite it with black and you will only ever see the black regardless of whether you pause in the middle.
confused me no end...
Re: silly simple program doesn't work
Reply #5 - Sep 27th, 2007, 12:00am
 
Thank you both for explaining this behavior.

The way I am jamming this in my head right now is:

Pixels get rendered out once and only once for every draw loop cycle --> We write to the screen buffer and it gets pushed to the screen every draw cycle.


There is something a bit unintuitive here, but I can live with it since a get so many other goodies from processing Smiley
Page Index Toggle Pages: 1