Java Font Text Bounds getTextBounds(Graphics g, Font font, String text, int x, int y, int halign, int valign)

Here you can find the source of getTextBounds(Graphics g, Font font, String text, int x, int y, int halign, int valign)

Description

get Text Bounds

License

Open Source License

Declaration

static public Rectangle getTextBounds(Graphics g, Font font, String text, int x, int y, int halign,
            int valign) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * This file is part of logisim-evolution.
 *
 *   logisim-evolution is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   logisim-evolution 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 General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with logisim-evolution.  If not, see <http://www.gnu.org/licenses/>.
 *
 *   Original code by Carl Burch (http://www.cburch.com), 2011.
 *   Subsequent modifications by ://w  w  w  .ja  v a 2  s. c  om
 *     + Haute ?cole Sp?cialis?e Bernoise
 *       http://www.bfh.ch
 *     + Haute ?cole du paysage, d'ing?nierie et d'architecture de Gen?ve
 *       http://hepia.hesge.ch/
 *     + Haute ?cole d'Ing?nierie et de Gestion du Canton de Vaud
 *       http://www.heig-vd.ch/
 *   The project is currently maintained by :
 *     + REDS Institute - HEIG-VD
 *       Yverdon-les-Bains, Switzerland
 *       http://reds.heig-vd.ch
 *******************************************************************************/

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

import java.awt.Rectangle;

public class Main {
    public static final int H_CENTER = 0;
    public static final int H_RIGHT = 1;
    public static final int V_TOP = -1;
    public static final int V_CENTER = 0;
    public static final int V_BASELINE = 1;
    public static final int V_BOTTOM = 2;
    public static final int V_CENTER_OVERALL = 3;

    static public Rectangle getTextBounds(Graphics g, Font font, String text, int x, int y, int halign,
            int valign) {
        if (g == null)
            return new Rectangle(x, y, 0, 0);
        Font oldfont = g.getFont();
        if (font != null)
            g.setFont(font);
        Rectangle ret = getTextBounds(g, text, x, y, halign, valign);
        if (font != null)
            g.setFont(oldfont);
        return ret;
    }

    static public Rectangle getTextBounds(Graphics g, String text, int x, int y, int halign, int valign) {
        if (g == null)
            return new Rectangle(x, y, 0, 0);
        FontMetrics mets = g.getFontMetrics();
        int width = mets.stringWidth(text);
        int ascent = mets.getAscent();
        int descent = mets.getDescent();
        int height = ascent + descent;

        Rectangle ret = new Rectangle(x, y, width, height);
        switch (halign) {
        case H_CENTER:
            ret.translate(-(width / 2), 0);
            break;
        case H_RIGHT:
            ret.translate(-width, 0);
            break;
        default:
            ;
        }
        switch (valign) {
        case V_TOP:
            break;
        case V_CENTER:
            ret.translate(0, -(ascent / 2));
            break;
        case V_CENTER_OVERALL:
            ret.translate(0, -(height / 2));
            break;
        case V_BASELINE:
            ret.translate(0, -ascent);
            break;
        case V_BOTTOM:
            ret.translate(0, -height);
            break;
        default:
            ;
        }
        return ret;
    }
}

Related

  1. getStringBounds(String s, Graphics g)
  2. getStringBounds(String str, java.awt.Font font)
  3. getTextBounds(AttributedString text, Graphics2D g2)
  4. getTextBounds(final Graphics2D graphics, final String text, final Font font)
  5. getTextBounds(final String text, final Graphics g, final Font font)
  6. getTextBounds(Graphics g, String text)
  7. getTextBounds(Graphics graphics, String text)
  8. getTextBounds(Graphics2D g, Font font, String text)
  9. getTextBounds(String text, Graphics2D g2, FontMetrics fm)