Differences Between Font and FontMetrics in Java



The Font class in Java is responsible for setting screen fonts, which are mapped to characters of the language in specific regions. But a FontMetrics class is said to be a font metrics object that encapsulates the data about the rendering of a specific font on a particular screen.

Font

A Font class can be used to create an instance of a Font object to set the font for drawing text, labels, text fields, buttons, etc, and it can be specified by its name, style, and size.

Syntax

The following is the syntax for Font initialization:

Font font = new Font(String name, int style, int size);

The fonts have a family name, a logical name, and a face name:

  • family name: It is the general name of the font, such as Courier.
  • logical name: It specifies a category of font, such as Monospaced.
  • face name: It specifies a specific font, such as Courier Italic.

Features of Font Class

The following are the key features of the Font class:

  • Font Family: Links the typeface, i.e., "Serif," "SansSerif," or "Monospaced," to the font.
  • Font Style: Determines the appearance of the text, employing styles such as Font.PLAIN, Font.BOLD, and Font.ITALIC.
  • Font Size: Is the size of the text in points (e.g., 10, 15).
  • Creation: Fonts can be created with constructors, for example, new Font("Arial", Font.BOLD, 16), or you can inherit them from base fonts with methods like deriveFont().

When to Use Font?

We can use the Font in the following use cases in Java:

  • When we need to specify how the text should look.
  • For changing the style, family, or size of text.
  • When we're setting up the visual properties of text.

Example of Font

Below is an example of the Font class showing Text with TimesRoman and BOLD style in Java:

import java.awt.*;
import javax.swing.*;
public class FontTest extends JPanel {
   public void paint(Graphics g) {
      g.setFont(new Font("TimesRoman", Font.BOLD, 15));
      g.setColor(Color.blue);
      g.drawString("Welcome to Tutorials Point", 10, 20);
   }
   public static void main(String args[]) {
      JFrame test = new JFrame();
      test.getContentPane().add(new FontTest());
      test.setTitle("Font Test");
      test.setSize(350, 275);
      test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      test.setLocationRelativeTo(null);
      test.setVisible(true);
   }
}

Output

FontMetrics

The FontMetrics class is used to return the specific parameters for a particular Font object. An object of the FontMetrics class is created using the getFontMetrics() method. The methods of the FontMetrics class can provide access to the details of the implementation of a Font object.

Syntax

The following is the syntax for FontMetrics initialization:

Font font = new Font("Times New Roman", Font.ITALIC, 18);
FontMetrics metrics = getFontMetrics(font);

The methods bytesWidth(), charWidth(), charsWidth(), getWidth(), and stringWidth() are used to determine the width of a text object in pixels. These methods are essential for determining the horizontal position of text on the screen.

Features of the FontMetrics Class

The following are the key features of the Font class:

  • Text Measurement: It provides some methods, such as stringWidth(String str), to give the width of a string in pixels.
  • Height Information: Vertical sizes of text are given by methods such as getHeight(), getAscent(), and getDescent().
  • Graphical Context: Tends to be tied to a specific Graphics object or component since the text metric is dependent on the rendering context (screen resolution, display settings, etc.).
  • Usage: It is usually obtained by calling the getFontMetrics(Font font) method on a Graphics object or on some component like JPanel.

When to Use FontMetrics?

We can use the Font in the following use cases in Java:

  • When we need to know how much space the text will occupy.
  • When we're positioning text precisely on the GUI.
  • For implementing the custom text layout.
  • When we need to align text with other elements.

Example of FontMetrics

Below is an example of Font showing Text with Times New Roman and BOLD or ITALIC style in Java:

import java.awt.*;
import javax.swing.*;
public class FontMetricsTest extends JPanel {
   public void paint(Graphics g) {
      String msg = "Tutorials Point";
      Font f = new Font("Times New Roman",Font.BOLD|Font.ITALIC, 15);
      FontMetrics fm = getFontMetrics(f);
      g.setFont(f);
      int x =(getSize().width-fm.stringWidth(msg))/2;
      System.out.println("x= "+x);
      int y = getSize().height/2;
      System.out.println("y= "+y);
      g.drawString(msg, x, y);
   }
   public static void main(String args[]){
      JFrame test = new JFrame();
      test.getContentPane().add(new FontMetricsTest());
      test.setTitle("FontMetrics Test");
      test.setSize(350, 275);
      test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      test.setLocationRelativeTo(null);
      test.setVisible(true);
   }
}

Output

Difference Table

The following are the key differences between a Font and a FontMetrics in Java:

Criteria Font FontMetrics
Purpose Defines visual appearance Provides measurement information
Creation Standalone object Derived from the Graphics context
Usage Specifies how to draw text Helps position text accurately
Information Style, family, size Widths, heights, spacing
Dependence Independent Depends on Font and Graphics
Updated on: 2025-04-29T19:12:54+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements