首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用outline和TextAttribute同时编写文本

如何用outline和TextAttribute同时编写文本
EN

Stack Overflow用户
提问于 2018-04-23 03:15:28
回答 1查看 419关注 0票数 2

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

但如果有轮廓,它不会显示文本属性,当我选中AttributedString#getIterator().getAttributes()时,它看起来是正确的:

代码语言:javascript
复制
{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}

这样就添加了属性。

此外,我还使用

代码语言:javascript
复制
createGlyphVector(<getFontRenderContext>, AttributedString#getIterator)

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

EN

回答 1

Stack Overflow用户

发布于 2021-05-19 21:14:22

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

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49969775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档