Here you can find the source of drawStringCenter(Graphics2D g, String text, Rectangle rect, int fontHeight, Color c)
public static void drawStringCenter(Graphics2D g, String text, Rectangle rect, int fontHeight, Color c)
//package com.java2s; //License from project: Open Source License import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics2D; import java.awt.Rectangle; public class Main { public static void drawStringCenter(Graphics2D g, String text, Rectangle rect, int fontHeight, Color c) { Font f = new Font("Serif", Font.PLAIN, fontHeight); g.setFont(f);/*from www.ja va 2 s .c om*/ FontMetrics fm = g.getFontMetrics(); g.setColor(c); g.drawString(text, (int) rect.getCenterX() - fm.stringWidth(text) / 2, rect.y + rect.height); } }