This will draw a paragraph centered in a box where drawing each line either starts at the bottom or top. - Java 2D Graphics

Java examples for 2D Graphics:Line

Description

This will draw a paragraph centered in a box where drawing each line either starts at the bottom or top.

Demo Code


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

public class Main {
    /**/*from  ww w . j a v a  2  s .c  o m*/
     * This will draw a paragraph centered in a box where drawing each line
     * either starts at the bottom or top.
     * 
     * @param gr
     *        The graphics object to draw on.
     * @param s
     *        The paragraph to draw.
     * @param left
     *        The left side of the box.
     * @param top
     *        The top side of the box.
     * @param right
     *        The right side of the box.
     * @param bottom
     *        The bottom side of the box.
     * @param startAtTop
     *        Whether to start drawing each line at the top or bottom of the box.
     * @return The bottom coordinate of the paragraph drawn.
     */
    public static float drawString(Graphics2D gr, String s, float left,
            float top, float right, float bottom, boolean startAtTop) {
        String[] lines = s.split("\n");
        FontMetrics metrics = gr.getFontMetrics();

        final float cushion = 4f;
        float fontHeight = metrics.getHeight();
        float totalHeight = (fontHeight + cushion);
        float paraBottom = top + (lines.length * totalHeight);
        float y = (startAtTop ? top + fontHeight : bottom);
        float cx = (left + right) * 0.5f;
        float width, x;
        int line;

        for (int i = 0; i < lines.length; i++) {
            // The current line to draw
            line = (startAtTop ? i : lines.length - i - 1);
            // The width of the line in pixels
            width = metrics.stringWidth(lines[line]);
            // The starting x centered in the bounds.
            x = cx - (width * 0.5f);
            // Draw the line
            gr.drawString(lines[line], x, y);
            // Increase/Decrease y based on whether it started at the top.
            y += (startAtTop ? totalHeight : -totalHeight);
        }

        // Return the bottom of the paragraph
        // based on whether it started at the top.
        return (startAtTop ? paraBottom : bottom);
    }
}

Related Tutorials