1. Member
Join Date
Sep 2008
Posts
58
Rep Power
0

Graphics2D: stack overflow error

Hey,
I have a program that has to draw a polygon, then apply a recursive function that fill the polygon with a color. Unfortunately, I cannot use the fillpolygon() function, but have to make my own function. I have developed a code for any small polygons. But if I apply my code to large images, I get buffer overflow. This is a recursive function code:

-16776961 = blue
-16711936 = green
-1 = white

Java Code:
```  public void fill4(int xpoint, int ypoint )
{
int pxl=0;
pxl = bufImag.getRGB(xpoint,ypoint);

if(pxl != -16776961 && pxl != -16711936 && pxl==-1   )
{
bufImag.setRGB(xpoint,ypoint,-16711936);

fill4(xpoint-1,ypoint);
fill4(xpoint,ypoint-1);
fill4(xpoint,ypoint+1);
fill4(xpoint+1,ypoint);
}

}```
Any piece of advise how to fill each point in the diagram, so that the result is the whole polygon is filled

2. If you do this
Java Code:
```fill4(xpoint-1,ypoint);
fill4(xpoint,ypoint-1);
fill4(xpoint,ypoint+1);
fill4(xpoint+1,ypoint);```
then each of these four method calls will send this x,y point (in the current trip through the method) through again -> overflow: you're going over the same points again and again.
Think of the image as a rectangle and try traversing it as a typewriter writes on a page. Do one point, increment the x and pass the x,y along to the next iteration. When x reaches the value of the rectangle width reset it to zero and increment the row/y. When x == width-1 and y == height-1 you are done.

3. A comment on the coding style. Don't use obscure literals in the code.

Define the colors as finals and use the variable in your code:
final int blue = 0Xff0000ff;
final int green = 0Xff00ff00;
final int white = 0XFFFFFFFF;
...
if(px1 != green && px1 != blue && px1 != white)

4. Member
Join Date
Sep 2008
Posts
58
Rep Power
0
Thanks, thats a good thought. And that thought would come to anyone's mind. Sorry, that I didn't mention the challenge earlier; but my origin (x,y) is off-center of the image, and I have to use a recursive loop.

5. Can you write a contains() method that can be called by fill4?

How do you know that the 4 sets of points you recursively call fill4 with are in the polygon?

6. Member
Join Date
Sep 2008
Posts
58
Rep Power
0
in my if loop, i have given 3 color conditions: border of the polygon, color of the fill (to prevent filling over what was already filled)and color of the background respectively

What will the contain method have?
Java Code:
```  public void fill4(int xpoint, int ypoint )
{
int pxl=0;
pxl = bufImag.getRGB(xpoint,ypoint);

if(pxl != -16776961 && pxl != -16711936 && pxl==-1   )
{
bufImag.setRGB(xpoint,ypoint,-16711936);

fill4(xpoint-1,ypoint);
fill4(xpoint,ypoint-1);
fill4(xpoint,ypoint+1);
fill4(xpoint+1,ypoint);
}

}```
Last edited by rosh72851; 10-15-2008 at 04:20 AM.

7. Senior Member
Join Date
Sep 2008
Posts
564
Rep Power
9
Isn't it redundant to have != checks when you have a == check for the same variable? Anyways...

A flood fill algorithm is bound to overflow if you are trying to fill an image that is too large. If there are n pixels to be filled, you make n comparisons and 4n recursive calls.

If you check the pixels prior to making a recursive call, then you will make 4n comparisons and n recursive calls instead. Not sure how much that will impact your buffer overflow, but it may be worth checking out.

You can also try to set a maximum recursion value and find a way to restart the algorithm such that everything gets filled up. This would be pretty difficult, though.

8. Member
Join Date
Sep 2008
Posts
58
Rep Power
0
What you are saying is very difficult. I mean there are 18 sides to this polygon. So I have to make 18 comparisons for 18X 4 recursions. Its difficult. Im sure I will still get a overflow. I have a question.

Is there a way that I can divide the diagram into 4, paint each part and the final result is a full painted polygon. Ive tried but was not successful in displaying all 4 filled simultaneously. Any suggestions?

9. Senior Member
Join Date
Sep 2008
Posts
564
Rep Power
9
The number of sides isn't a concern after having drawn the polygon, as a fill algorithm doesn't pay attention to the shape, hence it being recursive.

I can't think of a simple way to try what you suggested. If you divide the image into sections, you would have to confirm and locate a point inside the polygon in all partitions.

If you really want to go to the length of confirming if a point is in the polygon, you would probably be doing this in the same manner as the flood fill algorithm.

My only other suggestions is to scrap the recursion aspect, though I don't know if you want to (or are allowed to) do so, and read up on scan conversion. It basically runs through each pixel and decides on what to fill based on polygon edges and vertices (similar to what hardwired suggested, but applicable to polygons).

10. i have given 3 color conditions: border of the polygon, color of the fill (to prevent filling over what was already filled)and color of the background respectively
Why don't you put that info in your program as comments to help others to understand the logic? The if test with the obscure numbers is very poorly documented. NO WAY TO KNOW WHY IT IS THERE.

11. Member
Join Date
Sep 2008
Posts
58
Rep Power
0
About the colors, I have made the changes in my original program.

THis question is for my graphics assignment. I have to apply the scan conversion in my next assignment. For this question in this assignment, I have to apply recursion. So the method I have come up for this question, is to check the neighbors for each pixel if the colors are present.

12. Member
Join Date
Sep 2008
Posts
58
Rep Power
0
Ive got it. I just reduced the original size of the image by 8 times. fill the polygon, then magnify it back 8 times. Let me try it out.
Last edited by rosh72851; 10-15-2008 at 09:47 PM.

Posting Permissions

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