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 › Making Draw Wait
Page Index Toggle Pages: 1
Making Draw Wait (Read 2748 times)
Making Draw Wait
Sep 6th, 2008, 8:07pm
 
Hi folks,

What I would like to do is to be able to get draw to reach a predefined point and then stop and wait for input from me (a mouse press for example) before proceeding.

I tried to do it with the following test code but all I get is a blank screen and no output. PS - this code also sends the Java window off to never-never land
Code:

int loopEnd=1000;
boolean wait4mouse=true;
int strokC=0;
void setup(){
size(400,400);
background(255); strokeWeight(2); smooth();
stroke(strokC);
} // end setup


void draw() {
for(int i=1; i < loopEnd; i++) {
ellipse(random(0,width),random(0,height),4,4);
} // end loop

while(wait4mouse) {
if (mousePressed == true) {
strokC+=5;
stroke(strokC);
background(255);
println("****** NEW SCREEN ******");
wait4mouse=false;
} // end mousepressed
} // end wait4mouse loop

wait4mouse=true;
} // end draw


I click in the window repeatedly but nothing happens.

I also tried doing this using the mousePressed() method.

So what am I missing here?

Thanks.

Jim
Re: Making Draw Wait
Reply #1 - Sep 6th, 2008, 9:48pm
 
Hi Jim,

I may misunderstand what you are trying to do so if you feel I am telling you obvious things that you already know please don't take offense!

First of all, check the loop() reference, it may show you exactly what you want.

Quote:
Hi folks,

What I would like to do is to be able to get draw to reach a predefined point and then stop and wait for input from me (a mouse press for example) before proceeding.


OK, do you want the same thing drawn each time the sketch runs or are you happy to have random things drawn?

Quote:
Code:

int loopEnd=1000;
boolean wait4mouse=true;
int strokC=0;
void setup(){
 size(400,400);
 background(255); strokeWeight(2); smooth();
 stroke(strokC);
} // end setup



background() must be called each time the draw method is called if you want the screen to be redrawn. I think the unspecified framerate is 60 frames a second, but this may have changed.

Quote:
Code:


void draw() {
 for(int i=1; i < loopEnd; i++) {
   ellipse(random(0,width),random(0,height),4,4);
 } // end loop



This will draw 1000 random ellipses before the following lines are reached, and this is expected to happen 60 times a second. Is this what you want?
Quote:
Code:

while(wait4mouse) {
 if (mousePressed == true) {
   strokC+=5;
   stroke(strokC);
   background(255);
   println("****** NEW SCREEN ******");
   wait4mouse=false;
   } // end mousepressed
} // end wait4mouse loop

wait4mouse=true;
} // end draw  



This final while could cause some issues, I think nothing will be displayed while you wait for the mouse press, but as soon as you have it it blanks what has already been drawn (but not displayed). I could be wrong. Here is my version which I think does what you want. I try to avoid for and while loops in the draw that could potentially freeze everything.

Here is a reworked version of the code you posted:

Quote:
boolean paused = true;

void setup()
{
  size(400,400);
  background(255);
  strokeWeight(2);
  smooth();
}

void draw()
{
  stroke(random(255), random(255), random(255));  
  ellipse(random(0,width),random(0,height),4,4);


void keyPressed()
{
  paused = !paused;
  if(paused)
    noLoop();
  else
    loop();
}




Regards,

Stephen

Re: Making Draw Wait
Reply #2 - Sep 6th, 2008, 10:02pm
 
There's also a nifty little function called delay(), which takes and int of milliseconds for how long it should wait before drawing the next screen.
Also, by setting the while loop and having an if(mousePressed), you're basically waiting each frame for the mouse to be pressed... which isn't a good idea

you might as well just draw a blank screen.. and if the mouse is pressed, draw something..

-seltar
Re: Making Draw Wait
Reply #3 - Sep 6th, 2008, 10:59pm
 
Thanks, I didn't think of delay(). Sadly, using delay() seems to cause a thread exception no matter how I use it, but I suspect it's my unusual setup that's causing it.
Re: Making Draw Wait
Reply #4 - Sep 6th, 2008, 11:03pm
 
Hi Stephen,

Thank you for your response. In reply to your questions:

Q1)do you want the same thing drawn each time the sketch runs or are you happy to have random things drawn?

