Here you can find the source of centerStringX(String s, int w, Graphics2D g2d)
public static int centerStringX(String s, int w, Graphics2D g2d)
//package com.java2s; //License from project: Open Source License import java.awt.FontMetrics; import java.awt.Graphics2D; public class Main { public static int centerStringX(String s, int w, Graphics2D g2d) { FontMetrics fm = g2d.getFontMetrics(g2d.getFont()); return (w - fm.stringWidth(s)) / 2; }/*www . jav a 2 s. com*/ public static int stringWidth(String s, Graphics2D g2d) { FontMetrics fm = g2d.getFontMetrics(g2d.getFont()); return fm.stringWidth(s); } }