Java Font Text Bounds getTextBox(Font font, FontRenderContext frc, boolean withLeading, String text)

Here you can find the source of getTextBox(Font font, FontRenderContext frc, boolean withLeading, String text)

Description

Returns a rectangle that contains the supplied text with space around the text for an aesthetically pleasing border.

License

Open Source License

Declaration

public static Rectangle2D getTextBox(Font font, FontRenderContext frc, boolean withLeading, String text) 

Method Source Code


//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;
    }
}

Related

  1. getTextBounds(Graphics g, Font font, String text, int x, int y, int halign, int valign)
  2. getTextBounds(Graphics g, String text)
  3. getTextBounds(Graphics graphics, String text)
  4. getTextBounds(Graphics2D g, Font font, String text)
  5. getTextBounds(String text, Graphics2D g2, FontMetrics fm)