Question: I am trying to turn a program I wrote before (Swamp.java) into GUI. My goal is to create a GUI that contains 3 parts: a

I am trying to turn a program I wrote before (Swamp.java) into GUI. My goal is to create a GUI that contains 3 parts: a panel that displays the matrix (swamp), a panel that contains the "solve" button (JButton), and a panel that shows all possible solutions using JTextArea. The following is the Swamp.java I wrote and the swamp input:

import java.io.*;

import java.util.*;

public class Swamp

{

private static int[][] loadSwamp( String infileName, int[] dropInPt ) throws Exception

{

BufferedReader infile = new BufferedReader(new FileReader(infileName));

String[] firstLine = (infile.readLine()).split(" ");

int rows = Integer.parseInt(firstLine[0]);

int [][] swamp = new int[rows][rows];

dropInPt[0] = Integer.parseInt(firstLine[1]);

dropInPt[1] = Integer.parseInt(firstLine[2]);

for(int r = 0; r < swamp.length; r++)

{

String[] nextLine = (infile.readLine()).split(" ");

for(int c = 0; c < swamp[r].length; c++)

{

swamp[r][c] = Integer.parseInt(nextLine[c]);

}

}

infile.close();

return swamp;

}

public static void main(String[] args) throws Exception

{

int[] dropInPt = new int[2];

int[][] swamp = loadSwamp( args[0], dropInPt );

int row=dropInPt[0], col = dropInPt[1];

String path = "";

depthFirstSearch( swamp, row, col, path );

}

static void depthFirstSearch( int[][] swamp, int r, int c, String path )

{

path += "[" + r + "," + c + "]";

if(r == swamp.length - 1 || r == 0 || c == 0 || c == swamp[0].length - 1)

{

System.out.println(path);

return;

}

if(swamp[r - 1][c] == 1)

{

swamp[r][c] = -1;

depthFirstSearch(swamp, r - 1, c, path);

swamp[r][c] = 1;

}

if(swamp[r - 1][c + 1] == 1)

{

swamp[r][c] = -1;

depthFirstSearch(swamp, r - 1, c + 1, path);

swamp[r][c] = 1;

}

if(swamp[r][c + 1] == 1)

{

swamp[r][c] = -1;

depthFirstSearch(swamp, r, c + 1, path);

swamp[r][c] = 1;

}

if(swamp[r + 1][c + 1] == 1)

{

swamp[r][c] = -1;

depthFirstSearch(swamp, r + 1, c + 1, path);

swamp[r][c] = 1;

}

if(swamp[r + 1][c] == 1)

{

swamp[r][c] = -1;

depthFirstSearch(swamp, r + 1, c, path);

swamp[r][c] = 1;

}

if(swamp[r + 1][c - 1] == 1)

{

swamp[r][c] = -1;

depthFirstSearch(swamp, r + 1, c - 1, path);

swamp[r][c] = 1;

}

if(swamp[r][c - 1] == 1)

{

swamp[r][c] = -1;

depthFirstSearch(swamp, r, c - 1, path);

swamp[r][c] = 1;

}

if(swamp[r - 1][c - 1] == 1)

{

swamp[r][c] = -1;

depthFirstSearch(swamp, r - 1, c - 1, path);

swamp[r][c] = 1;

}

else

{

return;

}

}

}

input0.txt 10 2 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0

input1.txt 10 2 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0

input2.txt 9 4 4 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0

input3.txt 8 1 1 0 0 0 0 1 0 0 0 0 1 1 0 1 0 1 0 0 0 0 1 0 0 1 0 0 0 1 0 1 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0

I want to create 3 different parts with the part that display the swamp has the largest area, the part that displays the solution with a smaller area, and the part that has the "solve" button with the smallest area, using GridBagLayout (or better ways to set up the layouts). The part that displays the swamp must be able to load and print out matrices (swamp) with different size. Can someone help me with this problem?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!