當前位置: 首頁>>代碼示例>>Java>>正文


Java AttributedString.addAttribute方法代碼示例

本文整理匯總了Java中java.text.AttributedString.addAttribute方法的典型用法代碼示例。如果您正苦於以下問題:Java AttributedString.addAttribute方法的具體用法?Java AttributedString.addAttribute怎麽用?Java AttributedString.addAttribute使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.text.AttributedString的用法示例。


在下文中一共展示了AttributedString.addAttribute方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testAttributedStringSerialization3

import java.text.AttributedString; //導入方法依賴的package包/類
/**
 * Tests the serialization of an {@link AttributedString}.
 */
public void testAttributedStringSerialization3() {
    AttributedString s1 = new AttributedString("ABC");
    s1.addAttribute(TextAttribute.LANGUAGE, "English");
    AttributedString s2 = null;
    try {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(buffer);
        SerialUtilities.writeAttributedString(s1, out);
        out.close();

        ByteArrayInputStream bais = new ByteArrayInputStream(
                buffer.toByteArray());
        ObjectInputStream in = new ObjectInputStream(bais);
        s2 = SerialUtilities.readAttributedString(in);
        in.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    assertTrue(AttributedStringUtilities.equal(s1, s2));
}
 
開發者ID:mdzio,項目名稱:ccu-historian,代碼行數:25,代碼來源:SerialUtilitiesTest.java

示例2: formatToCharacterIterator

import java.text.AttributedString; //導入方法依賴的package包/類
/**
 * {@inheritDoc}
 *
 * @stable ICU 49
 */
@Override
public AttributedCharacterIterator formatToCharacterIterator(Object obj) {
    StringBuffer toAppendTo = new StringBuffer();
    FieldPosition pos = new FieldPosition(0);
    toAppendTo = format(obj, toAppendTo, pos);

    // supporting only DateFormat.Field.TIME_ZONE
    AttributedString as = new AttributedString(toAppendTo.toString());
    as.addAttribute(DateFormat.Field.TIME_ZONE, DateFormat.Field.TIME_ZONE);

    return as.getIterator();
}
 
開發者ID:abhijitvalluri,項目名稱:fitnotifications,代碼行數:18,代碼來源:TimeZoneFormat.java

示例3: render

import java.text.AttributedString; //導入方法依賴的package包/類
@Override
public void render(final String word, BufferedImage image) {
    Graphics2D g = image.createGraphics();

    RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
    g.setRenderingHints(hints);

    AttributedString as = new AttributedString(word);
    as.addAttribute(TextAttribute.FONT, getRandomFont());

    FontRenderContext frc = g.getFontRenderContext();
    AttributedCharacterIterator aci = as.getIterator();

    TextLayout tl = new TextLayout(aci, frc);
    int xBaseline = (int) Math.round(image.getWidth() * XOFFSET);
    int yBaseline = image.getHeight() - (int) Math.round(image.getHeight() * YOFFSET);
    Shape shape = tl.getOutline(AffineTransform.getTranslateInstance(xBaseline, yBaseline));

    g.setColor(getRandomColor());
    g.setStroke(new BasicStroke(_strokeWidth));

    g.draw(shape);
}
 
開發者ID:akuma,項目名稱:meazza,代碼行數:25,代碼來源:ColoredEdgesWordRenderer.java

示例4: pasteText

import java.text.AttributedString; //導入方法依賴的package包/類
/**
 * @see com.octo.captcha.component.image.textpaster.AbstractTextPaster#pasteText(java.awt.image.BufferedImage, java.text.AttributedString)
 */
public BufferedImage pasteText(final BufferedImage background, final AttributedString attributedWord) throws CaptchaException {
    BufferedImage out = copyBackground(background);
    Graphics2D g2d = pasteBackgroundAndSetTextColor(out, background);
    g2d.setRenderingHints(renderingHints);
    g2d.translate(10, background.getHeight() / 2);

    AttributedCharacterIterator iterator = attributedWord.getIterator();
    while (iterator.getIndex() != iterator.getEndIndex()) {
        AttributedString character = new AttributedString(String.valueOf(iterator.current()));
        character.addAttribute(TextAttribute.FONT, iterator.getAttribute(TextAttribute.FONT));
        pasteCharacter(g2d, character);
        iterator.next();
    }

    g2d.dispose();
    return out;
}
 
開發者ID:pengqiuyuan,項目名稱:g2,代碼行數:21,代碼來源:NonLinearRandomAngleTextPaster.java

示例5: createTickLabel

import java.text.AttributedString; //導入方法依賴的package包/類
/**
 * Creates a tick label for the specified value based on the current
 * tick unit (used for formatting the exponent).
 *
 * @param value  the value.
 *
 * @return The label.
 *
 * @since 1.0.18
 */
protected AttributedString createTickLabel(double value) {
    if (this.numberFormatOverride != null) {
        return new AttributedString(
                this.numberFormatOverride.format(value));
    } else {
        String baseStr = this.baseSymbol;
        if (baseStr == null) {
            baseStr = this.baseFormatter.format(this.base);
        }
        double logy = calculateLog(value);
        String exponentStr = getTickUnit().valueToString(logy);
        AttributedString as = new AttributedString(baseStr + exponentStr);
        as.addAttributes(getTickLabelFont().getAttributes(), 0, (baseStr 
                + exponentStr).length());
        as.addAttribute(TextAttribute.SUPERSCRIPT, 
                TextAttribute.SUPERSCRIPT_SUPER, baseStr.length(), 
                baseStr.length() + exponentStr.length());
        return as;
    }
}
 
開發者ID:jfree,項目名稱:jfreechart,代碼行數:31,代碼來源:LogAxis.java

示例6: paintComponent

import java.text.AttributedString; //導入方法依賴的package包/類
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    //Loops over all messages and draws them
    ArrayList<String> copyOfMessages = new ArrayList<>(messages);
    for (String message : copyOfMessages) {
        //Draws the black shadow
        AttributedString shadowString = new AttributedString(message);
        shadowString.addAttribute(TextAttribute.FOREGROUND, Color.BLACK);
        shadowString.addAttribute(TextAttribute.FONT, new Font("Arial", Font.PLAIN, 12));
        g.drawString(shadowString.getIterator(), 20 + 1, (messages.indexOf(message) + 1) * 20 + 1);
        //Draws the white text
        AttributedString frontString = new AttributedString(message);
        frontString.addAttribute(TextAttribute.FOREGROUND, Color.WHITE);
        frontString.addAttribute(TextAttribute.FONT, new Font("Arial", Font.PLAIN, 12));
        g.drawString(frontString.getIterator(), 20, (messages.indexOf(message) + 1) * 20);
    }
}
 
