FontMetrics Class In Java

Introduction

Sometimes it is necessary to know the attributes of fonts used within a program. In such a case, the FontMetric class proves useful. An object of this class is created by using getFontMetrics() method. The object will contain relevant information about a font, including the height of a character, width of string in pixel, and so on. The FontMetrics class represents a font metrics object, which encapsulates information about rendering of a particular font on a particular screen.

FontMetrics class

The FontMetrics class is used to obtain precise information on a specific font such as height, descent i,e the amount of characters dips below the baseline, ascent i,e. the amount of character rises above the baseline. Leading between lines i,e the difference between the height and ascent.


Figure 1:  shows illustrates some of the common font metrics.

Methods used in FontMetrics class

  • abstract Font getFont():  this will return a Font object representing the current font.
  • public int getAscent(): returns a value representing the ascent of a font in points.
  • public int getDescent(): returns a value representing the descent of a font in points.
  • public getLeading(): returns a value representing the leading of a font in points.
  • public FontMetrics getFontMetrics(): returns current font metrics.

Source Code

/* 
<applet code=fontMetrices width=400 height=450>
</applet>
*/	
import java.applet.*;
import java.awt.*;
public class fontMetrices extends Applet {
    Font f1, f2;
    int ascent, descent, height, leading;
    String one, two, three, four;
    public void init() {
        f1 = new Font("Arial", Font.BOLD, 16);
        f2 = new Font("verdana", Font.BOLD + Font.ITALIC, 14);
    }
    public void paint(Graphics g) {
        g.setFont(f1);
        ascent = g.getFontMetrics().getAscent();
        descent = g.getFontMetrics().getDescent();
        height = g.getFontMetrics().getHeight();
        leading = g.getFontMetrics().getLeading();
        one = "Ascent of font f1 is :" + ascent;
        two = "Descent of font f1 is :" + descent;
        three = "Height of font f1 is :" + height;
        four = "Leading of font f1 is :" + leading;
        g.drawString(one, 20, 20);
        g.drawString(two, 20, 55);
        g.drawString(three, 20, 85);
        g.drawString(four, 20, 120);
    }
}

Output

In this program, two font objects are created – one that is Arial, Bold, 16 points and another one is Verdana, 14 Point. The object f1 is set as the current font. The ascent, height, descent, and leading are determined in this program for the font object drawn on the applet.

Methods used for FontMetrics class

  • int stringWidth(String s): returns full width of string
  • int charWidth(char c): returns width of that character
  • int getHeight(): returns total height of the font.

Another example below makes use of both the Font and FontMetrics classes.

/*
<applet code=TextCentre width=400 height=400>
</applet>
*/
import java.applet.*;
import java.awt.*;
public class TextCentre extends Applet {
    public void paint(Graphics g) {
        String myQuote = "Happiness is an attitude";
        Font objFont = new Font("Times New Roman", Font.BOLD | Font.ITALIC, 24);
        FontMetrics fm = getFontMetrics(objFont);
        g.setFont(objFont);
        int numX = (getSize().width - fm.stringWidth(myQuote)) / 2;
        int numy = getSize().height / 2;
        g.drawString(myQuote, numX, numy);
    }
}

Output

FontMetrics class in Java

Conclusion 

The Font class is used to make text look attractive in the output of a Java Program. The FontMetrics class is used to obtain information about a font.


Recommended Ebook

Solutions Manual to Objects First with Java – A Practical Introduction using BlueJ

Download Now!
Similar Articles