Java FontMetrics Factory Methods

java.awt.FontMetrics is an abstract class that maintains the metadata of a font. The metadata includes such information how font letters should adjust with the previous line and next line. The characters like h, p, l, j etc have some special attention in adjusting with other characters in the line. The FontMetrics object can give the width, in pixels, of a character or string in a particular font. This data is sometimes required to place the string in the middle or a frame in a specified location on the monitor.

Following is the class signature

public abstract class FontMetrics extends Object implements Serializable

Following are a few important methods of FontMetrics class

  1. int charWidth(char ch): Returns the width of the character ch in pixels.
  2. int getAscent(): Returns the ascent for the specific font.
  3. int getDescent(): Returns the descent for the specific font.
  4. int getLeading(): Returns the leading space of the font. Leading space is the amount of space between ascent and descent of two side-by-side lines.
  5. int getHeight(): Returns the distance between two baselines. It is the total of ascent, descent and leading of a font.

Following is the meaning of certain words used with metadata.

  • Baseline: An imaginary line on which the text lies. Infact, the y coordinate of drawString() is the position of the baseline on which the character is placed.
  • Descent: The distance below the baseline a specific character slips. Some characters like g, p and j extend below the baseline.
  • Ascent: The distance above the baseline a particular character extends. A few characters like d, h and k has more ascent than the other letters like a and c etc.
  • Leading: It is the space between the descent of one line and the ascent of the next line. Every font will have a leading space else the letters g and j may touch the letters d and k of next line.
Example on Java FontMetrics Factory Methods retrieving metadata
import java.awt.*;
public class Metadata  extends Frame
   public Metadata ()   
	setTitle("Metadata of Monospaced Font");
	setSize(300, 300);
   public void paint(Graphics g)   
        Font f1 = new Font("Monospaced", Font.BOLD, 16);
        String str = "";      
        char bigx = 'X';
        char smallx = 'x';

	FontMetrics fm = getFontMetrics(f1);   
	g.drawString("f1font Ascent: " + fm.getAscent(), 60, 60);
	g.drawString("f1font Descent: " + fm.getDescent(), 60, 90);
	g.drawString("f1font Leading: " + fm.getLeading(), 60, 120);
	g.drawString("f1font Height: " + fm.getHeight(), 60, 150);
	g.drawString("X width in pixels: " + fm.charWidth(bigx), 60, 190);
	g.drawString("x width in pixels: " + fm.charWidth(smallx), 60, 230);
	g.drawString("str string wdith in pixels: " + fm.stringWidth(str), 60, 250);
   public static void main(String args[ ])   
        new Metadata();

Java FontMetrics Factory Methods

The methods getAscent(), getDescent() and getLeading() methods return the ascent, descent and leading of the font f1 which will change as per the font object.

Factory Methods

In java.awt package, the classes like Graphics, FontMetrics and Toolkit are abstract classes. Their object cannot be created directly with new keyword (operator). We can get the object indirectly.

           FontMetrics fm = getFontMetrics(f1);

getFontMetrics() is a static method of FontMetrics class that returns an object of FontMetrics. Now, fm is an object of FontMetrics class with which all the methods of FontMetrics can be called. Such methods which return an object are called as factory methods. getFontMetrics() is a factory method. Lot of factory methods exist in Java like getDefaultToolkit() that returns an object of Toolkit abstract class. We get slowly one by one.

The frame you get do not close when clicked over the close icon on the title bar of the frame. It requires extra code close icon to work.

Leave a Comment

Your email address will not be published.