0

The code below should make a shape flash twice, I triple checked the methods from root and I'm 99% sure it's that those methods are correct (I will post that code if needed though). What's the best way to make the current state of root pause on screen for a few seconds?

    noLoop();      
root.setVal(newVal);
root.highlight(0,255,0);
root.setopacity(200);
redraw();
try {Thread.sleep((long)1500);} 
catch (InterruptedException ex) {println("Error!");}
root.setopacity(0);
redraw();
try {Thread.sleep((long)1500);} 
catch (InterruptedException ex) {println("Error!");}
root.setopacity(200);
root.clearHL();//just to make sure I repeated these methods
root.highlight(0,255,0);
redraw();
try {Thread.sleep((long)1500);} 
catch (InterruptedException ex) {println("Error!");}
root.clearHL();
redraw();
loop();
return root;
Runeony1
  • 229
  • 1
  • 5
  • 14
  • On a completely different side note, you don't need to cast `1500` with `(long)`. Just use `L` instead like so `1500L`. The L indicates it's a Long Literal. – jluzwick Feb 27 '13 at 05:06

2 Answers2

2

You can only have one thread doing the drawing, and if you jam that thread up with sleep etc, it will "hang" until it gets a chance to get out of your code and back to rendering code inside the JRE. There are plenty of tutorials around about it, Google is your friend!

e.g.: http://www.java-tips.org/java-se-tips/java.awt/how-to-create-animation-paint-and-thread.html

Think of it as you drawing onto a page, and every now and then the page is pulled out of your notebook to be displayed. Doesn't matter if you take 10 seconds to draw a circle, then rub it out. All that matters is what is on the page when it gets displayed.

John3136
  • 28,809
  • 4
  • 51
  • 69
1

I'm not sure if i got your problem, and the code is not runnable, but...maybe you need a simpler approach? A little timer made by yourself? The thing is that draw() executes all instructions before rendering a frame at the end of draw(). So if you stop draw() it will pause, without doing any draw, and then continues making all changes and drawing at the end. I mean if I do:

draw(){
fill(0);
ellipse(10,10,10,10);
delay(1000);
fill(255,255,0);
ellipse(10,10,10,10);
}

I will never see the black ellipse as it is covered by the yellow one before render takes place...at the end of draw. But the program is gonna hang for one second every frame...

So maybe a simple timer could do it for you. Here a general sample of a timer you could try to adapt for your needs:

PFont font;
String time = "000";
int initialTime;
int interval = 1000;
int times;
color c = color(200);


void setup() {
  size(300, 300);
  font = createFont("Arial", 30);
  background(255);
  fill(0);
  smooth();

  //set the timer as setup is done
  initialTime = millis();
}

void draw()
{
  background(255);

  //compare elapsed time if bigger than interval...
  if (millis() - initialTime > interval)
  {
    //display the time
    time = nf(int(millis()/1000), 3);

    // reset timer
    initialTime = millis();

    //increment times
    times++;
  }

  // an arbitrary ammount
  if (times == 3) {

    //do somethng different
    c = color(random(255), random(255), random(255));

    // reset times
    times = 0;
  }

  //draw
  fill(0);
  text(time, width/2, height/2);
  fill(c);
  ellipse(75, 75, 30, 30);
}
v.k.
  • 2,826
  • 2
  • 20
  • 29