Java tutorial
//package com.java2s; import java.awt.*; import java.awt.geom.Rectangle2D; public class Main { public static void drawCenteredString(Graphics g, String str, int x, int y) { FontMetrics metrics = g.getFontMetrics(g.getFont()); Rectangle2D rect = metrics.getStringBounds(str, g); int w = (int) (rect.getWidth()); int h = (int) (rect.getHeight()); g.drawString(str, x - w / 2, y + h / 2); } }