Here you can find the source of drawCenteredString(Graphics g, String str, int x, int y)
public static void drawCenteredString(Graphics g, String str, int x, int y)
//package com.java2s; //License from project: Open Source License import java.awt.FontMetrics; import java.awt.Graphics; public class Main { public static void drawCenteredString(Graphics g, String str, int x, int y) { FontMetrics fm = g.getFontMetrics(); int strW = fm.stringWidth(str), strH = fm.getHeight(); x -= strW / 2;/*from www . ja v a 2 s. c o m*/ y += strH / 5; g.drawString(str, x, y); } }