Java Draw String drawString(Graphics g, String s, int x, int y, int width, int height)

Here you can find the source of drawString(Graphics g, String s, int x, int y, int width, int height)

Description

Draws a string starting at coordinate x,y and try to fit it in the given width and height.

License

Open Source License

Parameter

Parameter Description
g a parameter
s a parameter
x a parameter
y a parameter
width a parameter
height a parameter

Declaration

public static int drawString(Graphics g, String s, int x, int y, int width, int height) 

Method Source Code

//package com.java2s;
/*/*from  www .  j  a v  a  2 s. co m*/
 * Spirit, a study/biosample management tool for research.
 * Copyright (C) 2018 Idorsia Pharmaceuticals Ltd., Hegenheimermattweg 91,
 * CH-4123 Allschwil, Switzerland.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 * @author Joel Freyss
 */

import java.awt.Graphics;

public class Main {
    /**
     * Draws a string starting at coordinate x,y and try to fit it in the given width and height.
     * The string is automatically wrapped on new lines and when needed
     * @param g
     * @param s
     * @param x
     * @param y
     * @param width
     * @param height
     * @return
     */
    public static int drawString(Graphics g, String s, int x, int y, int width, int height) {
        return drawString(g, s, x, y, width, height, true);
    }

    public static int drawString(Graphics g, String s, int x, int y, int width, int height, boolean autowrap) {
        if (s == null || s.length() == 0)
            return y;

        if (height <= 0)
            height = 1;
        int offset = 0;
        int index = 1;
        int cy = y;
        for (; index < s.length() && cy < y + height; index++) {
            if (s.charAt(index) == '\n') {
                g.drawString(s.substring(offset, index), x, cy);
                offset = index;
                cy += g.getFont().getSize() - 1;
            } else if (g.getFontMetrics().stringWidth(s.substring(offset, index)) > width) {
                g.drawString(s.substring(offset, index - 1), x, cy);
                if (!autowrap) {
                    //skip to next line
                    index = s.indexOf('\n', index + 1);
                    if (index < 0)
                        index = s.length();
                }
                offset = index - 1;
                cy += g.getFont().getSize() - 1;
            }
        }
        if (cy < y + height) {
            g.drawString(s.substring(offset), x, cy);
        }
        return cy;
    }
}

Related

  1. drawString(final Graphics g, final String text, final int x, final int y)
  2. drawString(Graphics g, String s, int alignment, Rectangle r)
  3. drawString(Graphics g, String s, int x, int y, int width, int height)
  4. drawString(Graphics g, String text, int underlinedChar, int x, int y)
  5. drawString(Graphics g, String text, int underlinedChar, int x, int y)
  6. drawString(Graphics g, String text, int underlinedChar, int x, int y)
  7. drawString(Graphics g, String text, int x, int y)