Java examples for 2D Graphics:Text
Draws text in red with appropriate word wrap.
//package com.java2s; import java.awt.Color; import java.awt.Graphics2D; import java.awt.font.LineBreakMeasurer; import java.awt.font.TextLayout; import java.text.AttributedString; public class Main { /**/*from w w w . j av a 2 s . c om*/ * Draws text in red with appropriate word wrap. * * @param g * the Graphics on which to draw * @param message * the error message * @param widthOfDrawingSpace * the width (in pixels) of the "paragraph" */ public static void drawErrorMessage(Graphics2D g, String message, int widthOfDrawingSpace) { LineBreakMeasurer linebreaker = new LineBreakMeasurer( new AttributedString(message).getIterator(), g.getFontRenderContext()); g.setColor(Color.red); float y = 0.0f; while (linebreaker.getPosition() < message.length()) { TextLayout tl = linebreaker.nextLayout(widthOfDrawingSpace); y += tl.getAscent(); tl.draw(g, 0, y); y += tl.getDescent() + tl.getLeading(); } } }