Here you can find the source of getStringWidth(FontMetrics f, String s)
public static int getStringWidth(FontMetrics f, String s)
//package com.java2s; //License from project: Open Source License import java.awt.FontMetrics; public class Main { public static int getStringWidth(FontMetrics f, String s) { int width = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) < 256) width += f.getWidths()[s.charAt(i)]; else//from ww w . j av a 2 s . c om width += f.getWidths()['a']; } return width; } }