The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.awt.font  [5 examples]

e653. Drawing a Paragraph of Text

In order to change the font of the text, you need to supply an attributed string to the LineBreakMeasurer. See e655 Drawing Text with Mixed Styles for an example.
    public void drawParagraph(Graphics2D g, String paragraph, float width) {
        LineBreakMeasurer linebreaker = new LineBreakMeasurer(
            new AttributedString(paragraph).getIterator(), g.getFontRenderContext());
    
        float y = 0.0f;
        while (linebreaker.getPosition() < paragraph.length()) {
            TextLayout tl = linebreaker.nextLayout(width);
    
            y += tl.getAscent();
            tl.draw(g, 0, y);
            y += tl.getDescent() + tl.getLeading();
        }
    }

 Related Examples
e651. Listing All Available Font Families
e652. Getting the Font Faces for a Font Family
e654. Getting the Shape from the Outline of Text
e655. Drawing Text with Mixed Styles


© 2002 Addison-Wesley.