Progress bar inside a calculation (for loop)

Hello, everyone! I am working on a school project, photoshop. When I use the blur effect it takes a very long time. So, I want to tell the user, how much progress has the algorithm made so far. I have a single function doing all the work. Inside that function is a for loop and the most work is done inside that for loop.

The only values it is showing me are 0% and 100%. I want to find a way to either draw the progress bar individually or redraw the whole screen inside that for loop.

Thx for help in advance.

Answers

  • Answer ✓

    best bet to rewrite the for loop so that it uses the fact that draw() in itself loops

    so let's say now you have for(int i..........

    instead declare i before setup() and increase it at the end of draw()

    use i in your function but without the for-loop

    when i has reached the maximum change the state and stop displaying the progress bar

  • int startTime; 
    int counter; 
    int maxTime; 
    
    boolean done; 
    
    void setup() {
    
      //setup function called initially, only once 
    
      size(250, 250); 
      background(255); 
    
      counter = 0; 
      startTime= millis(); 
      maxTime=int(random(2000, 8001)); 
      done=false;
    } 
    
    void draw() {
    
      // the draw function loops on and on 
    
      background(244); 
    
      // this is like a state 
      if (done) {
        fill(0); 
        text("DONE", width/2-13, height/2);
      } else {
        if (counter-startTime < maxTime) {
    
          counter=millis();
        } else {
          done=true;
        }
        fill(244, 3, 3);
        noStroke();
        rect(20, 100, map(counter-startTime, 0, maxTime, 0, 200), 19 );
        text(counter- startTime+" " + int(maxTime) +  " " + int ( map(counter-startTime, 0, maxTime, 0, 200)), 20, 160);
        noFill();
        stroke(0);
        rect(20, 100, 200, 19);
      }//else
    } // func
    
    void mousePressed () { 
      if (done) { 
        counter = 0; 
        startTime= millis();
    
        maxTime=int(random(2000, 8001)); 
        done=false;
      }
    }
    
  • edited November 2017

    That's that I thought too. Rewriting the for loop and executing it one by one at runtime. However, my teacher wants a single function to do all the work. So, I can't use that, but thanks for your answer anyways.

    I recently learned to use thread(). Any solutions for that?

    I tried using a variable to indicate the progress and to draw the current progress and then running the blur function in a separate thread updating the variable. For some reason it didn't work...

  • That should work

    Post your attempt

  • It didn't work. Then I tried to do the same, but with a text file instead. In both cases, when I inputed a dark purple image, the screen started flickering purple.

    Just now, I might have come up with a solution. I have a question though. Does the thread loop or does it run the code once. And, if it loops, how do I stop it? I need the thread to run only once.

    I'll post my attempt later. I dont have my laptop with me. :P

  • Maybe return in the function you called with thread()?

  • Is that, how you terminate a thread?

  • So, now my problem boils down to how do I change a variable from a thread. The thread grabs an image, processes it and returns it. The problem now is that I have a variable called effectProgress and it is used two times:

    In drawing function:

    if(darkTheme){ stroke(#2F2F2F); strokeWeight(3); line(0, mainCluster.getValue("height") + 20, width, mainCluster.getValue("height") + 20); stroke(#FFFFFF); strokeWeight(3); line(0, mainCluster.getValue("height") + 20, width * progress, mainCluster.getValue("height") + 20); } else{ stroke(#9F9F9F); strokeWeight(3); line(0, mainCluster.getValue("height") + 20, width, mainCluster.getValue("height") + 20); stroke(#000000); strokeWeight(3); line(0, mainCluster.getValue("height") + 20, width * progress, mainCluster.getValue("height") + 20); }

    In the function in the new thread:

    returnImage.loadPixels(); for(int x = 0; x < returnImage.width; x ++){ for(int y = 0; y < returnImage.height; y ++){ returnImage.pixels[x + y * pic.width] = effectBlurGetPixel(x, y, pic, matrix, blurAmount); effectProgress = (x + y * pic.width) / pic.pixels.length; } }

    the new thread doesn't seem to influence the new animation thread.

  • I don't know, how I did it, but it is working. I need help with screen flickering. It is like while the function is running, when the new thread edits a pixel in the image, the animation thread takes the color as input in fill() or stroke(). I'll record it and send you a link.

Sign In or Register to comment.