Here you can find the source of getStringBounds(Graphics2D g2, String str, float x, float y)
public static Rectangle getStringBounds(Graphics2D g2, String str, float x, float y)
//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 Rectangle getStringBounds(Graphics2D g2, String str, float x, float y) { FontRenderContext frc = g2.getFontRenderContext(); GlyphVector gv = g2.getFont().createGlyphVector(frc, str); return gv.getPixelBounds(null, x, y); }/*from w w w .ja va2s . co m*/ }