Java Font Text Bounds getTextBounds(AttributedString text, Graphics2D g2)

Here you can find the source of getTextBounds(AttributedString text, Graphics2D g2)

Description

Returns the bounds for the attributed string.

License

Open Source License

Parameter

Parameter Description
text the attributed string (<code>null</code> not permitted).
g2 the graphics target (<code>null</code> not permitted).

Return

The bounds (never null).

Declaration

public static Rectangle2D getTextBounds(AttributedString text, Graphics2D g2) 

Method Source Code


//package com.java2s;
/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2014, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library 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; either version 2.1 of the License, or
 * (at your option) any later version./*  ww  w .  j a v a  2 s.co m*/
 *
 * This library 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 this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 *
 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
 * Other names may be trademarks of their respective owners.]
 *
 * --------------------
 * AttrStringUtils.java
 * --------------------
 * (C) Copyright 2013, 2014, by Object Refinery Limited and Contributors.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 * Changes:
 * --------
 * 01-Aug-2013 : Version 1, backported from JFreeChart-FSE (DG);
 * 18-Mar-2014 : Added getTextBounds() method (DG);
 * 
 */

import java.awt.Graphics2D;
import java.awt.font.TextLayout;

import java.awt.geom.Rectangle2D;
import java.text.AttributedString;

public class Main {
    /**
     * Returns the bounds for the attributed string.
     * 
     * @param text  the attributed string (<code>null</code> not permitted).
     * @param g2  the graphics target (<code>null</code> not permitted).
     * 
     * @return The bounds (never <code>null</code>).
     * 
     * @since 1.0.18
     */
    public static Rectangle2D getTextBounds(AttributedString text, Graphics2D g2) {
        TextLayout tl = new TextLayout(text.getIterator(), g2.getFontRenderContext());
        return tl.getBounds();
    }
}

Related

  1. getStringBounds(Graphics2D g2, String s)
  2. getStringBounds(Graphics2D g2, String str, float x, float y)
  3. getStringBounds(Graphics2D g2d, String text, int x, int y)
  4. getStringBounds(String s, Graphics g)
  5. getStringBounds(String str, java.awt.Font font)
  6. getTextBounds(final Graphics2D graphics, final String text, final Font font)
  7. getTextBounds(final String text, final Graphics g, final Font font)
  8. getTextBounds(Graphics g, Font font, String text, int x, int y, int halign, int valign)
  9. getTextBounds(Graphics g, String text)