Java - 填充自定义形状
我创建了一个自定义形状,本质上它是四个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
每当我创建一个形状时,我总是创建一个 Path2D.Double 对象。然后,我使用 moveTo 到达起始位置,并结合使用 lineTo() 和 curveTo() 来沿着路径移动。然后,当我完成后,我调用 closePath()。它始终填写正确。
我没有任何实现 getPathIterator 的经验,但我确实注意到您没有执行 closePath() 。我不知道这是否是必需的,但我确实觉得采取我的方法会起作用。
下面是填充圆角矩形的示例:
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:
我对图形不太了解。但是我在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
另外,尝试使用
append
和true
作为第二个参数,它将连接这些点。Also, try using
append
withtrue
as the second argument, it will connect the points.更改
GeneralPath
的缠绕规则Change the winding rule for the
GeneralPath