Search code examples
javaswingmaze

How to display a maze?


So I am working on a maze but for my own preference I want it too look visually appealing, so I got a code to work however its not really graphical. Basically I don't need help getting the maze to work I need help improving how it looks. So I am trying to see if theres any way I can convert what I have to some sort of buttons that display like a certain colour rather than "x" " " "." "-" and "#". I'll also note that I am very knew to coding and probably wouldnt just understand if u told me "do this" so if anyone is kind enough to help me please be patient as it is hard for me to just "do that"

I've tried converting my text to buttons, so that I could just colour code my buttons and have each of my "x" " " "." "-" "#" represented by colours, or if I can (which would be the preferred option), have them replaced by images of like an X a Line a Barrier etc..., however I could not get it to work as I'm literally sitting here clueless.. Like i've tried searching online but I just dont know what to look for.

here i have my entire code and you can see what I mean by I want it more visually appealing... I would prefer to have some sort of buttons with colour or images to represent the texts.

 package maze;

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class maze {

static JFrame mainFrame = new JFrame("MazeProgram");
static JLabel mazeLabel = new JLabel();
static boolean exitFound = false;

static char[][] puzzle = { { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
        { '#', ' ', ' ', ' ', '#', ' ', '#', '#', '#', '#', ' ', 'X', '#' },
        { '#', '#', ' ', '#', '#', ' ', '#', ' ', ' ', '#', ' ', '#', '#' },
        { '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#' },
        { '#', ' ', '#', ' ', ' ', '#', '#', '#', '#', ' ', '#', '#', '#' },
        { '#', '#', '#', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
        { '#', ' ', ' ', ' ', ' ', '#', '#', ' ', ' ', '#', '#', '#', '#' },
        { '#', '#', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', '#', '#' },
        { '#', ' ', '#', ' ', ' ', ' ', '#', '#', ' ', ' ', ' ', ' ', '#' },
        { '#', ' ', '#', '#', ' ', '#', '#', ' ', '#', ' ', '#', ' ', '#' },
        { '#', ' ', ' ', ' ', ' ', '#', '#', ' ', ' ', ' ', '#', '#', '#' },
        { '#', ' ', '#', '#', '#', ' ', '#', ' ', '#', ' ', ' ', '#', '#' },
        { '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', '#', ' ', ' ', ' ', '#' },
        { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }, };

public static void main(String[] args) throws InterruptedException {
    initializeWindow();
    move(1, 1);
    printMaze();
}

public static void initializeWindow() {
    mainFrame = new JFrame("Maze Solver");
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setLayout(null);
    mainFrame.setSize(1920, 1080);
    mainFrame.setLocationRelativeTo(null);

    mazeLabel.setHorizontalAlignment(JLabel.CENTER); // centers the Component
    mazeLabel.setSize(1920, 1080); // sets maze label size
    mazeLabel.setFont(new Font("", Font.BOLD, 28));

    mainFrame.add(mazeLabel);
    mazeLabel.setVisible(true);
    mainFrame.setVisible(true);
}

public static void printMaze() { // prints the maze to a label
    mazeLabel.setText(""); // clear the label
    String tempLine = "<html><pre>"; // stores the maze from the char array to a string set up for html to allow new
                                        // lines. pre tells html to keep multiple spaces

    for (int j = 0; j < puzzle.length; j++) { // changes the row number
        for (int i = 0; i < puzzle[0].length; i++) { // changes the column number.
            tempLine = tempLine + puzzle[j][i] + " "; // add one character from the maze to the string
        }
        tempLine = tempLine + "<br>"; // add a line break to the string
    }
    tempLine = tempLine + "</html>";
    mazeLabel.setText(tempLine); // put the string into the label

}

public static void move(int row, int col) throws InterruptedException {

    if (puzzle[row][col] == 'X') { // checks if the maze is at the end
        exitFound = true;

    } else {

        puzzle[row][col] = '-'; // change the current location symbol to indicate that the spot has been visited
        //Thread.sleep(50);

        if ((exitFound == false) && (puzzle[row][col + 1] == ' ' || puzzle[row][col + 1] == 'X')) {
            move(row, col + 1);
        }
        if ((exitFound == false) && (puzzle[row + 1][col] == ' ' || puzzle[row + 1][col] == 'X')) {
            move(row + 1, col);
        }
        if ((exitFound == false) && (puzzle[row][col - 1] == ' ' || puzzle[row][col - 1] == 'X')) {
            move(row, col - 1);
        }
        if ((exitFound == false) && (puzzle[row - 1][col] == ' ' || puzzle[row - 1][col] == 'X')) {

            move(row - 1, col);
        }

        if (exitFound == true) {
            puzzle[row][col] = '.';
            //Thread.sleep(50);

        }
    }

}
}

My expected results are to get the input of that text and convert the display to buttons that i can place images on or at the least buttons which i can change the colours of and the actual output is just the text and i have no idea how to convert it.


Solution

  • It is very similar to what you did in a previous question you posted.
    Use a GridPanel and populate it with JLabels:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class Maze { // see java naming conventions https://www.geeksforgeeks.org/java-naming-conventions/
    
        static char[][] puzzle = { { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
                { '#', ' ', ' ', ' ', '#', ' ', '#', '#', '#', '#', ' ', 'X', '#' },
                { '#', '#', ' ', '#', '#', ' ', '#', ' ', ' ', '#', ' ', '#', '#' },
                { '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#' },
                { '#', ' ', '#', ' ', ' ', '#', '#', '#', '#', ' ', '#', '#', '#' },
                { '#', '#', '#', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
                { '#', ' ', ' ', ' ', ' ', '#', '#', ' ', ' ', '#', '#', '#', '#' },
                { '#', '#', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', '#', '#' },
                { '#', ' ', '#', ' ', ' ', ' ', '#', '#', ' ', ' ', ' ', ' ', '#' },
                { '#', ' ', '#', '#', ' ', '#', '#', ' ', '#', ' ', '#', ' ', '#' },
                { '#', ' ', ' ', ' ', ' ', '#', '#', ' ', ' ', ' ', '#', '#', '#' },
                { '#', ' ', '#', '#', '#', ' ', '#', ' ', '#', ' ', ' ', '#', '#' },
                { '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', '#', ' ', ' ', ' ', '#' },
                { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }, };
    
        public static void main(String[] args) throws InterruptedException {
            Maze maze = new Maze();
            maze.initializeWindow();
        }
    
        private void initializeWindow() {
            JFrame mainFrame = new JFrame("Maze Solver");
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainFrame.setLayout(new GridLayout(puzzle.length, puzzle[0].length));// avoid null layouts
            //mainFrame.setSize(1920, 1080); //use preferred size and let layout manager set the size
            mainFrame.setLocationRelativeTo(null);
    
            for (int row = 0; row < puzzle.length; row++) {
                for (int col = 0; col < puzzle[0].length; col++) {
                    JLabel label = makeLabel(puzzle[row][col]);
                    mainFrame.add(label);
                }
            }
            mainFrame.pack();
            mainFrame.setVisible(true);
        }
    
        private JLabel makeLabel(char c) {
    
            JLabel label= new JLabel();
            label.setHorizontalAlignment(JLabel.CENTER);
            label.setPreferredSize(new Dimension(40, 40));
            switch(c) {
                case '#':
                    label.setBackground(Color.BLUE);
                    break;
                default:
                    label.setBackground(Color.WHITE);
                    break;
    
            }
            label.setOpaque(true);
            label.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1));
            return label;
        }
    }
    

    Code which is not essential for the answer was removed to make it mcve.

    enter image description here