Problem with Shapes / GeneralPath;

Hi,

I am drawing some shapes usind GeneralPath; these shapes are in a separate class. Due to a misunderstanding I had some problems with that, however itchyscratchy helped me with that in this thread (see reply #12): http://forum.java.sun.com/thread.jspa?threadID=5151744 (thanks again!)

I now have the following code (following the sample posted by itchyscratchy)

publicclass MyShapes

{

privatefinal Shape bird = createBird();

privatefinal Shape fish = createFish();

publicfinal Shape getBird()

{

return this.bird;

}

publicfinal Shape getFish()

{

return this.fish;

}

privatefinal Shape createBird()

{

GeneralPath p =new GeneralPath();

// lineTo() etc

return p;

}

// Similar code for the fish, etc

privatefinal Shape createFish()

{

GeneralPath p =new GeneralPath();

// lineTo() etc

return p;

}

}

I declared various shapes. Whenever I draw a shape in one of the other classes,all shapes are being drawn. There must be a bug somewhere, unfortunately I can't find it.

publicclass PMgmtextends JComponent{

privatestaticfinallong serialVersionUID = 1;

private MyShapes ms =new MyShapes();

(...)

protectedvoid paintComponent(Graphics g){

super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;

g2.setColor(Color.BLACK);

g2.draw(ms.getBird());// all shapes are being drawn, not only Bird :(

(...)

}

}

Thanks for your help!

[3115 byte] By [SFLa] at [2007-11-27 2:23:15]
# 1
debug your program using a good IDE like eclipse to see what's happening when you call g2.draw(ms.getBird());The problem is it's difficult to identify the problem.just a guess, try a repaint just after adding a Shape.
java_2006a at 2007-7-12 2:28:28 > top of Java-index,Desktop,Core GUI APIs...
# 2
I played with the Debug mode in Eclipse and noticed, that after private MyShapes ms = new MyShapes(); Java goes to MyShapes and just draws every shape. I don't know why because I am using getter/setter...?
SFLa at 2007-7-12 2:28:28 > top of Java-index,Desktop,Core GUI APIs...
# 3

I don't know what's the problem exactly.

Just a guess, I think because the final variables (not sure)

Try this instead and give it a try :

public class MyShapes

{

private Shape bird;

private Shape fish;

public Shape getBird()

{

return (this.bird==null)?createBird():this.bird;

}

public Shape getFish()

{

return (this.fish==null)?createFish():this.fish;

}

private Shape createBird()

{

GeneralPath p = new GeneralPath();

// lineTo() etc

return p;

}

// Similar code for the fish, etc

private Shape createFish()

{

GeneralPath p = new GeneralPath();

// lineTo() etc

return p;

}

}

Hope That Helps

java_2006a at 2007-7-12 2:28:28 > top of Java-index,Desktop,Core GUI APIs...
# 4
I found the error: when I rewrote my code I removed GeneralPath p = new GeneralPath(); from my methods and declared it just once after my class declaration... that's caused the painting of all shapes.
SFLa at 2007-7-12 2:28:28 > top of Java-index,Desktop,Core GUI APIs...