Skip to content

Instantly share code, notes, and snippets.

@GoToLoop
Last active Apr 13, 2018
Embed
What would you like to do?
Countdown Class (Java)
/**
* Countdown Class (v1.2.5)
* GoToLoop (2017/Aug/26)
*
* https://Forum.Processing.org/two/discussion/27733/
* countdown-class-library-for-java-js-python
*
* https://Forum.Processing.org/two/discussion/23846/
* time-delay-in-python-mode#Item_11
*
* https://Gist.GitHub.com/GoToLoop/ea4db8a8622e27541213da3c9a43ca21
*/
package gotoloop.countdown;
import java.util.Timer;
import java.util.TimerTask;
public class Countdown {
protected static final Timer t = new Timer("Countdown");
public TimerTask task;
public int delay;
public volatile boolean done = true;
public Countdown() {
}
public Countdown(final int waitTime) { // milliseconds
delay = Math.abs(waitTime);
}
@Override public String toString() {
return "Delay: " + delay + " - Done: " + done;
}
public Countdown start() {
if (task != null) task.cancel();
done = false;
t.schedule(task = new Timeout(), delay);
return this;
}
protected class Timeout extends TimerTask {
@Override public void run() {
done = true;
}
}
}
/**
* Countdown Class (v1.2.5)
* GoToLoop (2017/Aug/26)
*
* Forum.Processing.org/two/discussion/27733/
* countdown-class-library-for-java-js-python
*
* Forum.Processing.org/two/discussion/23846/
* time-delay-in-python-mode#Item_11
*
* Gist.GitHub.com/GoToLoop/ea4db8a8622e27541213da3c9a43ca21
*/
import gotoloop.countdown.Countdown;
static final float SECS = 7.5;
static final int WAIT_TIME = (int) (SECS * 1000);
static final color WAITING_BG = PImage.BLUE_MASK | PImage.ALPHA_MASK;
static final color DONE_BG = PImage.RED_MASK | PImage.ALPHA_MASK;
static final String AWAIT = "Awaiting " + SECS;
static final String END = "Countdown Finished";
final Countdown countdown = new Countdown(WAIT_TIME);
void setup() {
size(300, 180);
smooth(3);
frameRate(60);
colorMode(RGB);
fill(#FFFF00);
textSize(0100);
textAlign(CENTER, CENTER);
final int m = millis(), t = m + WAIT_TIME;
countdown.start();
println(m, t, t - m, TAB, countdown);
}
void draw() {
getSurface().setTitle(countdown.done? END : AWAIT);
background(countdown.done? DONE_BG : WAITING_BG);
final String txt = millis() + "\n" + frameCount;
text(txt, width>>1, height>>1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment