Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Start timer
- Runnable r = new TimerEg(gameLength);
- new Thread(r).start();
- public static void main(int count) {
- constructor TimerEg in class TimerEg cannot be applied to given types;
- required: no arguments; found int; reason: actual and formal arguments differ in length
- import java.util.Timer;
- import java.util.TimerTask;
- public class TimerEg {
- private static TimerTask myTask = null;
- public static void main(String[] args) {
- Timer timer = new Timer("My Timer", false);
- int count = 10;
- myTask = new MyTimerTask(count, new Runnable() {
- public void run() {
- System.exit(0);
- }
- });
- long delay = 1000L;
- timer.scheduleAtFixedRate(myTask, delay, delay);
- }
- }
- class MyTimerTask extends TimerTask {
- private int count;
- private Runnable doWhenDone;
- public MyTimerTask(int count, Runnable doWhenDone) {
- this.count = count;
- this.doWhenDone = doWhenDone;
- }
- @Override
- public void run() {
- count--;
- System.out.println("Count is: " + count);
- if (count == 0) {
- cancel();
- doWhenDone.run();
- }
- }
- }
- public class CountDown {
- //Counts down from x to 0 in approximately
- //(little more than) s * x seconds.
- static void countDown(int x, int s) {
- while (x > 0 ) {
- System.out.println("x = " + x);
- try {
- Thread.sleep(s*1000);
- } catch (Exception e) {}
- x--;
- }
- }
- public static void main(String[] args) {
- countDown(5, 1);
- }
- }
- int minute=10,second=60; // 10 min countdown
- int delay = 1000; //milliseconds
- ActionListener taskPerformer = new ActionListener() {
- public void actionPerformed(ActionEvent evt) {
- second--;
- // do something with second and minute. put them where you want.
- if (second==0) {
- second=59;
- minute--;
- if (minute<0) {
- minute=9;
- }
- }
- }
- };
- new Timer(delay, taskPerformer).start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement