Java Utililty Methods Font Text Bounds

List of utility methods to do Font Text Bounds

Description

The list of methods to do Font Text Bounds are organized into topic(s).

Method

Rectangle2DgetTextBounds(Graphics g, String text)
Returns text bounds for specified graphics context.
FontMetrics fm = g.getFontMetrics();
return fm.getStringBounds(text, g);
RectanglegetTextBounds(Graphics graphics, String text)
Get the bounds for drawing the specified text in the specified graphics context.
FontMetrics metrics = graphics.getFontMetrics();
Rectangle2D textBounds = metrics.getStringBounds(text, graphics);
int textAscend = metrics.getMaxAscent();
return new Rectangle(0, textAscend, (int) textBounds.getWidth(), textAscend + metrics.getMaxDescent());
Rectangle2DgetTextBounds(Graphics2D g, Font font, String text)
Get Bounding box for some text
FontMetrics metric = g.getFontMetrics(font);
return metric.getStringBounds(text, g);
Rectangle2DgetTextBounds(String text, Graphics2D g2, FontMetrics fm)
Returns the bounds for the specified text.
double width = fm.stringWidth(text);
double height = fm.getHeight();
return new Rectangle2D.Double(0.0, -fm.getAscent(), width, height);
Rectangle2DgetTextBox(Font font, FontRenderContext frc, boolean withLeading, String text)
Returns a rectangle that contains the supplied text with space around the text for an aesthetically pleasing border.
Rectangle2D bounds = getBounds(new TextLayout(text, font, frc), withLeading);
return pad(bounds, 2 * HEADER_BORDER, 2 * HEADER_BORDER);