Returns the point at which to draw a string so that it appears aligned within a given rectangle. - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

Returns the point at which to draw a string so that it appears aligned within a given rectangle.

Demo Code

/*//ww w .  j av a 2s. c o m
 * Copyright (C) 2012 Jason Gedge <http://www.gedge.ca>
 *
 * This file is part of the OpGraph project.
 * 
 * This program 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.
 * 
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

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

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.font.LineMetrics;
import java.awt.geom.Rectangle2D;

import javax.swing.SwingConstants;

public class Main {
    /**
     * Returns the point at which to draw a string so that it appears aligned
     * within a given rectangle.
     * 
     * @param g  the graphics context to work with
     * @param txt  the string
     * @param rect  the rectangle to place the string in
     * 
     * @param horizontalAlignment  the horizontal alignment; one of
     *             {@link SwingConstants#LEFT}, {@link SwingConstants#CENTER}
     *             or {@link SwingConstants#RIGHT}
     *                
     * @param verticalAlignment  the vertical alignment; one of
     *             {@link SwingConstants#TOP}, {@link SwingConstants#CENTER}
     *             or {@link SwingConstants#BOTTOM}
     * 
     * @return the point at which text should be drawn so that it is aligned
     *         appropriately with the given rectangle.
     */
    public static Point placeTextInRectangle(Graphics g, String txt,
            Rectangle rect, int horizontalAlignment, int verticalAlignment) {
        final FontMetrics fm = g.getFontMetrics();
        final LineMetrics lm = fm.getLineMetrics(txt, g);
        final Rectangle2D r = fm.getStringBounds(txt, g);

        int x = rect.x;
        int y = rect.y;

        switch (horizontalAlignment) {
        case SwingConstants.CENTER:
            x += (rect.width - r.getWidth()) * 0.5;
            break;
        case SwingConstants.RIGHT:
            x += rect.width - r.getWidth();
            break;
        }

        switch (verticalAlignment) {
        case SwingConstants.TOP:
            y += lm.getAscent();
            break;
        case SwingConstants.CENTER:
            y += (rect.height - r.getHeight()) * 0.5 + lm.getAscent();
            break;
        case SwingConstants.BOTTOM:
            y += rect.height - lm.getDescent();
            break;
        }

        return new Point(x, y);
    }
}

Related Tutorials