Java Applet | Implementing Flood Fill algorithm
Flood Fill Algorithm is to replace a certain closed or a similarly coloured field with a specified color. The use of the FloodFill algorithm can be seen in paints and other games such as minesweeper.
In this article, FloodFill is used for a connected area by a specified colour, in Java Applet by using the FllodFill algorithm.
There are two approaches that can be used:
- Recursive approach (limited usage as it crashes for a larger area)
- Using queue (more reliable)
Examples:
- For image 1:
- Output (floodfilled at position 35, 35):
- Output (floodfilled at position 1, 1):

- Output (floodfilled at position 35, 35):
- For image 2:
- Output(floodfilled at position 35, 35) :

-
Output (floodfilled at position 1, 1):

- Output(floodfilled at position 35, 35) :
- For image 3:
- Output(floodfilled at position 35, 35) :

- Output (floodfilled at position 1, 1):

- Output(floodfilled at position 35, 35) :
Program 1: To implement floodfill algorithm in Java Applet using recursion:
Note: To run the program, use an offline IDE such as Netbeans, Eclipse, etc. Please download the input images and put them along with the class file. Otherwise, the program might yield an “Can’t read the input file” error.
// Java Program to implement floodfill algorithm // in Java Applet(using recursion) 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() { } // paint function public void paint(Graphics g) { BufferedImage i = null; try { // Input the image to be used for FloodFill // The output is shown for 3 images // image1, image2 and image2 i = ImageIO.read(new File("image1.jpg")); // floodfill with color red at point 35, 35 // get color of image at 35, 35 Color c = new Color(i.getRGB(35, 35)); flood(i, g, 35, 35, c, Color.red); // draw the image after floodfill g.drawImage(i, 100, 100, this); } catch (Exception e) { JOptionPane.showMessageDialog(this, e.getMessage()); } // draw the image after floodfill g.drawImage(i, 100, 100, this); } // function to floodfill the image 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()) { // find the color at point x, y Color c2 = new Color(i.getRGB(x, y)); // if there is no boundary (the color is almost // same as the color of the point where // floodfill is to be applied if (Math.abs(c2.getGreen() - c.getGreen()) < 30 && Math.abs(c2.getRed() - c.getRed()) < 30 && Math.abs(c2.getBlue() - c.getBlue()) < 30) { // change the color of the pixel of image i.setRGB(x, y, c1.getRGB()); g.drawImage(i, 100, 100, this); // floodfill in all possible directions 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); } } } } |
Output:
- for image 1:
Input:

Output :
- for image 2:
Input:

Output :
-
for image 3:
Input:

Output :
Note: If a larger area is flood filled (at coordinate 1, 1) using the recursive approach, then recursive algorithm might get crashed.
Example:
floodfill the larger side of image Input:
Output:
Explanation:
Since the area to be covered is very large,
therefore only some part is covered by the algorithm,
and after that the program gets crashed.
Program 2: To implement floodfill algorithm in Java Applet using queue:
Note: To run the program, use an offline IDE such as Netbeans, Eclipse, etc. Please download the input images and put them along with the class file. Otherwise, the program might yield an “Can’t read the input file” error.
// Java Program to implement floodfill algorithm// in Java Applet(using queue)importjava.awt.*;importjavax.swing.*;importjava.awt.image.*;importjava.io.*;importjavax.imageio.ImageIO;publicclassfloodfillextendsJApplet {publicvoidinit(){}// paint functionpublicvoidpaint(Graphics g){BufferedImage i =null;try{// Input the image to be used for FloodFill// The output is shown for 3 images// image1, image2 and image2i = ImageIO.read(newFile("image1.jpg"));// floodfill with color red at point 1, 1// get color of image at 1, 1// if 35, 35 point is floodfilled it will floodfill// the smaller areaColor c =newColor(i.getRGB(1,1));flood(i, g,1,1, c, Color.red);// draw the image after floodfillg.drawImage(i,100,100,this);}catch(Exception e) {JOptionPane.showMessageDialog(this, e.getMessage());}// draw the image after floodfillg.drawImage(i,100,100,this);}// function to floodfill the image using queuepublicvoidflood(BufferedImage i,Graphics g,intx1,inty1,Color c,Color c1){// create a stack using arrayintstx[] =newint[100000];intsty[] =newint[100000], f, r, x, y;// create a front and rearf = r =0;// initilize themstx[0] = x1;sty[0] = y1;// while front is greater than rearwhile(f >= r) {// pop element outx = stx[r];y = sty[r++];if(x >=1&& y >=1&& x < i.getWidth()&& y < i.getHeight()) {// find the color at point x, yColor c2 =newColor(i.getRGB(x, y));// if there is no boundary (the color is almost// same as the color of the point where// floodfill is to be appliedif(Math.abs(c2.getGreen() - c.getGreen()) <30&& Math.abs(c2.getRed() - c.getRed()) <30&& Math.abs(c2.getBlue() - c.getBlue()) <30) {// change the color of the pixel of imagei.setRGB(x, y, c1.getRGB());g.drawImage(i,100,100,this);// floodfill in all possible directions// store them in queuestx[f] = x;sty[f++] = y +1;stx[f] = x;sty[f++] = y -1;stx[f] = x +1;sty[f++] = y;stx[f] = x -1;sty[f++] = y;}}}}}chevron_rightfilter_noneOutput:
- For image 1:
Input:
Output (floodfilled at position 35, 35):

Output (floodfilled at position 1, 1):
- For image 2:
Input:
Output(floodfilled at position 35, 35) :

Output (floodfilled at position 1, 1):
- For image 3:
Input:
Output(floodfilled at position 35, 35) :

Output (floodfilled at position 1, 1):

Recommended Posts:
- Flood fill Algorithm - how to implement fill() in paint?
- Java Applet Basics
- Draw a Smiley in Java Applet
- Java Applet | Digital Stopwatch
- Draw a Chessboard in Java Applet
- Draw a Polygon in Java Applet
- Java Applet | How to display a Digital Clock
- Draw an Olympic Symbol in Java Applet
- Java Applet | How to display an Analog Clock
- Draw a ellipse and a rectangle in Java Applet
- Java Applet | Draw a line using drawLine() method
- Java Code for Moving Text | Applet | Thread
- Implementing Checksum Using Java
- Arrays.fill() in Java with Examples
- Implementing next_permutation() in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
- For image 1:



