Here you can find the source of centerString(Graphics g, String s, Font f, int w0, int w, int h)
public static void centerString(Graphics g, String s, Font f, int w0, int w, int h)
//package com.java2s; import java.awt.Font; import java.awt.Graphics; public class Main { public static void centerString(Graphics g, String s, Font f, int w0, int w, int h) { int p, sw; g.setFont(f);//w ww.j ava 2 s. c o m sw = g.getFontMetrics(f).stringWidth(s); g.drawString(s, w0 + (w - sw) / 2, h); } public static void centerString(Graphics g, String s, int w0, int w, int h) { int p, sw; Font f = g.getFont(); g.setFont(f); sw = g.getFontMetrics(f).stringWidth(s); g.drawString(s, w0 + (w - sw) / 2, h); } }