Example usage for java.awt.font LineBreakMeasurer LineBreakMeasurer

List of usage examples for java.awt.font LineBreakMeasurer LineBreakMeasurer

Introduction

In this page you can find the example usage for java.awt.font LineBreakMeasurer LineBreakMeasurer.

Prototype

public LineBreakMeasurer(AttributedCharacterIterator text, FontRenderContext frc) 

Source Link

Document

Constructs a LineBreakMeasurer for the specified text.

Usage

From source file:Main.java

public static int paintMultilineText(Graphics2D g2d, String text, int textX, int textWidth, int textY,
        int maxTextLineCount) {
    FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, false);
    int fa = g2d.getFontMetrics().getAscent();

    if (text.length() == 0)
        return textY;

    int currOffset = 0;
    AttributedString attributedDescription = new AttributedString(text);
    attributedDescription.addAttribute(TextAttribute.FONT, g2d.getFont());
    LineBreakMeasurer lineBreakMeasurer = new LineBreakMeasurer(attributedDescription.getIterator(), frc);
    int lineCount = 0;
    while (true) {
        TextLayout tl = lineBreakMeasurer.nextLayout(textWidth);
        if (tl == null)
            break;

        int charCount = tl.getCharacterCount();
        String line = text.substring(currOffset, currOffset + charCount);

        g2d.drawString(line, textX, textY);

        textY += fa;/*from   w  ww .j a v a2 s.  c  om*/
        currOffset += charCount;

        lineCount++;
        if ((maxTextLineCount > 0) && (lineCount == maxTextLineCount))
            break;
    }

    // textY += fh;

    return textY;
}

From source file:Main.java

License:asdf

void drawParagraph(Graphics2D g, String paragraph, float width) {
    LineBreakMeasurer linebreaker = new LineBreakMeasurer(new AttributedString(paragraph).getIterator(),
            g.getFontRenderContext());// ww  w  .java 2 s .  c o  m

    int y = 0;
    while (linebreaker.getPosition() < paragraph.length()) {
        TextLayout textLayout = linebreaker.nextLayout(width);

        y += textLayout.getAscent();
        textLayout.draw(g, 0, y);
        y += textLayout.getDescent() + textLayout.getLeading();
    }
}

From source file:BasicShapes.java

License:asdf

void drawParagraph(Graphics2D g, String paragraph, float width) {
    LineBreakMeasurer linebreaker = new LineBreakMeasurer(new AttributedString(paragraph).getIterator(),
            g.getFontRenderContext());//from ww w . j  a  v  a 2 s .  c o  m

    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();
    }
}

From source file:TextLayoutLineBreakerMeasurer.java

public void paint(Graphics g) {
    Graphics2D graphics2D = (Graphics2D) g;
    GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font font = new Font("LucidaSans", Font.PLAIN, 14);
    AttributedString messageAS = new AttributedString(m);
    messageAS.addAttribute(TextAttribute.FONT, font);
    AttributedCharacterIterator messageIterator = messageAS.getIterator();
    FontRenderContext messageFRC = graphics2D.getFontRenderContext();
    LineBreakMeasurer messageLBM = new LineBreakMeasurer(messageIterator, messageFRC);

    Insets insets = getInsets();// w ww . j a  va  2 s  .  com
    float wrappingWidth = getSize().width - insets.left - insets.right;
    float x = insets.left;
    float y = insets.top;

    while (messageLBM.getPosition() < messageIterator.getEndIndex()) {
        TextLayout textLayout = messageLBM.nextLayout(wrappingWidth);
        y += textLayout.getAscent();
        textLayout.draw(graphics2D, x, y);
        y += textLayout.getDescent() + textLayout.getLeading();
        x = insets.left;
    }
}

From source file:Main.java

public void paint(Graphics g) {
    String input = "this is a test.this is a test.this is a test.this is a test.";

    AttributedString attributedString = new AttributedString(input);
    attributedString.addAttribute(TextAttribute.FONT, (Font) UIManager.get("Label.font"));
    Color color = (Color) UIManager.get("Label.foreground");

    attributedString.addAttribute(TextAttribute.FOREGROUND, color);

    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    int width = getSize().width;
    int x = 10;/*ww w .  ja v  a 2s.c  om*/
    int y = 30;

    AttributedCharacterIterator characterIterator = attributedString.getIterator();
    FontRenderContext fontRenderContext = g2d.getFontRenderContext();
    LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator, fontRenderContext);
    while (measurer.getPosition() < characterIterator.getEndIndex()) {
        TextLayout textLayout = measurer.nextLayout(width);
        y += textLayout.getAscent();
        textLayout.draw(g2d, x, y);
        y += textLayout.getDescent() + textLayout.getLeading();
    }
}

