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
   Syntax
(Moderators: fry, REAS)
   newbie seeking help
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: newbie seeking help  (Read 426 times)
Mythmon


newbie seeking help
« on: Feb 15th, 2004, 7:02am »

im just trying to learn the P5 enviroment, and in doing so im trying to make something that will have a 200x200 window covered in "pixels" that are 3x3 pixels.
 
this i can do ok, the next part im having trouble with though, i have this code
Code:

void setup() {  
  size(200,200);
  int shade;
  for (int i = 1; i <= 200; i+=3)
  {
    for (int j = 1; j <= 200; j+=3)
    {
 shade=int(random(255));
 shade=(shade<<16)+(shade<<8)+shade;
 set(i,j,shade);
 set(i+1,j,shade);
 set(i,j+1,shade);
 set(i+1,j+1,shade);
 set(i+2,j,shade);
 set(i+2,j+1,shade);
 set(i+2,j+2,shade);
 set(i+1,j+2,shade);
 set(i,j+2,shade);
    }
  }
}
 
void mousePressed()
{
  println("mousePressed");
  int shade;
  for (int i = 1; i <= 200; i+=1)
  {
    for (int j = 1; j <= 200; j+=1)
    {
 shade=int(random(255));
 shade=(shade<<16)+(shade<<8)+shade;
 set(i,j,shade);
    }
  }
}

which of course could be alot better, i bet.
 
now the problem is that, the screen does not redraw. it stays the exact same image. is there some fundamental command that im missing or does P5 just not like me?
 
jbk303


Re: newbie seeking help
« Reply #1 on: Feb 15th, 2004, 9:10am »

I am not sure _precisely_ why your code does not work, but I _think_ that it is because you do not use a loop() method. Here is code that sort of works:
 
Code:

void setup() {
  size(200,200);
  int shade;
  for (int i = 1; i <= 200; i+=3)
  {
    for (int j = 1; j <= 200; j+=3)
    {
 shade=int(random(255));
 shade=(shade<<16)+(shade<<8)+shade;
 set(i,j,shade);
 set(i+1,j,shade);
 set(i,j+1,shade);
 set(i+1,j+1,shade);
 set(i+2,j,shade);
 set(i+2,j+1,shade);
 set(i+2,j+2,shade);
 set(i+1,j+2,shade);
 set(i,j+2,shade);
    }
  }
}
 
void loop() {      //this...
  if(mousePressed) {    //and this are added (mousePressed is true when the mouse is pressed.)
    println("mousePressed");
    int shade;
    for (int i = 1; i <= 200; i+=1)
    {
 for (int j = 1; j <= 200; j+=1)
 {
   shade=int(random(255));
   shade=(shade<<16)+(shade<<8)+shade;
   set(i,j,shade);
 }
    }
  }
}

 
However, you will find that the code that runs when the mouse is pressed does not do what you want it to. I leave fixing that part as an excersize for the reader.
 
