绘制的字符串的粗边框

发布于 2024-12-08 19:34:20 字数 220 浏览 0 评论 0原文

现在我觉得我一直在互联网上寻找如何在文本上添加边框,所以我决定在这里问,因为你们总是知道答案。

那么,在 java 中,如何在 Graphics2D 元素上绘制的字符串中的每个字母周围绘制大约 2 个像素的边框?

像这样:
带有 2px 边框的文本


提前致谢。

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:
Text with 2px border

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身软味 2024-12-15 19:34:20

我在 Javaworld 中找到了一个简单的解决方案,用于在 Java 中绘制文本轮廓:

g.setColor(Color.red);
g.drawString("轮廓", ShiftWest(x, 1), ShiftNorth(y, 1));
g.drawString("轮廓", ShiftWest(x, 1), ShiftSouth(y, 1));
g.drawString("轮廓", ShiftEast(x, 1), ShiftNorth(y, 1));
g.drawString("轮廓", ShiftEast(x, 1), ShiftSouth(y, 1));
g.setColor(Color.yellow);
g.drawString("轮廓", x, y);

本质上,您首先绘制在每个方向上移动的相同字符串,然后再以所需的颜色绘制字符串。这对于单像素轮廓效果很好,但不能很好地缩放到粗轮廓,因为如果多次重复移动,角落可能会出现间隙。

另一种解决方案是使用转换和 getOutline()(TextLayout 类的方法)。可以在此处找到绘制轮廓的示例。

I found one simple solution in Javaworld for drawing an outline on text in Java:

g.setColor(Color.red);
g.drawString("Outline", ShiftWest(x, 1), ShiftNorth(y, 1));
g.drawString("Outline", ShiftWest(x, 1), ShiftSouth(y, 1));
g.drawString("Outline", ShiftEast(x, 1), ShiftNorth(y, 1));
g.drawString("Outline", ShiftEast(x, 1), ShiftSouth(y, 1));
g.setColor(Color.yellow);
g.drawString("Outline", x, y);

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.

野心澎湃 2024-12-15 19:34:20

请参阅:转换形状、文本和图像。在转换示例中,将“基元”设置为“文本”,将“渲染”设置为“描边和填充”。

See: Transforming Shapes, Text, and Images. Set the "primitive" to "text" and the "rendering" to "Stroke and Fill" in the transform example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文