new years eve is coming and im thinking what are the possible ways to program a old fashioned countdown timer into java to use it as a countdown of 10 seconds.
what i mean by a old fashioned countdown is http://www.youtube.com/watch?v=9h7J5VWUOYw
what i could think of is only using JLabel to do a simple countdown timer using Timer.
EDIT: what i have done is make a clock fullscreen and play a simple countdown video at 23:59:30 using VLCJ
public class TimerFrame extends JFrame{
SimpleDateFormat dateFormatter = new SimpleDateFormat(" H:mm:ss");
Date now = new Date();
private JLabel lblTime = new JLabel();
private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
private String countdownVideo ="C:\\Users\\LT\\Desktop\\newyear\\cd2.mp4";
private String vlcFolder = "C:/Program Files/VideoLAN/VLC";
private java.util.Timer timerVideoRun = new java.util.Timer();
private Timer timer;
public TimerFrame()
{
//time for video to run on
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 30);
Date time = calendar.getTime();
timerVideoRun.schedule(new saveTask(this), time);//schedule task on above time
//vlcj load
loadVLCJ();
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
//label time settings
timer = new Timer(1000, new MyListener(this)); //Tick every 1000ms, let MyListener listen to the ticks
timer.start(); //Start the timer
lblTime.setFont(new Font("Helvetica", Font.PLAIN, 270));
lblTime.setVerticalAlignment(SwingConstants.CENTER);
lblTime.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
updateLabelTime();
//JFrame
add(lblTime, BorderLayout.CENTER);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setUndecorated(true);
setResizable(false);
setVisible(true);
setExtendedState(JFrame.MAXIMIZED_BOTH);
mediaPlayerComponent.getMediaPlayer().prepareMedia(countdownVideo,":start-time=30");
}
public void updateLabelTime()
{
now.setTime(System.currentTimeMillis());
lblTime.setText(dateFormatter.format(now));
}
public void playVideo()
{
timer.stop();
setContentPane(mediaPlayerComponent);
validate();
mediaPlayerComponent.getMediaPlayer().play();
}
public void loadVLCJ()
{
NativeLibrary.addSearchPath(
RuntimeUtil.getLibVlcLibraryName(), vlcFolder
);
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
}
}
public class MyListener implements ActionListener {
TimerFrame frame;
public MyListener(TimerFrame timerFrame) {
this.frame = timerFrame;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
this.frame.updateLabelTime();
}
}
public class saveTask extends TimerTask {
TimerFrame frame;
public saveTask(TimerFrame timerFrame) {
this.frame = timerFrame;
}
@Override
public void run() {
this.frame.playVideo();
}
}
public class TimerMain {
/**
* @param args
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TimerFrame();
}
});
}
}
i would like to ask that in the method playVideo() if i were to comment out the validate() method , the program would not "switch" into the video does anyone know why does this happen and if there is a more better way rather than doing a call to validate()?
Also if anyone is experienced in VLCJ is it possible for the video to stop playing audio after a certain amount of time has passed in the video and then i would play another audio?
As @Vallentin said, we aren't here to do your work or research for you. I will, however, be more than happy to point you in the right direction at least.
If you want the animation and all, you'll have to research graphics. As for the countdown itself, create a method that will apply the
setText()
method to your label for each second that passes in the countdown.If you start working on the code and come across errors, feel free to post some of your code, tell us the error, and let us know what you're trying to accomplish.