Java Graphics Draw Multiline String drawMultilineText(final Graphics2D gc, final int x, int y, final String text)

Here you can find the source of drawMultilineText(final Graphics2D gc, final int x, int y, final String text)

Description

Draw multi-line text

License

Open Source License

Parameter

Parameter Description
gc AWT Graphics context. Font must be set.
x X position of text (left edge)
y Y position of text's baseline
text Text to draw, may contain '\n'

Declaration

public static void drawMultilineText(final Graphics2D gc, final int x, int y, final String text) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2015-2016 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/

import java.awt.Graphics2D;

public class Main {
    /** Draw multi-line text
     *  @param gc AWT Graphics context. Font must be set.
     *  @param x X position of text (left edge)
     *  @param y Y position of text's baseline
     *  @param text Text to draw, may contain '\n'
     *///  w  w  w. j av  a2 s. co  m
    public static void drawMultilineText(final Graphics2D gc, final int x, int y, final String text) {
        final int line_height = gc.getFontMetrics().getHeight();
        for (String line : text.split("\n")) {
            gc.drawString(line, x, y);
            y += line_height;
        }
    }
}

Related

  1. draw(String _str, Graphics2D _g2, int _nX0, int _nY0, int _nX1, int _nY1)
  2. drawMultilineString(Graphics g, String s, int alignment, Rectangle r, boolean print)
  3. drawMultiLineText(String text, Graphics G, int x, int y)
  4. drawRightMultiLineText(String text, Graphics G, int x, int y)
  5. drawString(Graphics g, String s, int x, int y)
  6. drawStringBiggest(final Graphics2D g2d, final Dimension dim, final String drawString)