Java如何同时使用轮廓和TextAttribute编写文本



我当前正在使用这种方式:在Java中使用轮廓绘制文本要编写带有轮廓的文本,然后创建下划线,背景颜色等:http://www.java2s.com/code/java/2d-graphics-gui/textattattributeunderunderlineandtrikethrough.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)

正确地写出轮廓,但不显示属性。

而不是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);

最新更新