I'm developing an image editor like Paint
, and I can not do the paint bucket method, the idea is like this: (example)
I have an 8x8 array and the possible values in this example are "-", "X" or "O"
0 1 2 3 4 5 6 7
0 [-][-][-][-][-][-][-][x]
1 [-][-][-][-][-][-][x][-]
2 [-][-][-][-][-][x][-][-]
3 [-][-][-][-][x][-][-][-]
4 [-][-][-][-][x][-][-][-]
5 [-][-][x][x][-][-][-][-]
6 [-][x][-][-][-][-][-][-]
7 [x][-][-][-][-][-][-][-]
In my method I choose a field that has a "-", then it and all the fields on the sides or above or below that are "-", become "O" and this is a recursive method, example : pint (5,6);
0 1 2 3 4 5 6 7
0 [-][-][-][-][-][-][-][x]
1 [-][-][-][-][-][-][x][o]
2 [-][-][-][-][-][x][o][o]
3 [-][-][-][-][x][o][o][o]
4 [-][-][-][-][x][o][o][o]
5 [-][-][x][x][o][o][o][o]
6 [-][x][o][o][o][o][o][o]
7 [x][o][o][o][o][o][o][o]
My code implementing in a bufferedImage
instead of an array:
public static void pinta(BufferedImage buff,int x,int y,int color)
{
int filtro=buff.getRGB(x,y);
buff.setRGB(x,y,color);
if(filtro!=color){
if(buff.getRGB(x+1, y)==filtro){
pinta(buff,x+1,y,color);
}
if(buff.getRGB(x-1, y)==filtro){
pinta(buff,x-1,y,color);
}
if(buff.getRGB(x, y+1)==filtro){
pinta(buff,x,y+1,color);
}
if(buff.getRGB(x, y-1)==filtro){
pinta(buff,x,y-1,color);
}
}
}
This code works for the example of the array because it is too small, but when I try on a large image, greater than 200px
and 200px
more or less, it StackOverflowError
, does not have to be with the fact I have not put a condition to avoid CordenatesOffBoundException
, considering that this will not happen in my example to facilitate.
So how do I do this method without using resource? can someone give me an example code?