Using the Font Class

Colors and fonts are represented in Java by the Color and Font classes in the java.awt package. With these classes, you can present text in different fonts and sizes and change the color of text and graphics. Fonts are created with the Font(String, int, int) constructor, which takes three arguments:

• The typeface of the font as either a generic name (“Dialog,” “DialogInput,” “Monospaced,” “SanSerif,” or “Serif”) or an actual font name (“Arial Black,” “Helvetica,” or “Courier New”)

• The style as one of three class variables: Font.BOLD, Font.ITALIC, or Font.PLAIN

• The size of the font in points

The following statement creates a 12-point italic Serif Font object:

Font current = new Font("Serif", Font.ITALIC, 12);

If you use ...

Get Sams Teach Yourself Java™ in 24 Hours, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.