Java tutorial
//package com.java2s; import java.awt.*; public class Main { public static int getStringWidth(Graphics g, Font font, String text) { FontMetrics fm = g.getFontMetrics(font); return (int) fm.getStringBounds(text, g).getWidth(); } }