I'm using Swing's GeneralPath to create complex shapes and fill them. Usually I do this, and it creates a nice hexagon for me:
path.moveTo(100, 100);
path.lineTo(150, 100);
path.lineTo(180, 150);
path.lineTo(150, 200);
path.lineTo(100, 200);
path.lineTo(70, 150);
path.lineTo(100, 100);
g2.draw(path);
Suppose I wanted to create a donut-shaped GeneralPath, and do this:
path.moveTo(100, 100);
path.lineTo(150, 100);
path.lineTo(180, 150);
path.lineTo(150, 200);
path.lineTo(100, 200);
path.lineTo(70, 150);
path.lineTo(100, 100);
path.moveTo(110, 110);
path.lineTo(125, 120);
path.lineTo(110, 120);
path.lineTo(110, 110);
path.closePath();
Draw this and you'll get the same hexagon with a closed triangle in the middle (a pretty bad donut, yeah).
That's all fine, but when I try to fill it using g.fill(), it fills the middle triangle portion too - so instead of the middle of the donut being white, it's filled in and I see just a hexagon.
Here's the full code so you can try it yourself:
public class GeneralPath1 extends Applet
{
private GeneralPath path;
public GeneralPath1()
{
path = new GeneralPath();
path.moveTo(100, 100);
path.lineTo(150, 100);
path.lineTo(180, 150);
path.lineTo(150, 200);
path.lineTo(100, 200);
path.lineTo(70, 150);
path.lineTo(100, 100);
path.moveTo(110, 110);
path.lineTo(125, 120);
path.lineTo(110, 120);
path.lineTo(110, 110);
path.closePath();
}
public void paint(Graphics gr)
{
setSize(600, 600);
Graphics2D g2 = (Graphics2D) gr;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
path.closePath();
//g2.draw(path);
g2.fill(path);
g2.setPaint(Color.black);
}
}