Magazine
 
Tips ‘n’ Tricks
 



4. Wishing New Year with count down in full screen window:

This program displays Frame in full screen window in which the remaining number of seconds for the New Year to come is displayed. Screen is updated every second and finally the message “Happy New Year 2008” is displayed.

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.NumberFormat;
public class GreetingNewYear
implements Runnable{
JFrame frame;
JLabel label;
long timeInMillis;
String greetingMessage;
static NumberFormat formatter =
NumberFormat.getInstance();
public GreetingNewYear (JFrame frame,
JLabel label)
{
this.frame = frame;
this.label = label;
// Set values for New Year.
Calendar calendar = new
CregorianCalendar();
int newYear = calendar.get(Calendar.YEAR)
+ 1;
calendar.set(Calendar.YEAR, newYear);
calendar.set(Calendar.MONTH,
Calendar.JANUARY);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

 

timeInMillis = alendar.getTime().getTime();
// set the message to be displayed for
greeting.
greetingMessage = “Happy New Year “ +
newYear;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setUndecorated(true);
JLabel label = new JLabel(“.”);
// Set the alignment of the text to be
displayed.
label.setHorizontalAlignment
(SwingConstants.CENTER);
frame.getContentPane().add(label);
// Set the size of window to full of the
screen.
GraphicsEnvironment.getLocal
GraphicsEnvironment().
getDefaultScreenDevice().
setFullScreenWindow(frame);
// Set the style, type and size for the
font of the label.
label.setFont(new
Font(“monospaced”,Font.BOLD,100));
new HappyNewYear(frame, label).run();
}
public void run()
{
boolean isNewYear = false;
do
{
long timeLeft= (timeInMillis -
System.currentTimeMillis()) /1000L;
String displayMessage;
// Check for New Year.
if (timeLeft< 1){
isNewYear = true;
displayMessage = greetingMessage;
}
else
{
displayMessage =
formatter.format(timeLeft);
// Set the above displayMessage
value to the label text.
label.setText(displayMessage);

Sept 2007 | Java Jazz Up | 69
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,   Download PDF