Java Draw Centered Text drawCenteredText(String text, Rectangle rect, Graphics g)

Here you can find the source of drawCenteredText(String text, Rectangle rect, Graphics g)

Description

Draw a block of text centered in or over the rectangle

License

LGPL

Parameter

Parameter Description
text a parameter
rect a parameter
g a parameter

Declaration

public static void drawCenteredText(String text, Rectangle rect, Graphics g) 

Method Source Code


//package com.java2s;
/*//w  ww .j  a v a  2  s  .  c om
 * Copyright (c) 2007-2012 The Broad Institute, Inc.
 * SOFTWARE COPYRIGHT NOTICE
 * This software and its documentation are the copyright of the Broad Institute, Inc. All rights are reserved.
 *
 * This software is supplied without any warranty or guaranteed support whatsoever. The Broad Institute is not responsible for its use, misuse, or functionality.
 *
 * This software is licensed under the terms of the GNU Lesser General Public License (LGPL),
 * Version 2.1 which is available at http://www.opensource.org/licenses/lgpl-2.1.php.
 */

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class Main {
    /**
     * Draw a block of text centered in or over the rectangle
     *
     * @param text
     * @param rect
     * @param g
     */
    public static void drawCenteredText(String text, Rectangle rect, Graphics g) {
        drawCenteredText(text, rect.x, rect.y, rect.width, rect.height, g);

    }

    public static void drawCenteredText(String text, int x, int y, int w, int h, Graphics g) {
        drawCenteredText(text, x, y, w, h, g, null);
    }

    public static void drawCenteredText(String text, int x, int y, int w, int h, Graphics g,
            Color backgroundColor) {
        FontMetrics fontMetrics = g.getFontMetrics();

        Rectangle2D textBounds = fontMetrics.getStringBounds(text, g);
        int xOffset = (int) ((w - textBounds.getWidth()) / 2);
        int yOffset = (int) ((h - textBounds.getHeight()) / 2);

        int xs = x + xOffset;
        int ys = y + h - yOffset - (int) (textBounds.getHeight() / 4);

        if (backgroundColor != null) {
            Graphics gb = g.create();
            gb.setColor(backgroundColor);
            int th = (int) textBounds.getHeight();
            gb.fillRect(xs, ys - 3 * th / 4, (int) textBounds.getWidth(), th);
        }

        g.drawString(text, xs, ys);
    }

    public static void drawCenteredText(char[] chars, int x, int y, int w, int h, Graphics2D g) {

        // Get measures needed to center the message
        FontMetrics fm = g.getFontMetrics();

        // How many pixels wide is the string
        int msg_width = fm.charsWidth(chars, 0, 1);

        // How far above the baseline can the font go?
        int ascent = fm.getMaxAscent();

        // How far below the baseline?
        int descent = fm.getMaxDescent();

        // Use the string width to find the starting point
        int msgX = x + w / 2 - msg_width / 2;

        // Use the vertical height of this font to find
        // the vertical starting coordinate
        int msgY = y + h / 2 - descent / 2 + ascent / 2;

        g.drawChars(chars, 0, 1, msgX, msgY);

    }
}