Java Reference
In-Depth Information
// createFont makes a 1-point font, bit hard to read :-)
Font ttfBase = Font . createFont ( Font . TRUETYPE_FONT , is );
// So scale it to 24 pt.
Font ttfReal = ttfBase . deriveFont ( Font . PLAIN , 24 );
setFont ( ttfReal );
}
/** Simple main program for TTFontDemo */
public
public static
static void
void main ( String [] args ) throws
throws Exception {
String DEFAULT_MESSAGE =
"What hath man wrought? Or at least rendered?" ;
// Loaded as Resource so don't need graphics/ in front
String DEFAULT_FONTFILE = "Kellyag_.ttf" ;
String message = args . length == 1 ? args [ 0 ] : DEFAULT_MESSAGE ;
JFrame f = new
new JFrame ( "TrueType Font Demo" );
TTFontDemo ttfd = new
new TTFontDemo ( DEFAULT_FONTFILE , message );
f . getContentPane (). add ( ttfd );
f . setBounds ( 100 , 100 , 700 , 250 );
f . setVisible ( true
true );
f . setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE );
}
}
This font technology has some restrictions. First, as noted in the comments, you can use this
font only on a Swing JComponent , not on an AWT Component (see Chapter 14 ) .
Also, this technique cannot easily be used in an applet due to security concerns (because it
needs to create a local temporary file copy of the font). It should work in an application
downloaded using Java WebStart (see Java Web Start ).
Drawing an Image
Problem
You want to display an image, a preformatted bitmap, or raster file.
Search WWH ::




Custom Search