Here you can find the source of drawStringCenter(Object o, Rectangle r, Font f, Graphics2D g)
public static void drawStringCenter(Object o, Rectangle r, Font f, Graphics2D g)
//package com.java2s; //License from project: Open Source License import java.awt.Font; import java.awt.Graphics2D; import java.awt.Rectangle; public class Main { public static void drawStringCenter(Object o, Rectangle r, Font f, Graphics2D g) { int w = r.x + (r.width - g.getFontMetrics(f).stringWidth(o.toString())) / 2; int h = r.y + g.getFontMetrics(f).getAscent(); h -= g.getFontMetrics(f).getDescent(); h += (r.height - f.getSize()) / 2; g.setFont(f);/*from w w w .ja v a 2s . c o m*/ g.drawString(o.toString(), w, h); } }