Here you can find the source of centerStringY(String s, int h, Graphics2D g2d)
public static int centerStringY(String s, int h, Graphics2D g2d)
//package com.java2s; //License from project: Open Source License import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.font.FontRenderContext; import java.awt.font.GlyphVector; public class Main { public static int centerStringY(String s, int h, Graphics2D g2d) { FontRenderContext frc = g2d.getFontRenderContext(); GlyphVector gv = g2d.getFont().createGlyphVector(frc, s); Rectangle vb = gv.getVisualBounds().getBounds(); return h / 2 - vb.height / 2 - vb.y; }/* ww w . ja va2 s.c om*/ }