開發者ID:Jelmerro,項目名稱:UniCam,代碼行數:19,代碼來源:ViewPanel.java

示例7: render

import java.text.AttributedString; //導入方法依賴的package包/類
@Override
public void render(final Graphics2D g) {
  if (this.displayedText == null || this.displayedText.isEmpty() || !Game.getRenderEngine().canRender(this.entity)) {
    return;
  }

  final Point2D location = Game.getCamera().getViewPortLocation(this.entity);
  RenderEngine.renderImage(g, this.bubble, new Point2D.Double(location.getX() + this.entity.getWidth() / 2.0 - this.textBoxWidth / 2.0 - PADDING, location.getY() - this.height - PADDING));

  g.setColor(SPEAK_FONT_COLOR);
  final FontRenderContext frc = g.getFontRenderContext();

  final String text = this.displayedText;
  final AttributedString styledText = new AttributedString(text);
  styledText.addAttribute(TextAttribute.FONT, this.font);
  final AttributedCharacterIterator iterator = styledText.getIterator();
  final LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, frc);
  measurer.setPosition(0);
  final float x = (float) Game.getCamera().getViewPortLocation(this.entity).getX() + this.entity.getWidth() / 2.0f - this.textBoxWidth / 2.0f;
  float y = (float) Game.getCamera().getViewPortLocation(this.entity).getY() - this.height;
  while (measurer.getPosition() < text.length()) {
    final TextLayout layout = measurer.nextLayout(this.textBoxWidth);

    y += layout.getAscent();
    final float dx = layout.isLeftToRight() ? 0 : this.textBoxWidth - layout.getAdvance();
    layout.draw(g, x + dx, y);
    y += layout.getDescent() + layout.getLeading();
  }
}
 
開發者ID:gurkenlabs,項目名稱:litiengine,代碼行數:30,代碼來源:SpeechBubble.java

示例8: drawStringToGraphics

