在java中绘制带有轮廓的文本
我正在 Java 中使用 graphcis2d,目前正在使用它将文本绘制到 bufferedImage 中,
Font font1 = new Font("Arial", Font.PLAIN, 120);
g2d.setFont(font1);
FontMetrics fm1 = g2d.getFontMetrics(font1);
g2d.drawString(s[1], width/2-fm1.stringWidth(s[1])/2, height/4-70);
我想用不同的颜色轮廓绘制该文本。
GlyphVector gv = font1.createGlyphVector(g2d.getFontRenderContext(), s[1]);
Shape shape = gv.getOutline();
g2d.setStroke(new BasicStroke(4.0f));
g2d.translate(width/2-fm1.stringWidth(s[1])/2, height/4-70);
g2d.draw(shape);
使用这种有效方法的问题是,我正在处理阿拉伯字符,而使用 GlyphVector 会颠倒顺序,并且不会使字母彼此流动。
有人可以向我解释如何用一种颜色渲染阿拉伯文本并用另一种颜色绘制轮廓吗?
这是我将使用的文本示例: 拉瑞姆
I'm working with graphcis2d in Java and am currently using this to draw text into a bufferedImage
Font font1 = new Font("Arial", Font.PLAIN, 120);
g2d.setFont(font1);
FontMetrics fm1 = g2d.getFontMetrics(font1);
g2d.drawString(s[1], width/2-fm1.stringWidth(s[1])/2, height/4-70);
I want to draw this text with an different color outline.
GlyphVector gv = font1.createGlyphVector(g2d.getFontRenderContext(), s[1]);
Shape shape = gv.getOutline();
g2d.setStroke(new BasicStroke(4.0f));
g2d.translate(width/2-fm1.stringWidth(s[1])/2, height/4-70);
g2d.draw(shape);
The problem with using this method, which works, is that I am working with arabic characters and using GlyphVector reverses the order and doesn't make the letters flow with one another.
Can someone please explain to me how to render arabic text in one color and have an outline with another?
Heres a sample of the text I would be using:
الرحمن
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种技巧是用轮廓颜色多次绘制文本,在 +/- x 和 +/- y 方向上通过轮廓宽度改变位置,然后在标称位置处绘制前景色。它并不完美,但只要轮廓相对于字母的笔划宽度不是太粗,它看起来就会相当不错。
One trick is to draw the text several times in the outline color, varying the position by the outline width in +/- x and +/- y directions, then draw in the foreground color at the nominal position. It isn't perfect, but it tends to look pretty good provided the outline isn't too thick with respect to the stroke width of the letters.
您可以使用方法
createStrokedShape ()
位于getOutline()
返回的字形Shape
上。另请参阅CompositeStroke
,演示了此处。You may be able to use the method
createStrokedShape()
on the glyph'sShape
returned bygetOutline()
. See alsoCompositeStroke
, demonstrated here.尝试使用
而不是
createGlyphVector
Try to use
instead of the
createGlyphVector