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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top