import java.text.AttributedString; //導入方法依賴的package包/類
protected void drawStringToGraphics(Graphics g, String s, Font font, boolean strike) {
    if (g != null) {
        if (!strike){
            g.drawString(s, drawX, drawY);
        }else{
            Graphics2D g2 = ((Graphics2D)g);
            AttributedString strikeText = new AttributedString(s);
            strikeText.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
            strikeText.addAttribute(TextAttribute.FONT, g.getFont());
            g2.drawString(strikeText.getIterator(), drawX, drawY);
        }
    }
    drawX += getWidth(s, font);
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:15,代碼來源:CCPaintComponent.java

示例9: formatToCharacterIterator

import java.text.AttributedString; //導入方法依賴的package包/類
AttributedCharacterIterator formatToCharacterIterator(Object obj, Unit unit) {
    if (!(obj instanceof Number))
        throw new IllegalArgumentException();
    Number number = (Number) obj;
    StringBuffer text = new StringBuffer();
    unit.writePrefix(text);
    attributes.clear();
    if (obj instanceof BigInteger) {
        format((BigInteger) number, text, new FieldPosition(0), true);
    } else if (obj instanceof java.math.BigDecimal) {
        format((java.math.BigDecimal) number, text, new FieldPosition(0)
                      , true);
    } else if (obj instanceof Double) {
        format(number.doubleValue(), text, new FieldPosition(0), true);
    } else if (obj instanceof Integer || obj instanceof Long) {
        format(number.longValue(), text, new FieldPosition(0), true);
    } else {
        throw new IllegalArgumentException();
    }
    unit.writeSuffix(text);
    AttributedString as = new AttributedString(text.toString());

    // add NumberFormat field attributes to the AttributedString
    for (int i = 0; i < attributes.size(); i++) {
        FieldPosition pos = attributes.get(i);
        Format.Field attribute = pos.getFieldAttribute();
        as.addAttribute(attribute, attribute, pos.getBeginIndex(), pos.getEndIndex());
    }

    // return the CharacterIterator from AttributedString
    return as.getIterator();
}
 
開發者ID:abhijitvalluri,項目名稱:fitnotifications,代碼行數:33,代碼來源:DecimalFormat.java

示例10: sendComposedText

import java.text.AttributedString; //導入方法依賴的package包/類
/**
 * Send the composed text to the client.
 */
private void sendComposedText() {
    AttributedString as = new AttributedString(buffer.toString());
    as.addAttribute(TextAttribute.INPUT_METHOD_HIGHLIGHT,
            InputMethodHighlight.SELECTED_RAW_TEXT_HIGHLIGHT);
    context.dispatchInputMethodEvent(
            InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
            as.getIterator(), 0,
            TextHitInfo.leading(insertionPoint), null);
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:13,代碼來源:CodePointInputMethod.java

示例11: TextLayout

import java.text.AttributedString; //導入方法依賴的package包/類
/**
 * Constructs a <code>TextLayout</code> from a <code>String</code>
 * and a {@link Font}.  All the text is styled using the specified
 * <code>Font</code>.
 * <p>
 * The <code>String</code> must specify a single paragraph of text,
 * because an entire paragraph is required for the bidirectional
 * algorithm.
 * @param string the text to display
 * @param font a <code>Font</code> used to style the text
 * @param frc contains information about a graphics device which is needed
 *       to measure the text correctly.
 *       Text measurements can vary slightly depending on the
 *       device resolution, and attributes such as antialiasing.  This
 *       parameter does not specify a translation between the
 *       <code>TextLayout</code> and user space.
 */
public TextLayout(String string, Font font, FontRenderContext frc) {

    if (font == null) {
        throw new IllegalArgumentException("Null font passed to TextLayout constructor.");
    }

    if (string == null) {
        throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
    }

    if (string.length() == 0) {
        throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
    }

    Map<? extends Attribute, ?> attributes = null;
    if (font.hasLayoutAttributes()) {
        attributes = font.getAttributes();
    }

    char[] text = string.toCharArray();
    if (sameBaselineUpTo(font, text, 0, text.length) == text.length) {
        fastInit(text, font, attributes, frc);
    } else {
        AttributedString as = attributes == null
            ? new AttributedString(string)
            : new AttributedString(string, attributes);
        as.addAttribute(TextAttribute.FONT, font);
        standardInit(as.getIterator(), text, frc);
    }
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:48,代碼來源:TextLayout.java

示例12: test2

import java.text.AttributedString; //導入方法依賴的package包/類
static void test2() {
    String target = "BACK WARDS";
    String str = "If this text is >" + target + "< the test passed.";
    int length = str.length();
    int start = str.indexOf(target);
    int limit = start + target.length();

    System.out.println("start: " + start + " limit: " + limit);

    AttributedString astr = new AttributedString(str);
    astr.addAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL);

    astr.addAttribute(TextAttribute.BIDI_EMBEDDING,
                     new Integer(-3),
                     start,
                     limit);

    Bidi bidi = new Bidi(astr.getIterator());

    for (int i = 0; i < bidi.getRunCount(); ++i) {
        System.out.println("run " + i +
                           " from " + bidi.getRunStart(i) +
                           " to " + bidi.getRunLimit(i) +
                           " at level " + bidi.getRunLevel(i));
    }

    System.out.println(bidi + "\n");

    if (bidi.getRunCount() != 6) { // runs of spaces and angles at embedding bound,s and final period, each get level 1
        throw new Error("Bidi embedding processing failed");
    } else {
        System.out.println("test2() passed.\n");
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:35,代碼來源:BidiEmbeddingTest.java

示例13: TextLayout

import java.text.AttributedString; //導入方法依賴的package包/類
/**
 * Constructs a {@code TextLayout} from a {@code String}
 * and a {@link Font}.  All the text is styled using the specified
 * {@code Font}.
 * <p>
 * The {@code String} must specify a single paragraph of text,
 * because an entire paragraph is required for the bidirectional
 * algorithm.
 * @param string the text to display
 * @param font a {@code Font} used to style the text
 * @param frc contains information about a graphics device which is needed
 *       to measure the text correctly.
 *       Text measurements can vary slightly depending on the
 *       device resolution, and attributes such as antialiasing.  This
 *       parameter does not specify a translation between the
 *       {@code TextLayout} and user space.
 */
public TextLayout(String string, Font font, FontRenderContext frc) {

    if (font == null) {
        throw new IllegalArgumentException("Null font passed to TextLayout constructor.");
    }

    if (string == null) {
        throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
    }

    if (string.length() == 0) {
        throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
    }

    Map<? extends Attribute, ?> attributes = null;
    if (font.hasLayoutAttributes()) {
        attributes = font.getAttributes();
    }

    char[] text = string.toCharArray();
    if (sameBaselineUpTo(font, text, 0, text.length) == text.length) {
        fastInit(text, font, attributes, frc);
    } else {
        AttributedString as = attributes == null
            ? new AttributedString(string)
            : new AttributedString(string, attributes);
        as.addAttribute(TextAttribute.FONT, font);
        standardInit(as.getIterator(), text, frc);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:48,代碼來源:TextLayout.java

示例14: test

import java.text.AttributedString; //導入方法依賴的package包/類
public void test() {

        // construct a paragraph as follows: MIXED + [SPACING + WORD] + ...
        StringBuffer text = new StringBuffer(MIXED);
        for (int i=0; i < NUM_WORDS; i++) {
            text.append(SPACING);
            text.append(WORD);
        }

        AttributedString attrString = new AttributedString(text.toString());
        attrString.addAttribute(TextAttribute.SIZE, new Float(24.0));

        LineBreakMeasurer measurer = new LineBreakMeasurer(attrString.getIterator(),
                                                           DEFAULT_FRC);

        // get width of a space-word sequence, in context
        int sequenceLength = WORD.length()+SPACING.length();
        measurer.setPosition(text.length() - sequenceLength);

        TextLayout layout = measurer.nextLayout(10000.0f);

        if (layout.getCharacterCount() != sequenceLength) {
            throw new Error("layout length is incorrect!");
        }

        final float sequenceAdvance = layout.getVisibleAdvance();

        float wrappingWidth = sequenceAdvance * 2;

        // now run test with a variety of widths
        while (wrappingWidth < (sequenceAdvance*NUM_WORDS)) {
            measurer.setPosition(0);
            checkMeasurer(measurer,
                          wrappingWidth,
                          sequenceAdvance,
                          text.length());
            wrappingWidth += sequenceAdvance / 5;
        }
    }
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:40,代碼來源:TestLineBreakWithFontSub.java

示例15: drawString

import java.text.AttributedString; //導入方法依賴的package包/類
private static void drawString(Graphics2D g, Font font, String value, float x, float y) {
    AttributedString str = new AttributedString(value);
    str.addAttribute(TextAttribute.FOREGROUND, Color.BLACK);
    str.addAttribute(TextAttribute.FONT, font);
    FontRenderContext frc = new FontRenderContext(null, true, true);
    TextLayout layout = new LineBreakMeasurer(str.getIterator(), frc).nextLayout(Integer.MAX_VALUE);
    layout.draw(g, x, y);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:9,代碼來源:GlyphVectorOutline.java


注:本文中的java.text.AttributedString.addAttribute方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。