From source file:Main.java

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();/*from   w w  w .j  a va  2 s  .c o m*/
        layout.draw(g2, x, y);
        y += layout.getDescent() + layout.getLeading();
    }
}

From source file:TextLayoutDemo.java

public void paint(Graphics g) {
    Graphics2D graphics2D = (Graphics2D) g;
    GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font font = new Font("LucidaSans", Font.PLAIN, 14);
    AttributedString messageAS = new AttributedString(textMessage);
    messageAS.addAttribute(TextAttribute.FONT, font);
    AttributedCharacterIterator messageIterator = messageAS.getIterator();
    FontRenderContext messageFRC = graphics2D.getFontRenderContext();
    LineBreakMeasurer messageLBM = new LineBreakMeasurer(messageIterator, messageFRC);

    Insets insets = getInsets();/*  ww w.  java  2s. co  m*/
    float wrappingWidth = getSize().width - insets.left - insets.right;
    float x = insets.left;
    float y = insets.top;

    while (messageLBM.getPosition() < messageIterator.getEndIndex()) {
        TextLayout textLayout = messageLBM.nextLayout(wrappingWidth);
        y += textLayout.getAscent();
        textLayout.draw(graphics2D, x, y);
        y += textLayout.getDescent() + textLayout.getLeading();
        x = insets.left;
    }
}

From source file:ParagraphLayout.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    String s = "Java components and products Directory for Java components "
            + "and applications.Hundreds of Java components and applications "
            + "are organized by topic. You can find what you need easily. "
            + "You may also compare your product with others. If your component "
            + "is not listed, just send your url to java2s@java2s.com. " + "http://www.java2s.com";
    Font font = new Font("Serif", Font.PLAIN, 24);
    AttributedString as = new AttributedString(s);
    as.addAttribute(TextAttribute.FONT, font);
    AttributedCharacterIterator aci = as.getIterator();

    FontRenderContext frc = g2.getFontRenderContext();
    LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
    Insets insets = getInsets();/*from  w  w  w .  j a  va 2 s  .co  m*/
    float wrappingWidth = getSize().width - insets.left - insets.right;
    float x = insets.left;
    float y = insets.top;

    while (lbm.getPosition() < aci.getEndIndex()) {
        TextLayout textLayout = lbm.nextLayout(wrappingWidth);
        y += textLayout.getAscent();
        textLayout.draw(g2, x, y);
        y += textLayout.getDescent() + textLayout.getLeading();
        x = insets.left;
    }
}

From source file:LineBreakMeasurerDemo.java

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();/*from  w w w . j  a  v  a2 s .c om*/
        layout.draw(g2, x, y);
        y += layout.getDescent() + layout.getLeading();
    }
}

From source file:MainClass.java

public void paint(Graphics g) {
    Dimension size = getSize();/*from  w w w. j  av a 2 s  . c  om*/

    String s = "To java2s.com or not to java2s.com, that is a question";

    Hashtable map = new Hashtable();
    map.put(TextAttribute.SIZE, new Float(32.0f));

    AttributedString as = new AttributedString(s, map);

    map = new Hashtable();
    map.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);

    as.addAttributes(map, 33, 52);

    AttributedCharacterIterator aci = as.getIterator();

    int startIndex = aci.getBeginIndex();
    int endIndex = aci.getEndIndex();

    LineBreakMeasurer measurer;
    measurer = new LineBreakMeasurer(aci, new FontRenderContext(null, false, false));
    measurer.setPosition(startIndex);

    float wrappingWidth = (float) size.width;

    float Y = 0.0f;

    while (measurer.getPosition() < endIndex) {
        TextLayout layout = measurer.nextLayout(wrappingWidth);

        Y += layout.getAscent();

        float X = 0.0f;

        switch (justify) {
        case LEFT:
            if (layout.isLeftToRight())
                X = 0.0f;
            else
                X = wrappingWidth - layout.getAdvance();
            break;

        case RIGHT:
            if (layout.isLeftToRight())
                X = wrappingWidth - layout.getVisibleAdvance();
            else
                X = wrappingWidth;
            break;

        case CENTER:
            if (layout.isLeftToRight())
                X = (wrappingWidth - layout.getVisibleAdvance()) / 2;
            else
                X = (wrappingWidth + layout.getAdvance()) / 2 - layout.getAdvance();
            break;

        case EQUALITY:
            layout = layout.getJustifiedLayout(wrappingWidth);
        }

        layout.draw((Graphics2D) g, X, Y);

        Y += layout.getDescent() + layout.getLeading();
    }
}