The Java code implements a simple graphical application that visually represents and counts blobs within a grid by using recursion. A blob is a group of adjacent filled squares in the grid.

The application uses Java Swing for creating the graphical user interface (GUI) components. It includes buttons for generating a new set of blobs, counting the number of blobs, and a grid where users can click to get information about the blob size at that specific position. This Java code uses recursion in the getBlobSize method to calculate the size of a blob.

The squares are arranged in a grid, and each position in the grid can be either empty or filled. A blob is defined to be a filled square and any square that can be reached from that square by moving horizontally or vertically to other filled squares. The application fills the grid randomly. If the user clicks on a filled square, all the squares in the blob that contains that square are colored red, and the number of squares in the blob is reported. The applet can also count and report the number of blobs. When the user clicks a “New Blobs” button, the grid is randomly re-filled.

Recursion is a natural fit for problems involving exploration of connected components, such as counting the size of blobs in this case. It simplifies the code and leverages the call stack to keep track of the positions to explore.

How this Code Works?

Class Structure:

  • The main class is named BlobsApp, which extends JFrame and implements ActionListener and MouseListener interfaces.
  • The application is structured with an outer JFrame which represents the main window and an inner JPanel named Content serving as the content pane.

Initialization:

  • The init method is called during the initialization of the applet. It sets up the GUI components, initializes the grid, and creates buttons for generating new blobs and counting them.

Grid and Blob Generation:

  • The grid is represented by a 2D array of boolean values (filled). Each cell in the grid represents a square, and the boolean value indicates whether the square is filled.
  • The fillGrid method generates a new grid of blobs based on a user-specified probability.

Blob Counting:

  • The countBlobs method counts the number of blobs in the grid using a recursive algorithm (getBlobSize). The algorithm marks visited squares to prevent counting the same blob multiple times.
  • The result is displayed in a message label on the GUI.

Blob Size Calculation:

  • The getBlobSize method recursively calculates the size of a blob starting from a given position in the grid.

Mouse Interaction:

  • Users can interact with the grid by clicking on squares. The mousePressed method retrieves blob information for the clicked position and displays it in the message label.

GUI Presentation:

  • The GUI is presented with buttons for user actions, a grid of squares representing the blobs, and a message label for displaying information.
  • The Content class extends JPanel and is responsible for painting the grid based on the state of the blobs and visited squares.

Java Blob Source Code

When you run the program you will see a similar window as showing below:

Java Code to Demonstrate Recursion