Text and Fonts

java.awt.Font class objects are used in order to use the drawString() method with different fonts. Font objects represent the name, style, and point size of a font. Another class, FontMetrics, provides methods to determine the size of the characters being displayed with a specified font, which can be used for things like formatting and centering text.

Creating Font Objects

A Font object is created by sending three arguments to its constructor:

  • The font's name

  • The font's style

  • The font's point size

The name of the font can be a specific font name such as Arial or Garamond Old Style, and it will be used if the font is present on the system on which the Java program is running.

There also are names that can be used to select Java's built-in ...

Get Sams Teach Yourself Java 2 in 21 Days, Second 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.