Java examples for 2D Graphics:Text
draw Wrapped String
//package com.java2s; import java.awt.FontMetrics; import java.awt.Graphics; public class Main { public static void drawWrappedString(Graphics g, String s, int x, int y, int width) { FontMetrics fm = g.getFontMetrics(); int lineHeight = fm.getHeight(); int curX = x; int curY = y; for (String word : s.split(" ")) { int wordWidth = fm.stringWidth(word + " "); if (curX + wordWidth >= x + width) { curY += lineHeight;// w w w. jav a 2 s . c om curX = x; } g.drawString(word, curX, curY); curX += wordWidth; } } }