Countdown Class library for Java, JS & Python

edited April 2018 in Share Your Work

Java Mode Version:

https://Gist.GitHub.com/GoToLoop/ea4db8a8622e27541213da3c9a43ca21

"CountdownClass.pde":

/** 
 * 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 String AWAIT = "Awaiting " + SECS;
static final String END = "Countdown Finished";

static final color WAITING_BG = PImage.BLUE_MASK | PImage.ALPHA_MASK;
static final color DONE_BG = PImage.RED_MASK | PImage.ALPHA_MASK;

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);
}

"Countdown.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;
    }
  }
}

Comments

  • edited April 2018

    p5.js JS Version:

    http://Bl.ocks.org/GoSubRoutine/1a0d21828319cf18fecf44101764bd60

    "index.html":

    <script async src=http://CDN.JSDelivr.net/npm/p5></script>
    <script defer src=countdown.js></script>
    <script defer src=sketch.js></script>
    

    "sketch.js":

    /**
     * Countdown Class (v1.2.5)
     * GoToLoop (2017/Aug/29)
     *
     * Forum.Processing.org/two/discussion/27733/
     * countdown-class-library-for-java-js-python#Item_1
     *
     * Forum.Processing.org/two/discussion/23846/
     * time-delay-in-python-mode#Item_16
     *
     * Bl.ocks.org/GoSubRoutine/1a0d21828319cf18fecf44101764bd60
     */
    
    "use strict";
    
    const SECS = 7.5, WAIT_TIME = SECS * 1000 | 0,
          AWAIT = SECS, END = 'Finished', LF = '\n',
          countdown = new Countdown(WAIT_TIME);
    
    let waitingBG, doneBG;
    
    function setup() {
      createCanvas(300, 180);
      frameRate(60);
    
      colorMode(RGB).blendMode(BLEND);
      fill('yellow').stroke(0).strokeWeight(1.5);
      textSize(0o100).textAlign(CENTER, CENTER);
    
      waitingBG = color('blue');
      doneBG = color('red');
    
      const m = ~~millis(), t = m + WAIT_TIME;
      countdown.start();
      console.log(m, t, t - m, countdown.toString());
      status = m + ', ' + t + ', ' + (t - m);
    }
    
    function draw() {
      top.document.title = countdown.done && END || AWAIT;
      background(countdown.done && doneBG || waitingBG);
    
      const txt = ~~millis() + LF + frameCount;
      text(txt, width>>1, height>>1);
    }
    

    "countdown.js":

    /**
     * Countdown Class (v1.2.5)
     * GoToLoop (2017/Aug/29)
     *
     * https://Forum.Processing.org/two/discussion/27733/
     * countdown-class-library-for-java-js-python#Item_1
     *
     * https://Forum.Processing.org/two/discussion/23846/
     * time-delay-in-python-mode#Item_16
     *
     * http://Bl.ocks.org/GoSubRoutine/1a0d21828319cf18fecf44101764bd60
     */
    
    "use strict";
    
    class Countdown {
      get delay() {
        return this._delay;
      }
    
      set delay(waitTime) {
        this._delay = ~~Math.abs(waitTime);
      }
    
      constructor(waitTime=0) { // milliseconds
        this.delay = waitTime;
        this.done = true;
        this._timeout = () => this.done = true;
        this.task = 0;
      }
    
      toString() {
        return 'Delay: ' + this.delay + '  -  Done: ' + this.done;
      }
    
      start() {
        clearTimeout(this.task);
        this.done = false;
        this.task = setTimeout(this._timeout, this.delay);
        return this;
      }
    }
    
  • edited April 2018

    Python Mode Version based on java.util.Timer

    https://Gist.GitHub.com/GoToLoop/a476ac6a01d18700240853fed33c0e57

    "CountdownClass.pyde":

    """
     Countdown Class (v1.2.5)
     GoToLoop (2017/Aug/28)
    
     Forum.Processing.org/two/discussion/27733/
     countdown-class-library-for-java-js-python#Item_2
    
     Forum.Processing.org/two/discussion/23846/
     time-delay-in-python-mode#Item_14
    
     Gist.GitHub.com/GoToLoop/a476ac6a01d18700240853fed33c0e57
    """
    
    from countdown import Countdown
    
    SECS = 7.5
    WAIT_TIME = int(SECS * 1000)
    
    WAITING_BG = PImage.BLUE_MASK | PImage.ALPHA_MASK
    DONE_BG = PImage.RED_MASK | PImage.ALPHA_MASK
    
    countdown = Countdown(WAIT_TIME)
    
    def setup():
        size(300, 180)
        smooth(3)
        frameRate(60)
    
        colorMode(RGB)
        fill(0xffFFFF00)
    
        textSize(0100)
        textAlign(CENTER, CENTER)
    
        m = millis(); t = m + WAIT_TIME
        countdown.start()
        print m, t, t - m, TAB, countdown
    
    
    def draw(AWAIT='Awaiting ' + `SECS`, END='Finished'):
        this.surface.title = countdown.done and END or AWAIT
        background(DONE_BG if countdown.done else WAITING_BG)
    
        txt = `millis()` + ENTER + `frameCount`
        text(txt, width>>1, height>>1)
    

    "countdown.py":

    """
     Countdown Class (v1.2.5)
     GoToLoop (2017/Aug/28)
    
     https://Forum.Processing.org/two/discussion/27733/
     countdown-class-library-for-java-js-python#Item_2
    
     https://Forum.Processing.org/two/discussion/23846/
     time-delay-in-python-mode#Item_14
    
     https://Gist.GitHub.com/GoToLoop/a476ac6a01d18700240853fed33c0e57
    """
    
    from java.util import Timer, TimerTask
    
    class Countdown:
        _t = Timer('Countdown')
    
        def __init__(self, waitTime=0): # milliseconds
            self.delay = abs(int(waitTime))
            self.done = True
    
            class Job(TimerTask):
                def run(_): self.done = True
    
            self._Timeout = Job
            self.task = None
    
    
        def __str__(self, STR='Delay: %d  -  Done: %s'):
            return STR % (self.delay, self.done)
    
    
        def start(self):
            self.task and self.task.cancel()
            self.task = self._Timeout()
            self.done = False
            self._t.schedule(self.task, self.delay)
            return self
    
  • edited April 2018

    Python Mode Version based on threading.Timer

    https://Gist.GitHub.com/GoToLoop/893dc18856a7842b76a92d8aea837da1

    "CountdownClass2.pyde":

    """
     Countdown Class II (v1.2.5)
     GoToLoop (2017/Aug/30)
    
     Forum.Processing.org/two/discussion/27733/
     countdown-class-library-for-java-js-python#Item_3
    
     Forum.Processing.org/two/discussion/23846/
     time-delay-in-python-mode#Item_17
    
     Gist.GitHub.com/GoToLoop/893dc18856a7842b76a92d8aea837da1
    """
    
    from countdown import Countdown
    
    SECS = 7.5
    WAIT_TIME = int(SECS * 1000)
    
    WAITING_BG = PImage.BLUE_MASK | PImage.ALPHA_MASK
    DONE_BG = PImage.RED_MASK | PImage.ALPHA_MASK
    
    countdown = Countdown(SECS)
    
    def setup():
        size(300, 180)
        smooth(3)
        frameRate(60)
    
        colorMode(RGB)
        fill(0xffFFFF00)
    
        textSize(0100)
        textAlign(CENTER, CENTER)
    
        m = millis(); t = m + WAIT_TIME
        countdown.start()
        print m, t, t - m, TAB, countdown
    
    
    def draw(AWAIT='Awaiting ' + `SECS`, END='Finished'):
        this.surface.title = countdown.done and END or AWAIT
        background(DONE_BG if countdown.done else WAITING_BG)
    
        txt = `millis()` + ENTER + `frameCount`
        text(txt, width>>1, height>>1)
    

    "countdown.py":

    """
     Countdown Class II (v1.2.5)
     GoToLoop (2017/Aug/30)
    
     https://Forum.Processing.org/two/discussion/27733/
     countdown-class-library-for-java-js-python#Item_3
    
     https://Forum.Processing.org/two/discussion/23846/
     time-delay-in-python-mode#Item_17
    
     https://Gist.GitHub.com/GoToLoop/893dc18856a7842b76a92d8aea837da1
    """
    
    from threading import Timer
    
    class Countdown:
        def __init__(self, waitTime=0.0): # seconds
            self.delay = abs(waitTime)
            self.done = True
    
            def job(): self.done = True
    
            self._timeout = job
            self.task = None
    
    
        def __str__(self, STR='Delay: %.3f  -  Done: %s'):
            return STR % (self.delay, self.done)
    
    
        def start(self):
            self.task and self.task.cancel()
            self.task = Timer(self.delay, self._timeout)
            self.done = False
            self.task.start()
            return self
    
  • Thxs for sharing @GoToLoop.

    Kf

Sign In or Register to comment.