Java tutorial
import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.font.FontRenderContext; import java.awt.font.LineBreakMeasurer; import java.awt.font.TextAttribute; import java.awt.font.TextLayout; import java.text.AttributedCharacterIterator; import java.text.AttributedString; import javax.swing.JFrame; import javax.swing.JPanel; public class LineBreakMeasurerDemo { public static void main(String arg[]) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add("Center", new DisplayPanel()); frame.pack(); frame.setSize(new Dimension(350, 400)); frame.setVisible(true); } } class DisplayPanel extends JPanel { String text = "This is a test. This is a test. This is a test."; AttributedString attribString; AttributedCharacterIterator attribCharIterator; public DisplayPanel() { setBackground(Color.white); setSize(350, 400); attribString = new AttributedString(text); attribString.addAttribute(TextAttribute.FOREGROUND, Color.blue, 0, text.length()); Font font = new Font("sanserif", Font.ITALIC, 20); attribString.addAttribute(TextAttribute.FONT, font, 0, text.length()); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; attribCharIterator = attribString.getIterator(); FontRenderContext frc = new FontRenderContext(null, false, false); LineBreakMeasurer lbm = new LineBreakMeasurer(attribCharIterator, frc); int x = 10, y = 20; int w = getWidth(); float wrappingWidth = w - 15; while (lbm.getPosition() < text.length()) { TextLayout layout = lbm.nextLayout(wrappingWidth); y += layout.getAscent(); layout.draw(g2, x, y); y += layout.getDescent() + layout.getLeading(); } } }