Here you can find the source of getStringSize(Graphics g, String text, Font font)
public static Dimension getStringSize(Graphics g, String text, Font font)
//package com.java2s; //License from project: LGPL import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; public class Main { public static Dimension getStringSize(Graphics g, String text, Font font) { if (text == null || text.isEmpty()) return new Dimension(); FontMetrics fm;/*www . j a va2 s .com*/ if (font == null) { fm = g.getFontMetrics(); } else { fm = g.getFontMetrics(font); } int w = fm.stringWidth(text); int h = Math.max(fm.getHeight(), fm.getAscent() + fm.getDescent()); return new Dimension(w, h); } }