Java:填充形状的线条

发布于 2024-10-29 03:17:16 字数 445 浏览 1 评论 0原文

这并不是很重要,但它困扰了我一段时间。

问题描述:
给定:一条线(Line2D)
想要:将线绘制为楔形(填充通用路径)

当然,这可以通过将楔形创建为通用路径然后用图形填充它来完成(我的解决方案)。

我的第一种方法是楔形描边线,因为出于某种原因我不想更改线对象,而且我只是想绘制线对象而不再考虑它。 创建楔形笔划没有问题(进行一些计算,然后创建通用路径) - 但我无法(轻松)填充它

显然,Graphics2D 的填充似乎只填充了它得到的形状- 并且不处理笔画的填充(如果仔细想想,这种行为是有道理的)。

问题:有什么方法可以填充 Stroke 的形状(填充形状 - 更具体地说:GeneralPath - 在绘制之前以某种方式填充)?

This is not really important, but it was bothering me for a little while.

Problem description:
Given: a line (Line2D)
Wanted: drawing the line as a wedge (Filled GeneralPath)

Surely this can be done by creating the wedge as General Path and then filling it by the Graphics (my solution).

My first approach would have been a wedge-stroked line, because I didn't want to change the line object for some reason, also I just wanted to draw the line object and not think about it any more.
Creating the wedge-stroke was no problem (Some calculations and then creating the General Path) - but I was not able to fill it (easily)

Apparently it seems the fill of Graphics2D only fills the shape it gets - and does not handle the filling of the stroke (that behavior makes sense if one thinks about it).

Question: Is there any way to fill a shape of a Stroke (filling a shape - more specifically: a GeneralPath - somehow before drawing it)?

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

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

发布评论

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

评论(3

顾挽 2024-11-05 03:17:16

如果您将 Line2D 传递到那里,BasicStroke.public Shape createStrokedShape(Shape s) 可能会有所帮助吗?

May be BasicStroke.public Shape createStrokedShape(Shape s) can help if you pass the Line2D there?

暮凉 2024-11-05 03:17:16

使用createStrokedShape()后,请注意draw() “描画 Shape 的轮廓”,而 fill()“填充Shape的内部。”

Once you use createStrokedShape(), note that draw() "strokes the outline of a Shape," while fill() "fills the interior of a Shape."

故事↓在人 2024-11-05 03:17:16
import java.awt.*;

public static Shape strokeToShape(Shape path, float strokeWidth)
    return new BasicStroke(strokeWidth).createStrokedShape(path);
}

您还可以指定 BasicStrokecapjoin 参数

import java.awt.*;

public static Shape strokeToShape(Shape path, float strokeWidth)
    return new BasicStroke(strokeWidth).createStrokedShape(path);
}

You may also specify cap and join parameters of BasicStroke

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