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.
Page Index Toggle Pages: 1
force redraw (Read 1210 times)
force redraw
Apr 14th, 2010, 3:35pm
 
Hi everyone,

noob and my first post.  I'm using processing to make a simple GUI for coworkers to turn a servomotor on and off, various speeds etc.

One of the functions has the motor cycle back and forth between two angles, which I accomplish with:
void draw(){
}

void motorcyclefunction(){
for(...){
move motor one way
move motor the other way
}
}

Problem is, while this is going on, my window does not update. Therefore I can't change anything or detect any clicks (especially the stop button!).

I used to do this stuff in Matlab, and there the same thing happened, but there was a function called drawnow which forced the figure window to update. Is there something similar in processing? Or is there another way of accomplishing this entirely?

Thanks,
Mike
Re: force redraw
Reply #1 - Apr 14th, 2010, 6:41pm
 
you have to call your function within draw() in order for it to run.

if you arnt talking about that then,

try redraw();
Re: force redraw
Reply #2 - Apr 15th, 2010, 1:08am
 
As pwn monkey says you need to put code in draw for it to be 'animated'.  You haven't posted the rest of your code, but one common problem is people using noLoop() and then trying to get around the limitations this imposes.  More often than not there's a simple enough solution that doesn't require noLoop()...
Re: force redraw
Reply #3 - Apr 15th, 2010, 1:16am
 
Don't try redraw()... We rarely, if ever, use it in sketches.

Your problem is very common (if not addressed in the FAQ, it should be).

You have to redesign your program, seeing draw() code as the body of a loop that runs forever. So you manage variables (booleans, etc.) to handle various states of the program, and you use a timer (millis()) to find out when to change state.

There are plenty examples, as a good number of sketches uses this logic. If you need more help, just ask.
Re: force redraw
Reply #4 - Apr 15th, 2010, 11:36am
 
Thanks for the answers guys.  I tried redraw() in various places and it didn't work.  Been working on a way of having draw() command the motor cycles but so far no luck.  Problem is even within a major motor-cycle there are also smaller while-loops which wait for the motor to signal it has reached a position.  At best I can only see the window redrawing once at the end of each motor-cycle, not affording enough time for a user to click stop.  
I'm also experimenting with a multithread solution but it's a bit above my head.  I'm using the code below which I found in the forums as a basis.
Can anyone tell me what the keyword "Thread" does?  Its not in the reference.  From what I can tell "loader" is of type ImageLoader which is defined as a class later in the code, and it is somehow told to run on a separate thread "loadThread" through use of this keyword "Thread."

//////////////////////////////////////
// ThreadImageLoader.pde - Multi-threaded loading of images.
// The ImageLoader class can be used as a template for creating
// a separate thread for loading images, leaving your main sketch
// free to do other things.
//
// Marius Watz - http://workshop.evolutionzone.com/

ImageLoader loader;
Thread loadThread;
PFont fnt;

void setup() {
 size(450,125);

 loader=new ImageLoader();
 loadThread=new Thread(loader);

 loader.loadImg(
   "http://farm3.static.flickr.com/2292/2317266674_77180fff63_s.jpg");
 loader.loadImg(
   "http://farm3.static.flickr.com/2131/2317257856_f9e9057000_s.jpg");
 loader.loadImg(
   "http://farm4.static.flickr.com/3250/2317275004_319ba43b1c_s.jpg");
 loader.loadImg(
   "http://farm3.static.flickr.com/2194/2317241464_43ebe23c0f_s.jpg");
 loader.loadImg(
   "http://farm3.static.flickr.com/2046/2304190351_f96852859d_s.jpg");
 loader.loadImg(
   "http://farm3.static.flickr.com/2069/2304970048_00d04a818d_s.jpg");

 loadThread.start();
 fnt=createFont("Courier",18,false);
}

void draw() {
 background(0);
 textFont(fnt);
 noStroke();

 fill(255);
 text("Remaining to load: "+loader.remaining,10,20);

 int x=0;
 for(int i=0; i< loader.num; i++) {
   PImage tmp=loader.getImage(i);
   if(tmp!=null) {
     image(tmp, x,50);
     x=x+tmp.width;
   }
 }
}

void stop() {
 loader.loadDone=true;
 super.stop();
}

class ImageLoader implements Runnable {
 ImageURL img[];
 int num,remaining;
 boolean loadDone=false;

 public ImageLoader() {
   img=new ImageURL[100];
   num=0;
   remaining=0;
 }

 void loadImg(String _url) {
   // expand array if necessary
   if(num==img.length) img=(ImageURL[])expand(img);

   img[num++]=new ImageURL(_url);
   remaining++;
 }    

 public PImage getImage(int id) {
   // return null if incorrect ID, image not loaded or error occurred
   if(img[id]==null || !img[id].loaded || img[id].error)
     return null;

   return img[id].img;
 }

 public void run() {
   int loadID=0;

   // loop run() until loadDone is set to true
   while(!loadDone) {

     // find an image to load
     loadID=0;
     if(num>0) do {
       if(img[loadID].loaded) loadID++;
       else if(img[loadID]!=null) {
         img[loadID].load(); // load image
         remaining--; // one less remaining to load
         break; // exit do loop;
       }
     } while(loadID< num);

     try {
       loadThread.sleep(500);
     } catch(InterruptedException e) {
       e.printStackTrace();
     }
   }

   println("Loader thread exiting.");
 }
}

class ImageURL {
 PImage img;
 boolean loaded=false,error=false;
 String url;

 public ImageURL(String _url) {
   url=_url;
 }

 void load() {
   println("nLoading image: "+url);
   img=loadImage(url);
   if(img==null) error=true;
   loaded=true;
   if(error) println("Error occurred when loading image.");
   else println("Load successful. "+
     "Image size is "+img.width+"x"+img.height+".");
 }
}
///////////////////////////////////
Page Index Toggle Pages: 1