Java - filling out a custom form

I created a custom form, essentially a set of four Arc2D objects.

When these arcs are drawn, they form what can be considered a four-point rounded star, like clover. When one arc ends, another begins. They form a square in the center. So, imagine taking a square and drawing half circles on all sides.

I can draw this shape for a Graphics2D object, but when I fill it, it will fill only the arcs, not the central square. Filling this inner square is my problem.

I have implemented the method getPathIterator()below. I also implemented methods contains(). But it will still fill the arcs.

I tried to add Rectangle. When filling out the form, the rectangle / square is filled in properly, however, it also draws a rectangle, which, obviously, should be expected, but certainly not the desired result.

So, does anyone have any ideas on how to “fill out” such a form?

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

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

    return gp.getPathIterator(at);
}
+6
source share
4 answers

Whenever I create a shape, I always create a Path2D.Double object. Then I use moveTo to go to the original location, as well as a combination of lineTo () and curveTo () to move along the path. Then, when I'm done, I call closePath (). It always filled correctly.

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);
+4

Change the winding rule to GeneralPath

gp.setWindingRule(GeneralPath.WIND_NON_ZERO);
0
source

Also try using appendwith trueas the second argument, it will connect the dots.

0
source

All Articles