Here you can find the source of paintCenteredString(Graphics2D g, String str, Font font, int centerX, int centerY)
public static void paintCenteredString(Graphics2D g, String str, Font font, int centerX, int centerY)
//package com.java2s; /**//from w w w.ja v a 2 s . c o m * This software is released as part of the Pumpernickel project. * * All com.pump resources in the Pumpernickel project are distributed under the * MIT License: * https://raw.githubusercontent.com/mickleness/pumpernickel/master/License.txt * * More information about the Pumpernickel project is available here: * https://mickleness.github.io/pumpernickel/ */ import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; public class Main { /** * Paint a String centered at a given (x,y) coordinate. */ public static void paintCenteredString(Graphics2D g, String str, Font font, int centerX, int centerY) { g.setFont(font); FontMetrics fm = g.getFontMetrics(); Rectangle2D r = fm.getStringBounds(str, g); float x = (float) (centerX - r.getWidth() / 2); float y = (float) (centerY - r.getHeight() / 2 - r.getY()); g.drawString(str, x, y); } }