Java - 填充自定义形状

发布于 2024-09-11 10:30:54 字数 658 浏览 6 评论 0原文

我创建了一个自定义形状,本质上它是四个 Arc2D 对象的集合。

当绘制这些弧线时,它们形成了可以被认为是四点圆形星形的形状,有点像三叶草。一条弧线结束的地方,另一条弧线开始。它们在中心形成一个正方形。想象一下,取一个正方形并在每条边上画半个圆。

我可以将此形状绘制到 Graphics2D 对象,但是当我填充它时,它只会填充圆弧,而不填充中心正方形。填充这个内部正方形是我的问题。

我已经实现了下面的 getPathIterator() 方法。我也实现了 contains() 方法。但它仍然只能填充弧线。

我尝试添加一个矩形。填充形状时,矩形/正方形将被正确填充,但是它也会绘制矩形,这显然应该是预期的,但绝对不是所需的结果。

那么,有人对如何“填充”这样的形状有什么想法吗?

public PathIterator getPathIterator(AffineTransform at) {
    GeneralPath gp = new GeneralPath

    for (Arc2D arcs : this.arcs) {
        gp.append(arc, false);
    }

    return gp.getPathIterator(at);
}

I have created a custom shape, essentially it is a collection of four Arc2D objects.

When these arcs are drawn they form what could be considered a four point rounded star shape, kind of like a clover. Where one arc ends, the other begins. They form a square in the center. So imagine, taking a square and drawing half circles on every side.

I can draw this shape to a Graphics2D object, but when I fill it, it will only fill the arcs and not the center square. Filling this internal square is my problem.

I have implemented the getPathIterator() method, below. I have implemented the contains() methods as well. But it will still only fill the arcs.

I tried to add a Rectangle. When filling the shape, the rectangle/square would be filled properly, however it draws the rectangle as well, which obviously should be expected, but definitely not the desired result.

So, does anyone have any ideas on how to "fill" such a shape?

public PathIterator getPathIterator(AffineTransform at) {
    GeneralPath gp = new GeneralPath

    for (Arc2D arcs : this.arcs) {
        gp.append(arc, false);
    }

    return gp.getPathIterator(at);
}

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

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

发布评论

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

评论(4

无敌元气妹 2024-09-18 10:30:54

每当我创建一个形状时,我总是创建一个 Path2D.Double 对象。然后,我使用 moveTo 到达起始位置,并结合使用 lineTo() 和 curveTo() 来沿着路径移动。然后,当我完成后,我调用 closePath()。它始终填写正确。

我没有任何实现 getPathIterator 的经验,但我确实注意到您没有执行 closePath() 。我不知道这是否是必需的,但我确实觉得采取我的方法会起作用。

下面是填充圆角矩形的示例:

double width = 300;
double height = 400;
Path2D.Double path = new Path2D.Double();
path.moveTo(0.0, 8.0);
path.curveTo(0.0, 0.0, 8.0, 0.0, 8.0, 0.0);
path.lineTo(width - 8.0, 0.0);
path.curveTo(width, 0.0, width, 8.0, width, 8.0);
path.lineTo(width, height - 8.0);
path.curveTo(width, height, width - 8.0, height, width - 8.0, height);
path.lineTo(8.0, height);
path.curveTo(0.0, .height, 0.0, height - 8.0, 0, height - 8.0);
path.closePath();
g2.fill(path);

Whenever I create a shape, I always create a Path2D.Double object. Then, I use moveTo to get to the starting location, and a combination of lineTo() and curveTo() to move around the path. Then, when I'm done, I call closePath(). It has always filled correctly.

I don't have any experience with implementing getPathIterator, but I do notice that you don't do a closePath(). I don't know if that's required or not, but I do feel like taking my approach will work.

Here is an example that fills a rounded rectangle:

double width = 300;
double height = 400;
Path2D.Double path = new Path2D.Double();
path.moveTo(0.0, 8.0);
path.curveTo(0.0, 0.0, 8.0, 0.0, 8.0, 0.0);
path.lineTo(width - 8.0, 0.0);
path.curveTo(width, 0.0, width, 8.0, width, 8.0);
path.lineTo(width, height - 8.0);
path.curveTo(width, height, width - 8.0, height, width - 8.0, height);
path.lineTo(8.0, height);
path.curveTo(0.0, .height, 0.0, height - 8.0, 0, height - 8.0);
path.closePath();
g2.fill(path);
差↓一点笑了 2024-09-18 10:30:54

我对图形不太了解。但是我在sun的网站上看到了这些示例。我只是发布链接以防您需要。
http://java.sun.com/products/ java-media/2D/samples/suite/index.html

I don't know much about graphics.But I have seen these examples in sun's site.I'm just posting the link in case you need it.
http://java.sun.com/products/java-media/2D/samples/suite/index.html

娇妻 2024-09-18 10:30:54

另外,尝试使用 appendtrue 作为第二个参数,它将连接这些点。

Also, try using append with true as the second argument, it will connect the points.

樱&纷飞 2024-09-18 10:30:54

更改 GeneralPath 的缠绕规则

gp.setWindingRule(GeneralPath.WIND_NON_ZERO);

Change the winding rule for the GeneralPath

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