Java Font Text Bounds getTextBounds(Graphics g, String text)

Here you can find the source of getTextBounds(Graphics g, String text)

Description

Returns text bounds for specified graphics context.

License

Open Source License

Parameter

Parameter Description
g Graphics context.
text Text to be drawn.

Return

Bounds of the text.

Declaration

public static Rectangle2D getTextBounds(Graphics g, String text) 

Method Source Code

//package com.java2s;
/* ODISP -- Message Oriented Middleware
 * Copyright (C) 2003-2005 Valentin A. Alekseev
 * Copyright (C) 2003-2005 Andrew A. Porohin 
 * /*w  ww . ja va 2 s. co m*/
 * ODISP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, version 2.1 of the License.
 * 
 * ODISP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with ODISP.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.FontMetrics;
import java.awt.Graphics;

import java.awt.geom.Rectangle2D;

public class Main {
    /**
     * Returns text bounds for specified graphics context.
     * 
     * @param g Graphics context.
     * @param text Text to be drawn.
     * @return Bounds of the text.
     */
    public static Rectangle2D getTextBounds(Graphics g, String text) {
        FontMetrics fm = g.getFontMetrics();
        return fm.getStringBounds(text, g);
    }
}

Related

  1. getStringBounds(String str, java.awt.Font font)
  2. getTextBounds(AttributedString text, Graphics2D g2)
  3. getTextBounds(final Graphics2D graphics, final String text, final Font font)
  4. getTextBounds(final String text, final Graphics g, final Font font)
  5. getTextBounds(Graphics g, Font font, String text, int x, int y, int halign, int valign)
  6. getTextBounds(Graphics graphics, String text)
  7. getTextBounds(Graphics2D g, Font font, String text)
  8. getTextBounds(String text, Graphics2D g2, FontMetrics fm)
  9. getTextBox(Font font, FontRenderContext frc, boolean withLeading, String text)