I am really bad with gui's. But here is my take on it:
Code
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.*;
public class JMaze extends JFrame
implements WindowListener
{
private JMazeSolver solver;
private JPanel contentPane;
public JMaze(JMazeSolver mazeSolver)
{
super("Zombie Run");
solver = mazeSolver;
setResizable(false);
setDefaultCloseOperation(2);
addWindowListener(this);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(new BorderLayout());
contentPane.add(mazeSolver,BorderLayout.NORTH);
final JButton jbutton = new JButton("Feed the Zombie");
contentPane.add(jbutton,BorderLayout.SOUTH);
setContentPane(contentPane);
jbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
solver.solve();
contentPane.revalidate();
contentPane.repaint();
jbutton.setEnabled(false);
}
});
pack();
setVisible(true);
}
public void windowActivated(WindowEvent windowevent)
{
}
public void windowClosing(WindowEvent windowevent)
{
}
public void windowDeactivated(WindowEvent windowevent)
{
}
public void windowDeiconified(WindowEvent windowevent)
{
}
public void windowIconified(WindowEvent windowevent)
{
}
public void windowOpened(WindowEvent windowevent)
{
}
public void windowClosed(WindowEvent windowevent)
{
System.exit(0);
}
public static void main(String[] args)
{
new JMaze(new JMazeSolver(10));
}
}
Code
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class JMazeSolver extends JPanel{
private MazeSolver mazeSolver;
private String[][] maze;
private JLabel[][] mazeLabels;
public JMazeSolver(int mazeSize)
{
setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
setLayout(new GridLayout(mazeSize, mazeSize));
maze = new String[mazeSize][mazeSize];
mazeLabels = new JLabel[mazeSize][mazeSize];
initializeMaze();
mazeSolver = new RecursiveMazeSolver();
}
public void solve()
{
mazeSolver.solve(maze);
}
private void initializeMaze()
{
for(int i = 0; i < maze.length; i++)
for(int j = 0; j < maze[i].length; j++)
{
final int row = i;
final int col = j;
mazeLabels[row][col] = new JLabel();
maze[row][col] = "";
final JLabel cellLabel = mazeLabels[row][col];
cellLabel.setOpaque(true);
Dimension size = new Dimension(30,20);
cellLabel.setSize(size);
cellLabel.setMaximumSize(size);
cellLabel.setPreferredSize(size);
cellLabel.setMaximumSize(size);
cellLabel.setBorder(BorderFactory.createLineBorder(Color.blue));
cellLabel.setBackground(Color.black);
add(cellLabel);
cellLabel.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent arg0) {
// the color changes when clicked
if(maze[row][col].equals(""))
{
cellLabel.setBackground(Color.CYAN);
cellLabel.setText("");
maze[row][col] = "W";
}
else if(maze[row][col].equals("W"))
{
cellLabel.setBackground(Color.BLACK);
cellLabel.setText("Start");
maze[row][col] = "S";
}
else if(maze[row][col].equals("S"))
{
cellLabel.setBackground(Color.BLACK);
cellLabel.setText("End");
maze[row][col] = "F";
}
else if(maze[row][col].equals("F"))
{
cellLabel.setBackground(Color.BLACK);
cellLabel.setText("");
maze[row][col] = "";
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int i = 0; i < maze.length; i++)
{
for(int j = 0; j < maze[i].length; j++)
{
mazeLabels[i][j].setText("");
if(maze[i][j].equals(""))
{
mazeLabels[i][j].setBackground(Color.BLACK);
}
else if (maze[i][j].equals("X"))
{
mazeLabels[i][j].setBackground(Color.RED);
}
else if( maze[i][j].equals("D"))
{
mazeLabels[i][j].setBackground(Color.YELLOW);
}
else if(maze[i][j].equals("W"))
{
mazeLabels[i][j].setBackground(Color.CYAN);
}
else if(maze[i][j].equals("S"))
{
mazeLabels[i][j].setBackground(Color.BLACK);
mazeLabels[i][j].setText("Start");
}
else if(maze[i][j].equals("F"))
{
mazeLabels[i][j].setBackground(Color.BLACK);
mazeLabels[i][j].setText("End");
}
}
}
}
}
Basically, you can click on each cell prior to pushing the button, and it will cycle through setting the cell (wall, start, end, or clear). After you push the button it will solve the maze, and then color the path red for path taken, or yellow for exhausted path.