views:

65

answers:

3

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);
}
+1  A: 

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);
Erick Robertson
I'm going to give it a shot. I was calling closePath() but found that it just closed the last arc.
abrightwell
This definitely solves the fill problem. It is proving to a bit difficult to get exactly the shape that I want, but that is calculation of the curve problem.
abrightwell
+1  A: 

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

Emil
A: 

Change the winding rule for the GeneralPath

gp.setWindingRule(GeneralPath.WIND_NON_ZERO);
Devon_C_Miller