A1) No, within either a mousepressed loop or mousepressed() method, once I press the button I want to load in new parameters and then redraw using those values.  The code I provided here is just a skeleton to see if I could get the basic process down.

Q2) background() must be called each time the draw method is called if you want the screen to be redrawn.

A2) I do want the screen redrawn anew for each set of parameters.  

Q3)  This will draw 1000 random ellipses before the following lines are reached, and this is expected to happen 60 times a second. Is this what you want?

A3) I want draw to execute the statements contained within the loop exactly one time (for the example to draw 1000 ellipses) and then just stop and do nothing further until I press the mouse button. Only once I've pressed the button (and load new values as a consequence) do I want draw() to start drawing again.

Q4)  I try to avoid for and while loops in the draw that could potentially freeze everything.

A4) Not what I was hoping to hear.

Q5) Here is a reworked version of the code you posted:

A5) The problem with your code is you have no control over how many things are drawn (ellipses in this case). That is the control that I need. I want draw() to draw exactly N things and then stop and wait for me to tell it to proceed.

As to anything being drawn on the screen, I just get a blank screen and Java hangs. Also putting the mouse checking code before the drawing statements makes no difference: nothing gets drawn, the print message doesn't print after a mouse click, and I have to close down all open processing windows to get the java window to close.

I fear that I am going to have to abandon this approach and rely on a mouse click to swap to the noloop mode and then a key press to load the new parms and then another mouse click to go back into loop mode.

Thanks Stephen.




Re: Making Draw Wait
Reply #5 - Sep 6th, 2008, 11:07pm
 
Hang on, don't abandon hope yet, I have to drive someone home and then I'll try to modify the code and repost.

Stephen
Re: Making Draw Wait
Reply #6 - Sep 6th, 2008, 11:23pm
 
Hi Jim,

This version doesn't support animation, but it seems to do mostly what you want. It is late here, so I may have misread what you need. It should draw n items once. Then wait until a mouse press. Then redraw once with a new set of params (in this case a single random, but easy to change). It clears the screen every draw. Again, this won't look animated, it draws and then displays a single frame. I could make an animated version if you need it (so each ellipse appears in turn) but it is messier.

Code:

int numberToDraw = 0;
void setup()
{
size(400,400);
strokeWeight(2);
smooth();
numberToDraw = (int)random(100);
}

void draw()
{
background(255);
for(int i = 0; i < numberToDraw; i++)
{
stroke(random(255), random(255), random(255));
ellipse(random(0,width),random(0,height),4,4);
}
noLoop();
}

void mousePressed()
{
//load new parameters here...
numberToDraw = (int)random(100);
redraw();
}


Regards,

Stephen
Re: Making Draw Wait
Reply #7 - Sep 7th, 2008, 12:13am
 
Hi Stephen,

Hey that is a nifty piece of code you came up with. Tried it on my system and it works fine.

Many thanks for your detective work on this.

Re: Making Draw Wait
Reply #8 - Sep 7th, 2008, 12:26am
 
No problems, send us a link to your final sketch if you can!

Stephen
Re: Making Draw Wait
Reply #9 - Sep 7th, 2008, 8:35pm
 
Hi Stephen,

There may never be a final sketch but then again there may be many. The purpose of the sketch I was working on is to allow me to move through multiple versions of equation(s) varying one or more parameters. So my goal is to draw out an equation, have draw() stop and let me decide how to proceed - with a key press to print out the parameters if I like what I see or a mouse click to proceed to the next variant.

Of particular interest to me are lissajous curves.

I also just posted a class to another thread in this forum that will normalize the x and y values so they map nicely to my screen coordinates.

Thanks for all your help.

Page Index Toggle Pages: 1