Here you can find the source of drawStringCentered(Graphics2D g, String text, int x, int y, int fontHeight, Color c)
public static void drawStringCentered(Graphics2D g, String text, int x, int y, int fontHeight, Color c)
//package com.java2s; //License from project: Open Source License import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics2D; public class Main { public static void drawStringCentered(Graphics2D g, String text, int x, int y, int fontHeight, Color c) { drawStringCentered(g, text, x, y, fontHeight, c, false); }//from w w w . ja v a2 s . co m public static void drawStringCentered(Graphics2D g, String text, int x, int y, int fontHeight, Color c, boolean bold) { Font f = new Font("Serif", bold ? Font.BOLD : Font.PLAIN, fontHeight); g.setFont(f); FontMetrics fm = g.getFontMetrics(); g.setColor(c); g.drawString(text, x - fm.stringWidth(text) / 2, y); } }