1. Member
Join Date
Jan 2011
Location
Beirut, Lebanon
Posts
90
Rep Power
0

## Recursive Sierpinski's Carpet

hello there, I need some help solving this mystery. I was asked to write a recursive method to draw the Sierpinski's Carpet using DrawingPanel. I have done all the work and studied it many times to figure what is wrong. Every thing is working perfectly it is just that i am having a little different drawing.

the picture to the right is what i am supposed to have, and this is my recursive method but i am sure that if any thing is wrong then it is in the logic maybe.
Java Code:
```/**
* Recursive method to draw the sqaures of the carpet
* @param level the level of drawing squares
* @param g the Graphics from the panel
* @param x the x-position of the square
* @param y the y-position of the square
* @param width the width of the square
* @param height the height of the square
*/
public static void drawFigure(int level, Graphics g,
int x, int y, int width, int height){
if (level == 1) {
g.fillRect(x, y, width, height);
} else {
int wid = width / 3;
int heig = height / 3;
int x1 = x  - 2 * wid;
int y1 = y - 2 * heig;
int x2 = x + wid;
int y2 = y + heig;
int x3 = x + 4 * wid;
int y3 = y + 4 * heig;

drawFigure(level-1, g, x1, y1, wid, heig);
drawFigure(level-1, g, x2, y1, wid, heig);
drawFigure(level-1, g, x3, y1, wid, heig);
drawFigure(level-1, g, x1, y2, wid, heig);
drawFigure(level-1, g, x1, y3, wid, heig);
drawFigure(level-1, g, x2, y3, wid, heig);
drawFigure(level-1, g, x3, y3, wid, heig);
drawFigure(level-1, g, x3, y2, wid, heig);
g.fillRect(x, y, width, height);
}
}```
hope anyone can help me find my error, thanks

2. Member
Join Date
Jan 2011
Location
Beirut, Lebanon
Posts
90
Rep Power
0
Never mind i solved my problem :) thanks any ways.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•