Chapter 12.2.1 Measuring Text | Introduction to Programming Using Java

Chapter 12.2.1 Measuring Text | Introduction to Programming Using Java

 

12.2.1 Measuring Text

 

Chapter 12.2.1 Measuring Text | Introduction to Programming Using Java

 

Although this section is mostly about Graphics2D, we start with a topic that has nothing to do with it.

Often, when drawing a string, it’s important to know how big the image of the string will be. For example, you need this information if you want to center a string in a component. Or if you want to know how much space to leave between two lines of text, when you draw them one above the other.

Or if the user is typing the string and you want to position a cursor at the end of the string. In Java, questions about the size of a string can be answered by an object belonging to the standard class java.awt.FontMetrics.

There are several lengths associated with any given font. Some of them are shown in this illustration:

 

Chapter 12.2.1 Measuring Text | Introduction to Programming Using Java

 

The dashed lines in the illustration are the baselines of the two lines of text. The baseline of a string is the line on which the bases of the rest of the characters. The suggested distance between two baselines, for single-spaced text, is known as the line height of the font. The ascent is the distance that tall characters can rise above the baseline, and the descent is the distance that tails like the one on the letter “g” can descend below the baseline.

The ascent and descent do not add up to the line height, because there should be some extra space between the tops of characters in one line and the tails of characters on the line above. The extra space is called leading. (The term comes from the time when lead blocks were used for printing. Characters were formed on blocks of lead that were lined up to make up the text of a page, covered with ink, and pressed onto paper to print the page.

 

Extra, blank “leading” was used to separate the lines of characters.) All these quantities can be determined by calling instance methods in a FontMetrics object. There are also methods for determining the width of a character and the total width of a string of characters.

Recall that font in Java is represented by the class Font. A FontMetrics object is associated with a given font and is used to measure characters and strings in that font. If font is of type Font and g is a graphics context, you can get a FontMetrics object for the font by calling g.getFontMetrics(font). Then, if FM is the variable that refers to the FontMetrics object, then the ascent, descent, leading, and line height of the font can be obtained by calling FM.

getAscent(), FM.getDescent(), FM.getLeading(), and FM.get eight(). If ch is a character, then fm.charWidth(ch) is the width of the character when it is drawn in that font. If str is a string, then FM. string width(str) is the width of the string when drawn in that font. For example, here is a paintComponent() method that shows the message “Hello World” in the exact center of the component:

 

Chapter 12.2.1 Measuring Text | Introduction to Programming Using Java

 

You can change the font that is used for drawing strings as described in Subsection 6.3.3. For the height of the string in this method, I use fm.getAscent(). If I were drawing “Goodbye World” instead of “Hello World,” I would have used fm.getAscent() + fm.getDescent(), where the descent is added to the height in order to take into account the tail on the “y” in “Goodbye”.

The value of baseX is computed to be the amount of space between the left edge of the component and the start of the string. It is obtained by subtracting half the width of the string from the horizontal center of the component.

This will center the string horizontally in the component. The next line computes the position of the top of the string in the same way. However, to draw the string, we need the y-coordinate of the baseline, not the y-coordinate of the top of the string. The baseline of the string is below the top of the string by an amount equal to the ascent of the font.

There is an example of centering a two-line block of text in the sample program TransparencyDemo.java, which is discussed in the next subsection.

 

Chapter 12.2.1 Measuring Text | Introduction to Programming Using Java

 

 

 

SEE MORE:

3 thoughts on “Chapter 12.2.1 Measuring Text | Introduction to Programming Using Java”

Leave a Comment