Java Graphics Draw String drawWrappedText(Graphics2D g2, float x, float y, float width, String text)

Here you can find the source of drawWrappedText(Graphics2D g2, float x, float y, float width, String text)

Description

Draw given text to given Graphics2D view starting at x,y and wrapping lines at width pixels.

License

Open Source License

Declaration

public static float drawWrappedText(Graphics2D g2, float x, float y, float width, String text) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.BasicStroke;
import java.awt.Color;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;

import java.text.AttributedCharacterIterator;
import java.text.AttributedString;

public class Main {
    /** Draw given text to given Graphics2D view starting at x,y and wrapping
     * lines at width pixels. If the y coordinate is negative, take it as
     * maximum y position. Return new y.
     *///  ww  w.  jav  a  2 s . c o  m
    public static float drawWrappedText(Graphics2D g2, float x, float y, float width, String text) {

        if (text == null || text.length() == 0)
            return y;

        FontRenderContext frc = g2.getFontRenderContext();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Anti-alias!
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setStroke(new BasicStroke(2.0f)); // 2-pixel lines

        AttributedString astr = new AttributedString(text);
        astr.addAttribute(TextAttribute.FONT, g2.getFont(), 0, text.length());

        if (y < 0) {
            // Calculate real Y by laying out the text without output
            y = -y;
            AttributedCharacterIterator chars = astr.getIterator();
            LineBreakMeasurer measurer = new LineBreakMeasurer(chars, frc);
            while (measurer.getPosition() < chars.getEndIndex()) {
                TextLayout layout = measurer.nextLayout(width);
                y -= layout.getAscent();
                float dx = layout.isLeftToRight() ? 0 : (width - layout.getAdvance());
                float sw = (float) layout.getBounds().getWidth();
                float sh = (float) layout.getBounds().getHeight();
                y -= layout.getDescent() + layout.getLeading();
            }
        }

        AttributedCharacterIterator chars = astr.getIterator();
        LineBreakMeasurer measurer = new LineBreakMeasurer(chars, frc);
        while (measurer.getPosition() < chars.getEndIndex()) {
            TextLayout layout = measurer.nextLayout(width);

            y += layout.getAscent();
            float dx = layout.isLeftToRight() ? 0 : (width - layout.getAdvance());

            float sw = (float) layout.getBounds().getWidth();
            float sh = (float) layout.getBounds().getHeight();
            Shape sha = layout.getOutline(AffineTransform.getTranslateInstance(x, y));

            Color oldColor = g2.getColor();
            g2.setColor(g2.getBackground());
            g2.draw(sha);

            g2.setColor(oldColor);
            g2.fill(sha);

            y += layout.getDescent() + layout.getLeading();
        }
        return y;
    }
}

Related

  1. drawTextInBoundedArea(Graphics2D g2d, int x1, int y1, int x2, int y2, String text)
  2. drawTuplet(Graphics g, int x, int y, int x2, int y2, int bi, String s1, String s2)
  3. drawUnderlineCharAt(Graphics g, String text, int underlinedIndex, int x, int y)
  4. drawVerticalText(Graphics2D graphics, Font font, Dimension2D dimension, String text)
  5. drawVerticalTextCenter(Graphics2D g2, String str, int x, int y)
  6. paintCenteredString(Graphics2D g, String str, Font font, int centerX, int centerY)
  7. paintOutline(Graphics g, String s, int textX, int textY, int thickness)
  8. paintShadowTitleFat(Graphics g, String title, int x, int y, Color frente, Color shadow, int desp)
  9. paintTextEffect(final Graphics2D g2d, final String s, final Color c, final int size, final double tx, final double ty, final boolean isShadow)