Chapter 6.3.3 – Fonts in java | Introduction to Programming Using Java

Chapter 6.3.3 – Fonts in java | Introduction to Programming Using Java

 

6.3.3 Fonts in java

 

Chapter 6.3.3 - Fonts in java | Introduction to Programming Using Java

 

A font represents a particular size and style of text. The same character will appear different in different font’s. In Java, a font is characterized by a font name, a style, and a size. The available font names are system dependent, but you can always use the following four strings as font names: “Serif”, “SansSerif”, “Monospaced”, and “Dialog”.

(A “serif” is a little decoration on a character, such as a short horizontal line at the bottom of the letter i. “SansSerif” means “without serifs.” “Monospaced” means that all the characters in the font have the same width. The “Dialog” font is the one that is typically used in dialog boxes.)

The style of a font is specified using named constants that are defined in the Font class. You can specify the style as one of the four values:

 

Chapter 6.3.3 - Fonts | Introduction to Programming Using Java

 

The size of a font is an integer. Size typically ranges from about 10 to 36, although larger sizes can also be used. The size of a font is usually about equal to the height of the largest characters in the font, in pixels, but this is not an exact rule. The size of the default font is 12.

Java uses the class named java.awt.Font for representing font. You can construct a new font by specifying its font name, style, and size in a constructor:

 

Chapter 6.3.3 - Fonts | Introduction to Programming Using Java

 

Every graphics context has a current font, which is used for drawing text. You can change the current font with the setFont() method. For example, if g is a graphics context and bigBoldFont is a font, then the command g.setFont(bigBoldFont) will set the current font of g to bigBoldFont. The new font will be used for any text that is drawn after the setFont() command is given. You can find out the current font of g by calling the method g.getFont(), which returns an object of type Font.

Every component has an associated font. It can be set with the instance method setFont(font), which is defined in the Component class. When a graphics context is created for drawing on a component, the graphic context’s current font is set equal to the font of the component.

 

fonts - introduction to programming using java
fonts – introduction to programming using java

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 5.5.3 – Example: Vehicles |

Chapter 5.6 – This and Super |

Chapter 5.7 – Interfaces |

Chapter 5.8 – Nested Classes | 

Chapter 6 – Introduction to GUI Programming |

1 thought on “Chapter 6.3.3 – Fonts in java | Introduction to Programming Using Java”

Leave a Comment