0

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);			
	}
}
2
Contributors
1
Reply
4
Views
5 Years
Discussion Span
Last Post by JamesCherrill
0

Maybe you can do that then fill the inner triangle with a transparent color (alpha = 0)?
OR
Maybe you can convert that shape into a single line so swing can understand inside/outside properly? - ie path round the outside, path a connecting line to the inner shape, path the inner shape then connect back to the outside shape back along the same line that you connected in with.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.