绘制的字符串的粗边框
现在我觉得我一直在互联网上寻找如何在文本上添加边框,所以我决定在这里问,因为你们总是知道答案。
那么,在 java 中,如何在 Graphics2D 元素上绘制的字符串中的每个字母周围绘制大约 2 个像素的边框?
像这样:
提前致谢。
Now I feel like I've been looking all over the internet to find out how to add a border on a text, so I decided to ask here, since you guys always knows the answer.
So, how do I, in java, draw a border of approx 2 pixels around every letter in a string drawn on a Graphics2D element ?
Like this:
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 Javaworld 中找到了一个简单的解决方案,用于在 Java 中绘制文本轮廓:
本质上,您首先绘制在每个方向上移动的相同字符串,然后再以所需的颜色绘制字符串。这对于单像素轮廓效果很好,但不能很好地缩放到粗轮廓,因为如果多次重复移动,角落可能会出现间隙。
另一种解决方案是使用转换和 getOutline()(TextLayout 类的方法)。可以在此处找到绘制轮廓的示例。
I found one simple solution in Javaworld for drawing an outline on text in Java:
Essentially, you draw the same string shifted in each direction first before you draw the string in the desired color. This works well for a one pixel outline, but does not scale well to thick outlines as there may be gaps in the corners if you repeat the shifting multiple times.
Another solution would be to use a transformation and getOutline() which is a method of the TextLayout class. An example for doing outline can be found here.
请参阅:转换形状、文本和图像。在转换示例中,将“基元”设置为“文本”,将“渲染”设置为“描边和填充”。
See: Transforming Shapes, Text, and Images. Set the "primitive" to "text" and the "rendering" to "Stroke and Fill" in the transform example.