Here you can find the source of getTextBounds(final Graphics2D graphics, final String text, final Font font)
public static Rectangle2D getTextBounds(final Graphics2D graphics, final String text, final Font font)
//package com.java2s; /*//from w ww . j a v a 2 s .com * Copyright ? 2016 spypunk <spypunk@gmail.com> * * This work is free. You can redistribute it and/or modify it under the * terms of the Do What The Fuck You Want To Public License, Version 2, * as published by Sam Hocevar. See the COPYING file for more details. */ import java.awt.Font; import java.awt.Graphics2D; import java.awt.font.FontRenderContext; import java.awt.font.GlyphVector; import java.awt.geom.Rectangle2D; public class Main { public static Rectangle2D getTextBounds(final Graphics2D graphics, final String text, final Font font) { final FontRenderContext frc = graphics.getFontRenderContext(); final GlyphVector gv = font.createGlyphVector(frc, text); return gv.getVisualBounds(); } }