siteName > > dynamicprogramming.BoundaryFill
Threads
java ( 81351 ) - java ( 81352 ) stack: com.thealgorithms.dynamicprogramming.BoundaryFill.main(BoundaryFill.java:72)
package com.thealgorithms.dynamicprogramming;

/**
 * Java program for Boundary fill algorithm.
 * @author Akshay Dubey (https://github.com/itsAkshayDubey)
 */
public final class BoundaryFill {
    private BoundaryFill() {
    }

    /**
     * Get the color at the given co-odrinates of a 2D image
     *
     * @param image The image to be filled
     * @param xCoordinate The x co-ordinate of which color is to be obtained
     * @param yCoordinate The y co-ordinate of which color is to be obtained
     */
    public static int getPixel(int[][] image, int xCoordinate, int yCoordinate) {
        return image[xCoordinate][yCoordinate];
    }

    /**
     * Put the color at the given co-odrinates of a 2D image
     *
     * @param image The image to be filed
     * @param xCoordinate The x co-ordinate at which color is to be filled
     * @param yCoordinate The y co-ordinate at which color is to be filled
     */
    public static void putPixel(int[][] image, int xCoordinate, int yCoordinate, int newColor) {
        image[xCoordinate][yCoordinate] = newColor;
    }

    /**
     * Fill the 2D image with new color
     *
     * @param image The image to be filed
     * @param xCoordinate The x co-ordinate at which color is to be filled
     * @param yCoordinate The y co-ordinate at which color is to be filled
     * @param newColor The new color which to be filled in the image
     * @param boundaryColor The old color which is to be replaced in the image
     */
    public static void boundaryFill(int[][] image, int xCoordinate, int yCoordinate, int newColor, int boundaryColor) {
        if (xCoordinate >= 0 && yCoordinate >= 0 && getPixel(image, xCoordinate, yCoordinate) != newColor && getPixel(image, xCoordinate, yCoordinate) != boundaryColor) {
            putPixel(image, xCoordinate, yCoordinate, newColor);
            boundaryFill(image, xCoordinate + 1, yCoordinate, newColor, boundaryColor);
            boundaryFill(image, xCoordinate - 1, yCoordinate, newColor, boundaryColor);
            boundaryFill(image, xCoordinate, yCoordinate + 1, newColor, boundaryColor);
            boundaryFill(image, xCoordinate, yCoordinate - 1, newColor, boundaryColor);
            boundaryFill(image, xCoordinate + 1, yCoordinate - 1, newColor, boundaryColor);
            boundaryFill(image, xCoordinate - 1, yCoordinate + 1, newColor, boundaryColor);
            boundaryFill(image, xCoordinate + 1, yCoordinate + 1, newColor, boundaryColor);
            boundaryFill(image, xCoordinate - 1, yCoordinate - 1, newColor, boundaryColor);
        }
    }

    /**
     * This method will print the 2D image matrix
     *
     * @param image The image to be printed on the console
     */
    public static void printImageArray(int[][] image) {
        for (int i = 0; i < image.length; i++) {
            for (int j = 0; j < image[0].length; j++) {
                System.out.print(image[i][j] + "  ");
            }

            System.out.println();
        }
    }

    // Driver Program
    public static void main(String[] args) {
        // Input 2D image matrix
        int[][] image = {
            {0, 0, 0, 0, 0, 0, 0},
            {0, 3, 3, 3, 3, 0, 0},
            {0, 3, 0, 0, 3, 0, 0},
            {0, 3, 0, 0, 3, 3, 3},
            {0, 3, 3, 3, 0, 0, 3},
            {0, 0, 0, 3, 0, 0, 3},
            {0, 0, 0, 3, 3, 3, 3},
        };

        boundaryFill(image, 2, 2, 5, 3);

        /* Output ==>
                 * 0  0  0  0  0  0  0
                   0  3  3  3  3  0  0
                   0  3  5  5  3  0  0
           0  3  5  5  3  3  3
           0  3  3  3  5  5  3
           0  0  0  3  5  5  3
           0  0  0  3  3  3  3
                 * */

        // print 2D image matrix
        printImageArray(image);
    }
}
Variables All
No.FromNameValue
172args[Ljava.lang.String;@7852e922
END 0 00
Output All Filter Merge
Process FilterThread Filter
81351 java 81352 java
No.PNPIDTIDTNMessage
1java8135181352java0
2java8135181352java0
3java8135181352java0
4java8135181352java0
5java8135181352java0
6java8135181352java0
7java8135181352java0
8java8135181352java0
9java8135181352java3
10java8135181352java3
11java8135181352java3
12java8135181352java3
13java8135181352java0
14java8135181352java0
15java8135181352java0
16java8135181352java3
17java8135181352java5
18java8135181352java5
19java8135181352java3
20java8135181352java0
21java8135181352java0
22java8135181352java0
23java8135181352java3
24java8135181352java5
25java8135181352java5
26java8135181352java3
27java8135181352java3
28java8135181352java3
29java8135181352java0
30java8135181352java3
31java8135181352java3
32java8135181352java3
33java8135181352java5
34java8135181352java5
35java8135181352java3
36java8135181352java0
37java8135181352java0
38java8135181352java0
39java8135181352java3
40java8135181352java5
41java8135181352java5
42java8135181352java3
43java8135181352java0
44java8135181352java0
45java8135181352java0
46java8135181352java3
47java8135181352java3
48java8135181352java3
49java8135181352java3
END 0 0 0 00
Project:Alg-Java
Update:20240824
Commit:a7cd97d7
Source Code:dynamicprogramming.BoundaryFill
BuildTool:Java17
Compiler:Java17
Runtime:Openjdk17
System:MySystemD
Kernel:Linux5.10.211
Cpu:Intel:Corei7-7700K
Machine:AwesomeMachine