Jump to content

Creating a countdown timer for paint


Recommended Posts

Posted
class PaintTimer {
     private static final long NANO_SECOND = 1000000000L, BOUNDS = NANO_SECOND * 2;
     private long previousFrameTime;
     private long timeElapsed;


     private int secondsPassed;


     public int countDownUntil(boolean condition) {
          if(condition)
               secondsPassed = 0;


          long currentFrameTime = System.nanoTime();
          timeElapsed += currentFrameTime - previousFrameTime;
          previousFrameTime = currentFrameTime;


          if(timeElapsed > BOUNDS) 
               timeElapsed = 0;
          else if(timeElapsed >= NANO_SECOND) {
               secondsPassed++;
               timeElapsed = 0;
          }


          return secondsPassed;
     }
}

Your script would look something like:

final class MyScript {
     private PaintTimer timer;

     public void onStart() {
          timer = new PaintTimer();
     }

     public void onPaint(Graphics2D g) {
          int secondsPassed = timer.countDownUntil(getCondition());
          //use secondsPassed
     }

     private boolean getCondition() {
          return ...; //your condition
     }
}
Posted
class PaintTimer {
     private static final long NANO_SECOND = 1000000000L, BOUNDS = NANO_SECOND * 2;
     private long previousFrameTime;
     private long timeElapsed;


     private int secondsPassed;


     public int countDownUntil(boolean condition) {
          if(condition)
               secondsPassed = 0;


          long currentFrameTime = System.nanoTime();
          timeElapsed += currentFrameTime - previousFrameTime;
          previousFrameTime = currentFrameTime;


          if(timeElapsed > BOUNDS) 
               timeElapsed = 0;
          else if(timeElapsed >= NANO_SECOND) {
               secondsPassed++;
               timeElapsed = 0;
          }


          return secondsPassed;
     }
}

Your script would look something like:

final class MyScript {
     private PaintTimer timer;

     public void onStart() {
          timer = new PaintTimer();
     }

     public void onPaint(Graphics2D g) {
          int secondsPassed = timer.countDownUntil(getCondition());
          //use secondsPassed
     }

     private boolean getCondition() {
          return ...; //your condition
     }
}

Thanks, it works now!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...