Working With Fonts in Applet

Working with Fonts in Applet

To draw text to the screen, first, you need to create an instance of the Font class.

Font objects represent an individual font; that is, its name, style (bold, italic), and point size.

Font names are strings representing the family of the font, for example, "TimesRoman", "Courier", or "Helvetica".

Font styles are constants defined by the Font class; you can get to them using class variables, for example, Font.PLAIN, Font.BOLD, or Font.ITALIC.

Finally, the point size is the size of the font, as defined by the font itself; the point size may or may not be the height of the characters.

To create an individual font object, use these three arguments to the Font class's new constructor.

Font f = new Font("TimesRoman", Font.BOLD, 24);

This example creates a font object for the TimesRoman bold font, in 24 points. Note that like most Java classes, you need to import the java.awt.Font class before you can use it.

The fonts you have available to you in your applets depend on which fonts are installed on the system where the applet is running. If you pick a font for your applet and that font isn't available on the current system then Java will substitute a default font (usually Courier).

Code

Output

Applet

Applet viewe


Similar Articles