Advertisement
Guest User

Untitled

a guest
Jul 14th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. // Start timer
  2. Runnable r = new TimerEg(gameLength);
  3. new Thread(r).start();
  4.  
  5. public static void main(int count) {
  6.  
  7. constructor TimerEg in class TimerEg cannot be applied to given types;
  8. required: no arguments; found int; reason: actual and formal arguments differ in length
  9.  
  10. import java.util.Timer;
  11. import java.util.TimerTask;
  12.  
  13. public class TimerEg {
  14. private static TimerTask myTask = null;
  15. public static void main(String[] args) {
  16. Timer timer = new Timer("My Timer", false);
  17. int count = 10;
  18. myTask = new MyTimerTask(count, new Runnable() {
  19. public void run() {
  20. System.exit(0);
  21. }
  22. });
  23.  
  24. long delay = 1000L;
  25. timer.scheduleAtFixedRate(myTask, delay, delay);
  26. }
  27. }
  28.  
  29. class MyTimerTask extends TimerTask {
  30. private int count;
  31. private Runnable doWhenDone;
  32.  
  33. public MyTimerTask(int count, Runnable doWhenDone) {
  34. this.count = count;
  35. this.doWhenDone = doWhenDone;
  36. }
  37.  
  38. @Override
  39. public void run() {
  40. count--;
  41. System.out.println("Count is: " + count);
  42. if (count == 0) {
  43. cancel();
  44. doWhenDone.run();
  45. }
  46. }
  47.  
  48. }
  49.  
  50. public class CountDown {
  51. //Counts down from x to 0 in approximately
  52. //(little more than) s * x seconds.
  53. static void countDown(int x, int s) {
  54. while (x > 0 ) {
  55. System.out.println("x = " + x);
  56. try {
  57. Thread.sleep(s*1000);
  58. } catch (Exception e) {}
  59. x--;
  60. }
  61. }
  62.  
  63. public static void main(String[] args) {
  64. countDown(5, 1);
  65. }
  66. }
  67.  
  68. int minute=10,second=60; // 10 min countdown
  69. int delay = 1000; //milliseconds
  70. ActionListener taskPerformer = new ActionListener() {
  71. public void actionPerformed(ActionEvent evt) {
  72. second--;
  73. // do something with second and minute. put them where you want.
  74. if (second==0) {
  75. second=59;
  76. minute--;
  77.  
  78. if (minute<0) {
  79. minute=9;
  80. }
  81. }
  82. }
  83. };
  84. new Timer(delay, taskPerformer).start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement