Java Reference
In-Depth Information
// the first pass (assumes the applet window never
// resized).
if (fFirstPass) {
// Get measures needed to center the message
FontMetrics fm = g.getFontMetrics ();
// How many pixels wide is the string
int msg - width = fm.stringWidth (date - out);
// Use the string width to find the starting point
fMsgX = getSize ().width/2 - msg - width/2;
// How far above the baseline can the font go?
int ascent = fm.getMaxAscent ();
// How far below the baseline?
int descent= fm.getMaxDescent ();
// Use the vertical height of this font to find
// the vertical starting coordinate
fMsgY = getSize ().height/2 - descent/2 + ascent/2;
}
g.drawString (date - out, fMsgX, fMsgY);
fFirstPass = false;
} // paintComponent
} // class DateFormatPanel
The DateFormatPanel class uses an instance of DateFormat obtained with
the factory method getTimeInstance (int style) . The style of the date
and time output is obtained with the DateFormat constants. The Java 2 API
Specifications for this class list the style constants. Here we chose for the time
format the DateFormat.DEFAULT style. What this actually means varies some-
what with the locale setting (see the Sun tutorial on internationalization of Java
programs [7]). In the USA the default style results in a time string such as
“5:30:33 PM”.
The DateFormat subclass SimpleDateFormat offers a somewhat more
explicit technique for setting the format. Date and time formats are chosen with
a string pattern. The Java 2 API Specification for the SimpleDateFormat
provides a table of symbols to use in the format patterns. For example, in the
example below, the pattern
"EEE, d MMM yyyy HH:mm:ss Z"
results in a time format that goes as
Wed, 26 Mar 2003 15:34:35 —0500
 
Search WWH ::




Custom Search