This sort of brings us to the next part, which is:  
It is always better to write code once and use it twice, like your random-square-drawing code. (Put the code in a function. If you dont know what I mean, http://p5.chronotext.org/tutorials/motion_oop/ should be helpful.) If you need further help, you can email  me at phredthephish@hotmail.com. But remember, it is always better to research on your own and ask for help when you really need it. Your question now is a perfect example of the latter.
 
Best Regards,
Josh
 
benelek

35160983516098 WWW Email
Re: newbie seeking help
« Reply #2 on: Feb 15th, 2004, 12:43pm »

without having a detailed look at your code, i'm just making a guess - but I'd say Josh is right in that you need a loop() method in there. however, you can still do what you want from within the mousePressed() method. just stick these few lines right at the end of your code, Mythmon:
 
Code:
void loop() {
}

 
if i'm right, in that this will solve your problem, then it's because Processing looks for the existance of a loop() method to tell it that the sketch should be kept "living". static until proven lively
 
then again, i could be completely off track.
 
~Jacob
 
kevinP

Email
Re: newbie seeking help
« Reply #3 on: Feb 15th, 2004, 1:41pm »

on Feb 15th, 2004, 12:43pm, benelek wrote:

if i'm right, in that this will solve your problem, then it's because Processing looks for the existance of a loop() method to tell it that the sketch should be kept "living". static until proven lively

 
Hi Jacob,
 
I think you are right, as I had trouble with this at first as well. loop() starts up the "state machine" (term picked up from openGL class).
 
Without loop() there seems to be no "while(1){ }" (or however one does that in Java).
 
Not only that, but writing something like
Code:

boolean one = true;
int cnt = 1;
while(one == true) {
  println(cnt);
  cnt++;
}

 
will freeze Processing.
 
-K
 
 

Kevin Pfeiffer
arielm

WWW
Re: newbie seeking help
« Reply #4 on: Feb 15th, 2004, 1:50pm »

considering only the structural part of the problem, the following rules apply to processing sketches (as far as i'm updated...):
 
 
- if you only want to draw something static:
Code:
size(200, 200);
// your code here

should should work (no setup() is required)
 
 
- if you need to draw something static, but using functions:
Code:
void setup()
{
  size(200, 200);
  myFunction();
}
 
void myFunction()
{
  // your code here
}

then you need setup()
 
 
- finally, there is the draw() method, which is the static counterpart of loop():
Code:
void setup()
{
  size(200, 200);
}
 
void draw()
{
  // your code here
}

i guess it's the best method to use, if you want to avoid headaches...
 

Ariel Malka | www.chronotext.org
toxi_
Guest
Email
Re: newbie seeking help
« Reply #5 on: Feb 15th, 2004, 2:03pm »

a loop like this will freeze the app, because there's no exit condition and println() will fill up the out buffer faster than the stream can be written to the console. but you know, you don't need this classic main loop construct in your sketch as this is being taken care of in the next level up in the class hierarchy (ie. the BApplet class). so i'm a bit confused, why you're mentioning it... if a sketch hasn't got a loop() method the main thread of BApplet (and your sketch) will stop after the 1st iteration and no more events will processed thereafter => static mode.
 
toxi

WWW
Re: newbie seeking help
« Reply #6 on: Feb 15th, 2004, 2:10pm »

btw. this previous post referred to pfeiffer's, not ariel's (who managed to squeeze inbetween
 

http://toxi.co.uk/
kevinP

Email
Re: newbie seeking help
« Reply #7 on: Feb 15th, 2004, 2:22pm »

on Feb 15th, 2004, 2:03pm, toxi_ wrote:
a loop like this will freeze the app, because there's no exit condition and println() will fill up the out buffer faster than the stream can be written to the console.

 
But nothing gets written at all to the console.
 
Quote:

but you know, you don't need this classic main loop construct in your sketch as this is being taken care of in the next level up in the class hierarchy (ie. the BApplet class).

 
I see. I forget that Processing really lives inside another java class. Is BApplet a Processing class that extends Applet (We're on the edge of my known universe, here.)
 
-K
 

Kevin Pfeiffer
Mythmon


Re: newbie seeking help
« Reply #8 on: Feb 15th, 2004, 3:39pm »

hmm, thats odd though, since when even with my previous code, when i clicked i got mousePressed in my console like a i should, oh well, it works now.
 
anyways, for that err, challenge? in the first post, to make it do what i want, it took me 5 minutes (im not that much of a newbie )
Code:

void setup() {  
  size(200,200);  
  int shade;  
  for (int i = 1; i <= 200; i+=3)  
  {  
    for (int j = 1; j <= 200; j+=3)  
    {  
 shade=int(random(255));  
 shade=(shade<<16)+(shade<<8)+shade;  
 set(i,j,shade);  
 set(i+1,j,shade);  
 set(i,j+1,shade);  
 set(i+1,j+1,shade);  
 set(i+2,j,shade);  
 set(i+2,j+1,shade);  
 set(i+2,j+2,shade);  
 set(i+1,j+2,shade);  
 set(i,j+2,shade);  
    }  
  }  
}  
boolean ran=false;
void loop()  
{
  if(mousePressed && ran!=true)  
  {
    int shade;  
    for (int i = 1; i <= 200; i+=1)  
    {  
      for (int j = 1; j <= 200; j+=1)  
      {  
        shade=int(random(255));  
        shade=(shade<<16)+(shade<<8)+shade;  
        set(i,j,shade);  
      }  
    }
    ran = true;  
  }  
}  

 
oh and btw, just adding a blank loop() statement did it as well, and that can be repeated easier, so im gonna use that, now to make it so the drawing uses functions.
« Last Edit: Feb 15th, 2004, 3:45pm by Mythmon »  
fry


WWW
Re: newbie seeking help
« Reply #9 on: Feb 15th, 2004, 3:55pm »

to clarify a bit... (impromptu documentation)
 
p5 looks to see if draw() or loop() is present. the presence of one will shut off the other (preference is given to 'draw' over loop, if both are present).  
 
so the missing thing from the original code was loop(), as was pointed out.
 
something like:
while (true) {
  println("blah");
}
will freeze *any* java-based program, that's not to do with processing. this is because java is threaded, and the thread that is running the "while" loop will starve the other threads from running. other threads handle things like updating the ui or giving the operating system some time to think.  
 
so where "nothing was being written to the console" in pfeiffer's note, that's because the other threads (the gui thread and the os thread) were being starved. the gui thread (the "java awt thread") would be updating the visible area of the console so that you see something, and another thread would give time to the operating system so that it would take notice to update that portion of the screen.  
 
for this reason, java applets that animate must be run inside their own thread which does something equivalent to that "while" loop. so in processing this is hidden from the user (to avoid the extra page of code required to implement it) by simply having a function called loop(), which, when present, will be run continuously. this is one of the advantages of processing, to avoid the mechanics of such thing, in favor of simply building things quickly.
 
finally, anything that you write in processing, unless specified otherwise, will extend BApplet. BApplet extends java.applet.Applet, which is the base class for any applet type thing.
 
Mythmon


Re: newbie seeking help
« Reply #10 on: Feb 15th, 2004, 4:03pm »

heh, im glad you replied so i can post the version of my previos code that uses a function for all the drawing
Code:

int resolution = 200;
 
void setup() {  
  size(200,200);
  drawer (resolution);
}
 
void mousePressed()
{
  if (resolution>=2)
  {
    resolution/=2;
  }
  drawer (resolution);
}
 
void drawer (int rez)
{
  int shade;
  for (int i = 1; i <= 200; i+=rez)
  {
    for (int j = 1; j <= 200; j+=rez)
    {
 shade=int(random(255));
 shade=(shade<<16)+(shade<<8)+shade;
 for (int k = 0; k<(rez); k++)
 {
   for (int l = 0; l<(rez); l++)
   {
     set(i+k,j+l,shade);
   }  
 }
    }
  }
}  
void loop() {}

 
hows that?
 
kevinP

Email
Processing structure
« Reply #11 on: Feb 15th, 2004, 5:51pm »

Thanks (Fry) for the explanation; sorry to have disrupted the original thread topic. Threads is a new topic for me, so didn't think about it.
 
BTW, I also just discovered that to import other classes all one needs to do is to place the import line _after_ "void setup()".
 
-K
 

Kevin Pfeiffer
jbk303


Re: newbie seeking help
« Reply #12 on: Feb 15th, 2004, 11:06pm »

Nice.  
 
(btw, i was just being lazy and didnt want to fix that part of the code at 4:00 AM, sorry. )
 
Mythmon


Re: newbie seeking help
« Reply #13 on: Feb 16th, 2004, 12:17am »

thats ok, it was pretty easy.
 
so, any other ideas of stuff i should try and to help me learn P5?
 
Pages: 1 

« Previous topic | Next topic »