I have no experience with any type of flood fill algorithms at all. But I gave it my best shot. At first, I would fill one pixel wherever the user clicked with magenta, and then went through a loop which drew a pixel on top, left, bottom, and right of each magenta pixel. This would take up to 8 seconds to complete the fill with especially large areas. So I added another loop which drew straight magenta-colored lines up, down, left, and right. This halved the time. I had the computer draw more lines... up-up-left, up-left, up-left-left, down-left-left... etc. Now it's down to between 1 & 2 seconds. What else can I do to decrease the amount of time it takes to complete the fill?
for(Point p:shiftDownPoints)
{
//down
for(int y = p.y+1; y<SCREEN_DIM.height; y++)
{
if(capture.getRGB(p.x,y)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x,y,Color.MAGENTA.getRGB());
}
//up
for(int y = p.y-1; y>0; y--)
{
if(capture.getRGB(p.x,y)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x,y,Color.MAGENTA.getRGB());
}
//right
for(int x = p.x+1; x<SCREEN_DIM.width; x++)
{
if(capture.getRGB(x,p.y)==Color.BLACK.getRGB())break;
else capture.setRGB(x,p.y,Color.MAGENTA.getRGB());
}
//left
for(int x = p.x-1; x>0; x--)
{
if(capture.getRGB(x,p.y)==Color.BLACK.getRGB())break;
else capture.setRGB(x,p.y,Color.MAGENTA.getRGB());
}
//down-right
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x+i,p.y+i)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x+i,p.y+i,Color.MAGENTA.getRGB());
}
//down-left
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x-i,p.y+i)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x-i,p.y+i,Color.MAGENTA.getRGB());
}
//up-left
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x-i,p.y-i)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x-i,p.y-i,Color.MAGENTA.getRGB());
}
//up-right
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x+i,p.y-i)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x+i,p.y-i,Color.MAGENTA.getRGB());
}
//up-up-left
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x-i,p.y-i*2)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x-i,p.y-i*2,Color.MAGENTA.getRGB());
}
//up-left-left
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x-i*2,p.y-i)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x-i*2,p.y-i,Color.MAGENTA.getRGB());
}
//down-left-left
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x-i*2,p.y+i)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x-i*2,p.y+i,Color.MAGENTA.getRGB());
}
//down-down-left
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x-i,p.y+i*2)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x-i,p.y+i*2,Color.MAGENTA.getRGB());
}
//down-down-right
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x+i,p.y+i*2)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x+i,p.y+i*2,Color.MAGENTA.getRGB());
}
//down-right-right
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x+i*2,p.y+i)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x+i*2,p.y+i,Color.MAGENTA.getRGB());
}
//up-right-right
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x+i*2,p.y-i)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x+i*2,p.y-i,Color.MAGENTA.getRGB());
}
//up-up-right
for(int i = 1; i<Math.min(SCREEN_DIM.width,SCREEN_DIM.height); i++)
{
if(capture.getRGB(p.x+i,p.y-i*2)==Color.BLACK.getRGB())break;
else capture.setRGB(p.x+i,p.y-i*2,Color.MAGENTA.getRGB());
}
}
while(pixelsDrawn)
{
pixelsDrawn=false;
for(int x = 0; x<SCREEN_DIM.width; x++)
for(int y = 0; y<SCREEN_DIM.height; y++)
{
if(capture.getRGB(x,y)==Color.MAGENTA.getRGB())
{
if(capture.getRGB(x-1,y)!=Color.MAGENTA.getRGB()
&&capture.getRGB(x-1,y)!=Color.BLACK.getRGB())
{
capture.setRGB(x-1,y,Color.MAGENTA.getRGB());
pixelsDrawn = true;
}
if(capture.getRGB(x+1,y)!=Color.MAGENTA.getRGB()
&&capture.getRGB(x+1,y)!=Color.BLACK.getRGB())
{
capture.setRGB(x+1,y,Color.MAGENTA.getRGB());
pixelsDrawn = true;
}
if(capture.getRGB(x,y-1)!=Color.MAGENTA.getRGB()
&&capture.getRGB(x,y-1)!=Color.BLACK.getRGB())
{
capture.setRGB(x,y-1,Color.MAGENTA.getRGB());
pixelsDrawn = true;
}
if(capture.getRGB(x,y+1)!=Color.MAGENTA.getRGB()
&&capture.getRGB(x,y+1)!=Color.BLACK.getRGB())
{
capture.setRGB(x,y+1,Color.MAGENTA.getRGB());
pixelsDrawn = true;
}
}
}
}