import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
public class floodfill extends JApplet {
public void init()
{
}
public void paint(Graphics g)
{
BufferedImage i = null;
try {
i = ImageIO.read(new File("image1.jpg"));
Color c = new Color(i.getRGB(35, 35));
flood(i, g, 35, 35, c, Color.red);
g.drawImage(i, 100, 100, this);
}
catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
g.drawImage(i, 100, 100, this);
}
public void flood(BufferedImage i,
Graphics g,
int x,
int y,
Color c,
Color c1)
{
if (x >= 1 && y >= 1
&& x < i.getWidth()
&& y < i.getHeight()) {
Color c2 = new Color(i.getRGB(x, y));
if (Math.abs(c2.getGreen() - c.getGreen()) < 30
&& Math.abs(c2.getRed() - c.getRed()) < 30
&& Math.abs(c2.getBlue() - c.getBlue()) < 30) {
i.setRGB(x, y, c1.getRGB());
g.drawImage(i, 100, 100, this);
flood(i, g, x, y + 1, c, c1);
flood(i, g, x + 1, y, c, c1);
flood(i, g, x - 1, y, c, c1);
flood(i, g, x, y - 1, c, c1);
}
}
}
}