Question:
Need help writing a countdown clock in Java?
clueless
2012-11-09 08:53:10 UTC
I'm trying to write a simple GUI countdown clock in java. I want it to be a simple desktop app that counts down the time to a specific date down to the sec(or milliseconds, not that i would need that kind of accuracy i just think it would look cool). I do have some experience writing simple GUI programs in Java. What libraries, include statements. functions etc would i have to use? The end date does not have to be changeable by the user it can be hard coded in the program. I know how to code all the GUI, I just need help coding the timer portion of it.
Three answers:
?
2016-08-02 07:31:39 UTC
The doomsday clock. It's a leftover the cold conflict. A bunch of folks saved tabs on the political atmosphere and adjusted the clock's time for that reason, if it ever reached nighttime an nuclear battle was speculated to be imminent
Bon Jo
2012-11-09 09:14:29 UTC
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();

}

}



}





try this

it is simple
Zoltie
2012-11-10 00:19:10 UTC
public class Timer(){



int time;



public Timer(int start){

time = start;

startTime();

}



public void startTime(){

While (time > 0){

repaint();

try{

Thread.sleep(1000);

}catch(Exception ex){}

time--;

}

}

public void paintComponent(Graphics g){

g.drawString(time, 100, 100);

}

}



NOTE: If you have images of numbers you can put them into an array and then instead of using "drawString" you can use "drawImage(image[time], 100, 100, null)" assuming that the array is named "Image".


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...