FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   i just don't get it
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: i just don't get it  (Read 611 times)
michael

Email
i just don't get it
« on: Dec 12th, 2002, 6:44am »

okay, can someone tell me why this doesn't draw 6 white boxes on the screen?  i feel like a putz but i really don't get why it doesn't work.
 
Code:

int i;
 
void setup()
{
  size(300,300);
  background(49,114,151);
  stroke(255);
   
  for (i=10;i<120;i+=20)
  {
    rect(20,i,10,10);
  }
}
 

In the beginning, the universe was created. This made a lot of people very angry, and has been widely regarded as a bad idea.

-- Douglas Adams in The Hitchhiker's Guide to the Universe.
REAS


WWW
Re: i just don't get it
« Reply #1 on: Dec 12th, 2002, 11:28am »

ah, very interesting ... i'm not sure if that should conceptually work or not.
 
here is one way to get it to work:
 
Code:
size(300,300);  
background(49,114,151);  
stroke(255);  
for (int i=10;i<120;i+=20) {  
   rect(20,i,10,10);  
}  

 
and here is another:  
 
Code:
void setup()  
{  
  size(300,300);  
  background(49,114,151);  
  stroke(255);  
}
 
void loop()  
{
  for (int i=10;i<120;i+=20)  
  {  
    rect(20,i,10,10);  
  }  
}  

 
the setup() method only gets the program going. it is called once when the program is run, but it doesn't draw to the screen when it exits. it does set the background color but nothing else.
« Last Edit: Dec 12th, 2002, 11:29am by REAS »  
michael

Email
Re: i just don't get it
« Reply #2 on: Dec 12th, 2002, 2:13pm »

ahhh. okay. thanks reas.
 

In the beginning, the universe was created. This made a lot of people very angry, and has been widely regarded as a bad idea.

-- Douglas Adams in The Hitchhiker's Guide to the Universe.
michael

Email
Re: i just don't get it
« Reply #3 on: Dec 12th, 2002, 6:30pm »

just curious... how is performance affected if i have a set of boxes that i want drawn once and, having it in loop() like it is, it gets drawn over and over?  
 
i guess maybe for the size of any project i'll be doing, probably not a lot but it seems like a waste of processing to draw something over and over what only needs to be drawn once.
 

In the beginning, the universe was created. This made a lot of people very angry, and has been widely regarded as a bad idea.

-- Douglas Adams in The Hitchhiker's Guide to the Universe.
REAS


WWW
Re: i just don't get it
« Reply #4 on: Dec 12th, 2002, 9:12pm »

if you're making static images, it's best to reference the first of the two examples posted above. when draw commands such as rect() are places within loop() they are drawn over and over, which is very inefficient if you only need them to draw once.
 
benelek

35160983516098 WWW Email
Re: i just don't get it
« Reply #5 on: Jan 7th, 2003, 9:08am »

the problem is that if you want to do anything complicated and non-static, you have to use the setup() and loop() format.
 
proce55ing simply won't let you use setup() without loop(), or even setup() with loop() and still draw something to the screen outside of loop().
 
BUT what you can do, is use the setup() and loop() format, and specify noBackground(); in setup. then you can do all the drawing you want outside loop(), as long as it is within another event call, such as mouseMoved() or mousePressed(). the shape is drawn once, and since the background is not refreshed, whatever is drawn, stays drawn. when you want to refresh the screen, simply draw a blank rectangle, rect(0,0,width,height);
 
for instance:
 
Code:

 
// testing pixel drawing.
 
void setup() {
  size(300,300);
  stroke(#000000);
  noBackground();
}
 
void loop() {
}
 
void mouseDragged() {
    line(pmouseX,pmouseY,mouseX,mouseY);
}
 
void keyPressed() {
  if(key == ' ') { rect(0,0,width,height); }
}
 

 
run the code, and drag the mouse to draw. press spacebar to "refresh".
 
-jacob
 
Pages: 1 

« Previous topic | Next topic »