Displaying iteration result, one at a time

edited December 2016 in How To...

Hi all,

I am working on a simple sketch that reads an x amount of words and sentences from a text (say, "Hamlet", for example), and loads every sentence in a String array. Then, during the void draw function, a loop displays every sentence randomly in the screen for each iteration. The final result is a bunch of words and sentences, ones over the others.

As far as I know, and noticed, all the words and sentences are shown at once, once the sketch is finished. Is there any way to display each sentence as it is written, so the screen becomes populated in an increasing way, instead of show all the words at once, at the end?

I would prefer not to use delay or frame rate. Is there a simple way to do it?

Thanks in advance for your help!

Best,

Marcelo

Tagged:

Answers

  • so the screen becomes populated in an increasing way, instead of show all the words at once, at the end? I would prefer not to use delay or frame rate.

    What would you like to do? Are you trying to show a new word or a new sentence every n seconds? Or every time the spacebar is pressed? Please be specific about what you are trying to accomplish.

  • draw() is a loop. it gets called repeatedly.

    so define a counter OUTSIDE the draw (as a global variable), update it within draw and use it to pick the sentence to display.

  • Hi, jeremydouglass, I just want the viewer to see the sentences as they are loaded in the loop and drawn in the screen. What bothers me is that the viewer has to wait till the iterations are finished. I would like to see them being displayed as they are read from the array. I don't need any interaction or n seconds. Instead of seen the final image, I would like the viewer see how these sentences appear and form the image, one at a time. Thanks for your time!

  • Hi koogs, Thanks for your answer. I thought there was a simpler way to do it. This is not to say that what your suggest is difficult, but I do not seek to pick some specific sentence... just show the one being displayed or drawn in the backstage, at the time it is picked and drawn, instead of waiting for the final image .It seems there is no way to accomplish this, but it need to be coded specifically. Thanks anyway!

  • Answer ✓

    Well, in this case the specific sentence is just the next sentence... Typically you'd load all the sentences at the same time, in setup, using loadStrings. Then you'd display one at a time using the next one in the array.

  • edited December 2016 Answer ✓

    The answer to: "How do I draw repeatedly to the screen with Processing, and change what happens each time?" is "use draw()". For example:

    void setup(){
      frameRate(1);
    }
    void draw(){
      background(192);
      drawWords(frameCount);
    }
    void drawWords(int count){
      // add code year t odraw some words based on the number given in 'count'
    }
    

    In this example Processing will call draw every 1 second. Draw will then call drawWords with an argument -- the current frame count (1,2,3,4)...:

    • 1 second: drawWords(1);
    • 2 seconds: drawWords(2);
    • 3 seconds: drawWords(3);
    • ....

    So each second, a different number of words will appear on your screen.

  • edited December 2016

    Hi again. Thanks both of you for your help and your suggestions. To koogs: yes, that is what I mean, but it seems that void draw() will not show anything until all the iterations are done, and only then, it shows the final image. As you say, I load the sentences in void setup(), and then I use a for loop in void draw() to display every item (sentence) of that array. My problem is that it doesn't show the sentences until it finishes. No way to do this, unless I specifically code it, as jeremydouglass did with draw(). Thanks again.

  • Answer ✓

    I use a for loop in void draw() to display every item (sentence) of that array.

    And that's exactly what I'm telling you not to do. Use a counter outside the draw. See Jeremy's example.

  • at it's very simplest:

    int count = 0;
    
    void setup() {
      size(640, 480);
      frameRate(1);
    }
    
    void draw() {
      println(count);
      count++;
    }
    
  • Yes, I understand you. I guess the final answer is simply that Processing just displays the final image, and will not display partial results unless coded to do so.

  • the final answer is simply that Processing just displays the final image, and will not display partial results.

    This is correct.

    If you want to draw a result to the screen, you call draw() (or redraw()). When the draw() function returns, the screen updates. There are no "partial results" -- everything else that happens up to that point is moving color values around in memory getting ready for the moment when the actual screen pixels are updated once draw returns. Call draw once to update the screen once. Call draw 100 times to update the screen 100 times.

  • Hi! Yes, I understood that. Thanks! It seems that when I see some visualizations that show one item at a time, it's just draw() being call every time... Thanks again.

Sign In or Register to comment.