Draws the given String on the given Graphics2D aligned to the top center of the bounding rectangle. - Java 2D Graphics

Java examples for 2D Graphics:Text

Description

Draws the given String on the given Graphics2D aligned to the top center of the bounding rectangle.

Demo Code


//package com.java2s;
import java.awt.Graphics2D;

import java.awt.FontMetrics;

public class Main {
    public static final int ALIGN_LEFT = 0;
    public static final int ALIGN_CENTER = 1;
    public static final int ALIGN_RIGHT = 2;
    public static final int ALIGN_TOP = 3;
    public static final int ALIGN_BOTTOM = 4;

    /**   Draws the given String on the given Graphics2D aligned to the top center of the bounding rectangle.
       @param g The Graphics2D object on which to draw the string
       @param s The String to draw//  w w w. j a v a  2s .  co  m
       @param x The minimum x coordinate of the rectangle in which to draw the string
       @param y The minimum y coordinate of the rectangle in which to draw the string
       @param width The width of the rectangle in which to draw the string
       @param height The height the rectangle in which to draw the string
       @param clip True if the string should not go past the rectangle if it is too wide
     */
    public static void drawHorizontalCenteredString(Graphics2D g, String s,
            int x, int y, int width, int height, boolean clip) {
        FontMetrics font = g.getFontMetrics();
        if (clip)
            s = clip(font, s, width);

        g.drawString(s, x + width / 2 - font.stringWidth(s) / 2,
                y + font.getAscent());
    }

    /**   Used for cutting off text that is too wide.
       @param font The FontMetrice displaying the string
       @param s The String to be displayed
       @param width The cutoff width
       @return The string cut to fit into the width with "..." added to the end.
     */
    private static String clip(FontMetrics font, String s, int width) {
        if (font.stringWidth(s) <= width)
            return s;
        else {
            String clip = s.substring(0, s.length() - 1);

            while (font.stringWidth(clip + "...") > width
                    && clip.length() > 0)
                clip = clip.substring(0, clip.length() - 1);

            return clip + "...";
        }
    }

    /**   Draws the given string on the given graphics with a given horizontal and vertical alignment.
       @param g The Graphics2D object on which to draw the string
       @param s The String to draw
       @param x The minimum x coordinate of the rectangle in which to draw the string
       @param y The minimum y coordinate of the rectangle in which to draw the string
       @param width The width of the rectangle in which to draw the string
       @param height The height the rectangle in which to draw the string
       @param alignHorizontal The horizontal alignment of the string. Should be ALIGN_LEFT, ALIGN_CENTER, or ALIGN_RIGHT
       @param alignVertical The vertical alignment of the string. Should be ALIGN_TOP, ALIGN_CENTER, or ALIGN_BOTTOM
       @param clip True if the string should not go past the rectangle if it is too wide
     */
    public static void drawString(Graphics2D g, String s, int x, int y,
            int width, int height, int alignHorizontal, int alignVertical,
            boolean clip) {
        if (s.length() == 0)
            return;

        FontMetrics font = g.getFontMetrics();
        if (clip)
            s = clip(font, s, width);

        int alignx = x;
        int aligny = y;

        switch (alignHorizontal) {
        case ALIGN_LEFT:
        case ALIGN_CENTER:
        case ALIGN_RIGHT:
            alignx += alignHorizontal * (width - font.stringWidth(s)) / 2;
            break;
        default:
            throw new IllegalArgumentException(
                    "Invalid horizontal alignment!");
        }

        switch (alignVertical) {
        case ALIGN_TOP:
            aligny += font.getAscent();
            break;
        case ALIGN_CENTER:
            aligny += height / 2 + (font.getAscent() - font.getDescent())
                    / 2;
            break;
        case ALIGN_BOTTOM:
            aligny += height - font.getDescent();
            break;
        default:
            throw new IllegalArgumentException(
                    "Invalid vertical alignment!");
        }

        g.drawString(s, alignx, aligny);
    }
}

Related Tutorials