Here you can find the source of getTextBox(Font font, FontRenderContext frc, boolean withLeading, String text)
public static Rectangle2D getTextBox(Font font, FontRenderContext frc, boolean withLeading, String text)
//package com.java2s; // http://github.com/samskivert/viztool/blob/master/LICENSE import java.awt.Font; import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; import java.awt.geom.Rectangle2D; public class Main { /**/*from w ww .j av a 2s . c om*/ * The number of points surrounding the name of the chain. */ public static double HEADER_BORDER = 3; /** * Returns a rectangle that contains the supplied text with space around the text for an * aesthetically pleasing border. */ public static Rectangle2D getTextBox(Font font, FontRenderContext frc, boolean withLeading, String text) { Rectangle2D bounds = getBounds(new TextLayout(text, font, frc), withLeading); // incorporate room for the border in the bounds return pad(bounds, 2 * HEADER_BORDER, 2 * HEADER_BORDER); } protected static Rectangle2D getBounds(TextLayout layout, boolean withLeading) { Rectangle2D lbounds = layout.getBounds(); return new Rectangle2D.Double(lbounds.getX(), lbounds.getY(), lbounds.getWidth(), (withLeading ? layout.getLeading() : 0) + layout.getAscent() + layout.getDescent()); } protected static Rectangle2D pad(Rectangle2D rect, double width, double height) { rect.setRect(rect.getX(), rect.getY(), rect.getWidth() + width, rect.getHeight() + height); return rect; } }