help w fill-algo; throws StackOverFlowError

P

pantalaimon

I'm writing a paint program in java and for the moment I am having
trouble with my filling-algorithm. It works fine for smaller areas but
whenever I try to fill my whole screen with it, I get this error. I
can't think of any other way to do this really so please help me!
thanks in advance

public void fillArea(int x,int y)
{
//reveice rgb of wich is to be replaced
int rgb = image.getRGB(x, y);
//fill start pixel
image.setRGB(x, y, currentColor);
//check surroundings
fillSurroundings(x,y,rgb);
}


void fillSurroundings(int x, int y, int replaceColor)
{
try
{
if(image.getRGB(x, y + 1) == replaceColor)
{
image.setRGB(x, y + 1, currentColor);
fillSurroundings(x, y + 1, replaceColor);
}
}
catch(ArrayIndexOutOfBoundsException e)
{
//
}
try
{
if(image.getRGB(x, y - 1) == replaceColor)
{
image.setRGB(x, y - 1, currentColor);
fillSurroundings(x, y - 1, replaceColor);
}
}
catch(ArrayIndexOutOfBoundsException e)
{
//
}
try
{
if(image.getRGB(x + 1, y) == replaceColor)
{
image.setRGB(x + 1, y, currentColor);
fillSurroundings(x + 1, y, replaceColor);
}
}
catch(ArrayIndexOutOfBoundsException e)
{
//
}
try
{
if(image.getRGB(x - 1, y) == replaceColor)
{
image.setRGB(x, y - 1, currentColor);
fillSurroundings(x - 1, y, replaceColor);
}
}
catch(ArrayIndexOutOfBoundsException e)
{
//
}

}
 
S

Stephan Dahl

pantalaimon said:
I'm writing a paint program in java and for the moment I am having
trouble with my filling-algorithm. It works fine for smaller areas but
whenever I try to fill my whole screen with it, I get this error. I
can't think of any other way to do this really so please help me!
thanks in advance
...

You could use loops to process the pixels instead of recursion.

Best regards, Stephan
 
K

Kevin McMurtrie

I'm writing a paint program in java and for the moment I am having
trouble with my filling-algorithm. It works fine for smaller areas but
whenever I try to fill my whole screen with it, I get this error. I
can't think of any other way to do this really so please help me!
thanks in advance

Make your own stack with an ArrayList.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top