Example usage for java.text AttributedString addAttribute

List of usage examples for java.text AttributedString addAttribute

Introduction

In this page you can find the example usage for java.text AttributedString addAttribute.

Prototype

public void addAttribute(Attribute attribute, Object value, int beginIndex, int endIndex) 

Source Link

Document

Adds an attribute to a subrange of the string.

Usage

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON, 2, 8);
    g2d.drawString(as1.getIterator(), 15, 60);
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, 3, 4);
    g2d.drawString(as1.getIterator(), 15, 60);
}

From source file:TextAttributesSuperscript.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 5, 7);
    g2d.drawString(as1.getIterator(), 15, 60);
}

From source file:TextAttributesBackground.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.BACKGROUND, Color.LIGHT_GRAY, 2, 9);
    g2d.drawString(as1.getIterator(), 15, 60);
}

From source file:TextAttributesColor.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.FOREGROUND, Color.red, 0, 2);
    g2d.drawString(as1.getIterator(), 15, 60);
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    AttributedString astr = new AttributedString("aString");
    astr.addAttribute(TextAttribute.FONT, new Font("", 1, 30), 1, 2);
    astr.addAttribute(TextAttribute.BACKGROUND, Color.red, 2, 3);

    TextLayout tl = new TextLayout(astr.getIterator(), g2d.getFontRenderContext());
    tl.draw(g2d, 10, 20);/*from w  w  w . j  a  v a 2  s .co m*/
}

From source file:BasicShapes.java

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

    int x = 10, y = 10, start = 2, end = 4;

    AttributedString astr = new AttributedString("aString");
    astr.addAttribute(TextAttribute.FONT, new Font("", 1, 1), start, end);
    astr.addAttribute(TextAttribute.BACKGROUND, Color.red, start, end);

    // Draw mixed-style text
    TextLayout tl = new TextLayout(astr.getIterator(), g2d.getFontRenderContext());
    tl.draw(g2d, x, y);//from   w  ww .  ja v  a2  s  .c  om

}

From source file:MainClass.java

private void getLayouts(Graphics g) {
    layouts = new ArrayList();

    Graphics2D g2d = (Graphics2D) g;
    FontRenderContext frc = g2d.getFontRenderContext();

    AttributedString attrStr = new AttributedString(text);
    attrStr.addAttribute(TextAttribute.FONT, font, 0, text.length());
    LineBreakMeasurer measurer = new LineBreakMeasurer(attrStr.getIterator(), frc);
    float wrappingWidth;

    wrappingWidth = getSize().width - 15;

    while (measurer.getPosition() < text.length()) {
        TextLayout layout = measurer.nextLayout(wrappingWidth);
        layouts.add(layout);/*from ww w .  java2 s  . c om*/
    }
}

From source file:TextFormat.java

/**
 * Lazy evaluation of the List of TextLayout objects corresponding to this
 * MText. Some things are approximations!
 *///from   ww w .ja  v a 2s .  c  o  m
private void getLayouts(Graphics g) {
    layouts = new ArrayList();

    Point pen = new Point(10, 20);
    Graphics2D g2d = (Graphics2D) g;
    FontRenderContext frc = g2d.getFontRenderContext();

    AttributedString attrStr = new AttributedString(text);
    attrStr.addAttribute(TextAttribute.FONT, font, 0, text.length());
    LineBreakMeasurer measurer = new LineBreakMeasurer(attrStr.getIterator(), frc);
    float wrappingWidth;

    wrappingWidth = getSize().width - 15;

    while (measurer.getPosition() < text.length()) {
        TextLayout layout = measurer.nextLayout(wrappingWidth);
        layouts.add(layout);
    }
}

From source file:com.qumasoft.guitools.compare.ContentRow.java

@Override
public void paint(Graphics g) {
    if ((getRowType() == ROWTYPE_REPLACE) && rowHadAnnotations) {
        Graphics2D g2 = (Graphics2D) g;

        String s = getText();// ww w  . j a  va  2  s  .  co  m

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

        if (s.length() > 0) {
            int index = 0;
            AttributedString attributedString = new AttributedString(s, getFont().getAttributes());
            try {
                for (byte rType : fileACharacterTypeArray) {
                    switch (rType) {
                    case ContentRow.ROWTYPE_DELETE:
                        attributedString.addAttribute(TextAttribute.STRIKETHROUGH, null, index, index + 1);
                        break;
                    case ContentRow.ROWTYPE_REPLACE:
                        attributedString.addAttribute(TextAttribute.BACKGROUND,
                                ColorManager.getReplaceCompareHiliteBackgroundColor(), index, index + 1);
                        break;
                    default:
                        break;
                    }
                    index++;
                }
                g2.drawString(attributedString.getIterator(), 0, getFont().getSize());
            } catch (java.lang.IllegalArgumentException e) {
                LOGGER.log(Level.WARNING, "bad replace indexes. begin index: [" + index + "] end index: ["
                        + index + "]. String length: [" + s.length() + "]");
            }
        } else {
            super.paint(g);
        }
    } else {
        super.paint(g);
    }
}