我目前正在使用这种方式: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)
正确地写入带有轮廓的文本,但不显示属性。
答案 0 :(得分:1)
使用 font.createGlyphVector
代替 java.awt.font.TextLayout
:
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);