Galera, eu implementei aqui o Flood Fill:
public void floodFill(int x, int y, Color target_color, Color replacement_color) {
Stack<Integer[]> q = new Stack<Integer[]>();
if (imageBuffer.getRGB(x, y) == replacement_color.getRGB())
return;
q.push(new Integer[] { x, y });
while (!q.isEmpty()) {
Integer[] n = q.peek();
imageBuffer.setRGB(n[0], n[1], replacement_color.getRGB());
q.pop();
if (imageBuffer.getRGB(n[0] - 1, n[1]) == target_color.getRGB()) {
q.push(new Integer[] { n[0] - 1, n[1] });
}
if (imageBuffer.getRGB(n[0] + 1, n[1]) == target_color.getRGB()) {
q.push(new Integer[] { n[0] + 1, n[1] });
}
if (imageBuffer.getRGB(n[0], n[1] - 1) == target_color.getRGB()) {
q.push(new Integer[] { n[0], n[1] - 1 });
}
if (imageBuffer.getRGB(n[0], n[1] + 1) == target_color.getRGB()) {
q.push(new Integer[] { n[0], n[1] + 1 });
}
}
}
Tem a versão recursiva tbm:
public void floodFill0(int x, int y, Color target_color, Color replacement_color) {
if (imageBuffer.getRGB(x, y) != target_color.getRGB()) {
return;
}
imageBuffer.setRGB(x, y, replacement_color.getRGB());
floodFill0(x - 1, y, target_color, replacement_color);
floodFill0(x + 1, y, target_color, replacement_color);
floodFill0(x, y - 1, target_color, replacement_color);
floodFill0(x, y + 1, target_color, replacement_color);
}
A recursiva n é muito boa, pq quando a tela a ser pintada é muito grande, da Stack Overflow…
O primeiro ali de cima, iterativo funciona perfeitamente só que esta meio custoso, ele demora poco mais de 1 segundo pra pintar uma tela de 750x550…
Seria culpa da JVM do java que faz com que ele fique lerdo, ou tteria alguma implementação menos custosa que voces podem me passar ?