Java Swing Tutorial - Java Graphics.getFontMetrics()








Syntax

Graphics.getFontMetrics() has the following syntax.

public FontMetrics getFontMetrics()

Example

In the following code shows how to use Graphics.getFontMetrics() method.

//from   ww  w  .jav  a  2  s .  com
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JPanel {

  public void paint(Graphics g) {
  
    int fontSize = 20;

    g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
    
    
    FontMetrics fm = g.getFontMetrics();

    String s = "www.java2s.com";
    
    int stringWidth = fm.stringWidth(s);

    int w = 200;
    int h = 200;
    
    int x = (w - stringWidth) / 2;
    int baseline = fm.getMaxAscent() +
                       (h - (fm.getAscent() + fm.getMaxDecent()))/2;
    int ascent  = fm.getMaxAscent();
    int descent = fm.getMaxDecent();
    int fontHeight = fm.getMaxAscent() + fm.getMaxDecent();

    g.setColor(Color.white);
    g.fillRect(x, baseline-ascent , stringWidth, fontHeight);

    g.setColor(Color.gray);
    g.drawLine(x, baseline, x+stringWidth, baseline);

    g.setColor(Color.red);
    g.drawLine(x, baseline+descent, x+stringWidth, baseline+descent);

    g.setColor(Color.blue);
    g.drawLine(x, baseline-ascent,
               x+stringWidth, baseline-ascent);
    g.setColor(Color.black);
    g.drawString(s, x, baseline);

  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.add(new Main());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(20,20, 500,500);
    frame.setVisible(true);
  }
}