我目前正在使用这样的方式: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)正确写入带有轮廓的文本,但不显示属性。
发布于 2021-05-19 21:14:22
使用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);https://stackoverflow.com/questions/49969775
复制相似问题