I am writing an application that implements Flood Fill 4 algorithm. It works perfectly fine as long as the borders are thick. The algorithm fills in a certain color within the boarder. I tried to make the boarder thinner, but in that case the pixels were able to went out the boarder, and the program crashed.
Flood fill algorithm works EXCELLENT within the "thick boarder" area, which is the right triangle. However, the algorithm does not work within the other four areas because the boarders are thin, i.e. leakage takes place. Except for making the other boarders thick, are there any methods I can use?
Here is the complete code, it is just one class:
import java.awt.Color;
import java.awt.Container;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyPolygon extends JFrame {
private JLabel my;
private BufferedImage buffered;
public MyPolygon() throws InterruptedException {
createMy();
}
private void createMy() throws InterruptedException {
Container contentPane = getContentPane();
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(null);
contentPane.setSize(1200, 900);
my = new JLabel();
my.setIcon(new ImageIcon("myImage.png"));
my.setBounds(10,200, 1000, 800);
contentPane.add(my);
setSize(1200, 900);
setVisible(true);
setLocationRelativeTo(null);
Image img = ((ImageIcon) my.getIcon()).getImage();
buffered = new BufferedImage(img.getWidth(null),
img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
buffered.getGraphics().drawImage(img, 0, 0, null);
int fill = 100;
boundaryFill4(200, 215, fill, 50);
my.setIcon(new ImageIcon(buffered));
}
// Flood Fill method
public void boundaryFill4(int x, int y, int fill, int boundary) {
Color c = new Color(buffered.getRGB(x, y));
int current = c.getRed();
System.out.println(x + " " + y + " | " + current);
if ((current > boundary) && (current != fill)) {
int red = fill;
int green = fill;
int blue = fill;
c = new Color(red, green, blue);
buffered.setRGB(x, y, c.getRGB());
boundaryFill4(x + 1, y, fill, boundary);
boundaryFill4(x - 1, y, fill, boundary);
boundaryFill4(x, y + 1, fill, boundary);
boundaryFill4(x, y - 1, fill, boundary);
}
}
// Main method
public static void main(String args[]) throws InterruptedException {
MyPolygon my = new MyPolygon();
my.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}