【问题标题】:Java how to write a text with outline and TextAttribute in the same timeJava如何同时编写带有大纲和TextAttribute的文本
【发布时间】:2021-05-19 21:14:22
【问题描述】:

我目前正在使用这种方式:Drawing text with outline in java 编写带有轮廓的文本,并创建下划线、背景颜色等:http://www.java2s.com/Code/Java/2D-Graphics-GUI/TextAttributeUnderlineandstrikethrough.htm

但如果有大纲,它不会显示文本属性,当我检查AttributedString#getIterator().getAttributes() 时,它看起来正确:

{java.awt.font.TextAttribute(strikethrough)=true, java.awt.font.TextAttribute(foreground)=java.awt.Color[r=255,g=255,b=255], java.awt.font.TextAttribute(font)=java.awt.Font[family=Impact,name=Impact,style=bolditalic,size=80], java.awt.font.TextAttribute(underline)=0}

所以属性被添加了。

我也在用

createGlyphVector(<getFontRenderContext>, AttributedString#getIterator)

正确书写带有轮廓的文本,但不显示属性。

【问题讨论】:

    标签: java graphics


    【解决方案1】:

    java.awt.font.TextLayout代替font.createGlyphVector

    TextLayout textlayout = new TextLayout(attributes.getIterator(), graphics.getFontRenderContext());
    // the position of the shape at the graphic
    Shape shape = textlayout.getOutline(AffineTransform.getTranslateInstance(xOffset, yOffset));
    graphics.setColor(outlineColor);
    // Choose another width / strength of the stroke on demand
    graphics.setStroke(new BasicStroke(font.getSize2D() / 10.0f));
    // draw the outline / stroke
    graphics.draw(shape);
    // draw / fill the letters / text
    graphics.setColor(textColor);
    graphics.fill(shape);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      • 2015-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-23
      • 2021-08-30
      相关资源
      最近更新 更多