Java Reference
In-Depth Information
quirement that the first argument be
Font.TRUETYPE_FONT
, only TrueType fonts are suppor-
ted at present. The
Font
class documentation, ever the optimist, states that this field is to al-
low possible future addition of other font formats, though none is promised. Given the avail-
ability of free PostScript font renderers, such as the one in the X Window System XFree86, it
should be possible to add PostScript font support in the future.
Example 12-4
is a listing of a
small standalone application that creates the window shown in
Figure 12-5
.
Figure 12-5. TTFontDemo in action
Example 12-4. Demo of an application font
public
public class
class
TTFontDemo
TTFontDemo
extends
extends
JLabel
{
private
private static
static final
final
long
long
serialVersionUID
= -
2774152065764538894L
;
/** Construct a TTFontDemo -- Create a Font from TTF.
*/
public
public
TTFontDemo
(
String fontFileName
,
String text
)
throws
throws
IOException
,
FontFormatException
{
super
super
(
text
,
JLabel
.
CENTER
);
setBackground
(
Color
.
white
);
// First, see if we can load the font file.
InputStream is
=
this
this
.
getClass
().
getResourceAsStream
(
fontFileName
);
iif
(
is
==
null
null
) {
throw
throw new
new
IOException
(
"Cannot open "
+
fontFileName
);
}