Draw a multi-line text - Java 2D Graphics

Java examples for 2D Graphics:Line

Description

Draw a multi-line text

Demo Code

/*/*from  w ww .  j a v  a 2 s.c o m*/
 * A Collection of Miscellaneous Utilities
 *
 * Copyright 2010
 *      The President and Fellows of Harvard College.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;

public class Main{
    /**
     * Draw a multi-line text
     * 
     * @param g the graphics context
     * @param s the string
     * @param x the X coordinate
     * @param y the Y coordinate (middle)
     * @param justify the justification
     */
    public static void drawText(Graphics g, String s, int x, int y,
            TextJustify justify) {

        FontMetrics fm = g.getFontMetrics();
        int fh = g.getFont().getSize();

        int cur_y = y + (int) (fh / 2 - fm.getDescent()) + 1;
        int lh = (int) fh + 1;

        if (s.indexOf('\n') >= 0) {
            String[] a = s.split("\n");
            cur_y -= ((a.length - 1) * lh) / 2;

            if (justify == TextJustify.LEFT) {
                for (int ai = 0; ai < a.length; ai++) {
                    g.drawString(a[ai], x, cur_y + ai * lh);
                }
            } else if (justify == TextJustify.CENTER) {
                for (int ai = 0; ai < a.length; ai++) {
                    Rectangle2D ar = fm.getStringBounds(a[ai], g);
                    g.drawString(a[ai], x - (int) (ar.getWidth() / 2),
                            cur_y + ai * lh);
                }
            } else if (justify == TextJustify.RIGHT) {
                for (int ai = 0; ai < a.length; ai++) {
                    Rectangle2D ar = fm.getStringBounds(a[ai], g);
                    g.drawString(a[ai], x - (int) ar.getWidth(), cur_y + ai
                            * lh);
                }
            }
        } else {
            if (justify == TextJustify.LEFT) {
                g.drawString(s, x, cur_y);
            } else if (justify == TextJustify.CENTER) {
                Rectangle2D r = fm.getStringBounds(s, g);
                g.drawString(s, x - (int) (r.getWidth() / 2), cur_y);
            } else if (justify == TextJustify.RIGHT) {
                Rectangle2D r = fm.getStringBounds(s, g);
                g.drawString(s, x - (int) r.getWidth(), cur_y);
            }
        }
    }
}

Related Tutorials