Draws the given multi-lined string on the given graphics with a given horizontal and vertical alignment. - Java 2D Graphics

Java examples for 2D Graphics:Text

Description

Draws the given multi-lined string on the given graphics with a given horizontal and vertical alignment.

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 multi-lined 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// ww w.ja  va  2  s .  c  om
       @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 drawMultilineString(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();

        String[] split = s.split("\n");
        int lines = split.length;
        int alignx = x;
        int aligny = y;

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

        for (String s2 : split) {
            if (clip)
                s2 = clip(font, s2, width);

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

            g.drawString(s2, alignx, aligny);
            aligny += 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