Java GUI:突出显示迷宫的路径?

Java GUI:突出显示迷宫的路径?,java,swing,user-interface,actionlistener,jlabel,Java,Swing,User Interface,Actionlistener,Jlabel,我正在一个合作伙伴项目中工作,我的合作伙伴为游戏创建了一个解算器类,而我的任务是创建MazeGUI 当您单击SolveJButton时,它应该高亮显示它在GUI本身上的路径,但它只高亮显示它“结束”的位置,这是最右下角,因为它是一个“W”,所以不应该访问它。如果有解决方案,“F”将变成“RIP”,如果迷宫没有解决方案,它应该有一个JLabel表示迷宫无法解决 如何在ActionListener for solveButton下的代码中输入所有这些内容 import java.awt.Border

我正在一个合作伙伴项目中工作,我的合作伙伴为游戏创建了一个解算器类,而我的任务是创建MazeGUI

当您单击Solve
JButton
时,它应该高亮显示它在GUI本身上的路径,但它只高亮显示它“结束”的位置,这是最右下角,因为它是一个“W”,所以不应该访问它。如果有解决方案,“F”将变成“RIP”,如果迷宫没有解决方案,它应该有一个
JLabel
表示迷宫无法解决

如何在ActionListener for solveButton下的代码中输入所有这些内容

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MazeGUI {
    String appName = "Zombie Attack!";
    JLabel appNameLabel;

    private JFrame frame;
    private JPanel buttonPanel, solvePanel;
    private JButton solveButton;
    // private JLabel noSolutionLabel;

    private final int rowCount = 10;
    private final int colCount = 10;
    private final int startRow = 0;
    private final int startCol = 1;
    private final int endRow = rowCount - 1;
    private final int endCol = colCount - 2;

    String[][] map;

    private void createAndShowGui() {
        frame = new JFrame("Zombie Attack!");

        solveButton = new JButton("Solve");

        map = new String[rowCount][colCount];
        buttonPanel = new JPanel();

        buttonPanel.setLayout(new GridLayout(rowCount, colCount, 2, 2));

        appNameLabel = new JLabel(appName);
        for (int rows = 0; rows < rowCount; rows++) {
            for (int columns = 0; columns < colCount; columns++) {

                map[rows][columns] = " ";

                final JLabel jlabel = new JLabel("");
                jlabel.setBackground(Color.BLACK);

                if (rows == startRow && columns == startCol) {
                    jlabel.setBackground(Color.BLACK);
                    jlabel.setForeground(Color.MAGENTA);
                    jlabel.setText("S");
                    map[startRow][startCol] = "S";
                }

                if (rows == endRow && columns == endCol) {
                    jlabel.setBackground(Color.BLACK);
                    jlabel.setForeground(Color.MAGENTA);
                    jlabel.setText("F");
                    map[endRow][endCol] = "F";
                }

                if (!(rows == startRow && columns == startCol || rows == endRow
                        && columns == endCol)) {
                    if (rows == 0 || rows == rowCount - 1 || columns == 0
                            || columns == colCount - 1) {
                        jlabel.setBackground(Color.LIGHT_GRAY);
                        map[rows][columns] = "W";
                    }
                }


                final int rc = rows;
                final int cc = columns;


                jlabel.addMouseListener(new MouseListener() {
                    boolean clicked = false;

                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if (clicked == false) {
                            clicked = true;
                            jlabel.setBackground(Color.LIGHT_GRAY);
                            map[rc][cc] = "W";


                        } else {
                            clicked = false;
                            jlabel.setBackground(Color.BLACK);
                            map[rc][cc] = "";

                        }
                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                    }

                });

                solveButton.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent event) {

                        MazeSolver solver;
                        solver = new MyMazeSolver();
                        solver.solve(map);
                        jlabel.setBackground(Color.RED);
                        jlabel.setForeground(Color.GREEN);
                        jlabel.setText("RIP");

                        for (int i = 0; i < map.length; i++){
                            for (int j = 0; j < map[0].length; j++){
                                System.out.print(map[i][j] + " ");
                            }
                            System.out.println();
                        }
                    }
                });
                System.out.print(map[rows][columns] + " ");
                buttonPanel.add(jlabel);

                jlabel.setOpaque(true);
            }
            System.out.println();
        }
        frame.add(appNameLabel, BorderLayout.NORTH);
        frame.add(buttonPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setSize(500, 500);
        frame.add(solveButton, BorderLayout.SOUTH);

        frame.setVisible(true);
    }

    public static void main(String[] args) {
        MazeGUI maze = new MazeGUI();
        maze.createAndShowGui();
    }
}
这是我搭档的代码(坐标类):

MyMazeSolver等级:

import java.util.ArrayList;

public class MyMazeSolver implements MazeSolver {
    private static ArrayList<Coordinate> coordinates = new ArrayList<Coordinate>();

    private int row;
    private int col;

    @Override
    public String[][] solve(String[][] map) {
        startingPos(map);
        //for (int i = 0; i < 15; i++) {
        do{
            makeMove(map);

            if (nextToF(map, row, col)) {
                System.out.println(row + "" + col);
                map[row][col] = "X";
            } else {

                if (isExplorable(map, row, col)) {
                    map[row][col] = "D";
                } else {
                    map[row][col] = "X";
                }

                if(deadEnd(map, row, col)){

                }
            }
            addCoordinates(row, col);
            //}
        }while(!isEnded(map));
        print(map);

        // delete printCoordinates
        printCoordinates();
        return map;
    }

    public void startingPos(String[][] map) {
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[0].length; j++) {
                if (map[i][j].contains("S")) {
                    row = i;
                    col = j;
                }
            }
        }
    }

    public boolean clear(String[][] map, int row, int col) {


        if (row <= 0 || col <= 0 ||row > map.length) {
            return false;
        }
        if (col <= 0 ||  col > map[0].length) {
            return false;
        }

        if ("S".equals(map[row][col])) {
            return false;
        }
        if ("W".equals(map[row][col])) {
            return false;
        }
        if ("X".equals(map[row][col])) {
            return false;
        }
        if ("D".equals(map[row][col])) {
            return false;
        }

        return true;
    }

    public void makeMove(String[][] map) {
        if (clear(map, row + 1, col)) {
            row++;
        } else if (clear(map, row, col - 1)) {
            col--;
        } else if (clear(map, row, col + 1)) {
            col++;
        } else if (clear(map, row, col)) {
            row--;
        }

    }

    public boolean explorable(String[][] map, int row, int col) {
        if (row > map.length) {
            return false;
        }
        if (col > map[0].length) {
            return false;
        }

        if (map[row][col].equals("S")) {
            return false;
        }
        if (map[row][col].equals("W")) {
            return false;
        }
        if (map[row][col].equals("X")) {
            return false;
        }
        if (map[row][col].equals("D")) {
            return false;
        }

        return true;
    }

    public boolean isExplorable(String[][] map, int row, int col) {
        int squares = 0;

        if (explorable(map, row + 1, col)) {
            squares++;
        }
        if (explorable(map, row, col - 1)) {
            squares++;
        }
        if (explorable(map, row, col + 1)) {
            squares++;
        }
        if (explorable(map, row - 1, col)) {
            squares++;
        }

        if (squares > 1) {
            return true;
        } else {
            return false;
        }
    }

    public void addCoordinates(int row, int col) {
        coordinates.add(new Coordinate(row, col));
    }

    public void printCoordinates() {
        for (int i = 0; i < coordinates.size(); i++) {

            System.out.println(coordinates.get(i));
        }
    }

    public boolean nextToF(String[][] map, int row, int col) {
        if ("F".equals(map[row + 1][col])) {
            // row++;
            map[row + 1][col] = ("RIP");
            return true;
        } else if ("F".equals(map[row][col - 1])) {
            // col--;
            map[row][col - 1] = "RIP";
            return true;
        } else if ("F".equals(map[row][col + 1])) {
            // col++;
            map[row][col + 1] = "RIP";
            return true;
        } else if ("F".equals(map[row - 1][col])) {
            // row--;
            map[row - 1][col] = "RIP";
            return true;
        }
        return false;
    }

    public boolean deadEnd(String[][] map, int row, int col) {
        int deadEnds = 0;

        if (row > map.length) {
            deadEnds++;
        }
        if (col > map[0].length) {
            deadEnds++;
        }
        if (map[row][col].equals("S")) {
            deadEnds++;
        }
        if (map[row][col].equals("W")) {
            deadEnds++;
        }
        if (map[row][col].equals("X")) {
            deadEnds++;
        }
        if (map[row][col].equals("D")) {
            deadEnds++;
        }

        if (deadEnds == 4) {
            return true;
        } else {
            return false;
        }

    }

    public void findD(){

    }

    public boolean isEnded(String[][] map) {
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map.length; j++) {
                if (map[i][j].equals("RIP")) {
                    return true;
                }
            }
        }
        return false;
    }

    public void print(String[][] map) {
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[0].length; j++) {
                System.out.print(map[i][j]);

            }
            System.out.println();
        }
    }

}
import java.util.ArrayList;
公共类MyMazeSolver实现MazeSolver{
私有静态ArrayList坐标=新ArrayList();
私人int row;
私人国际学院;
@凌驾
公共字符串[][]求解(字符串[][]映射){
startingPos(地图);
//对于(int i=0;i<15;i++){
做{
makeMove(地图);
if(下一个(地图、行、列)){
系统输出打印项次(行+列+列);
地图[行][列]=“X”;
}否则{
if(可解释(地图、行、列)){
地图[行][col]=“D”;
}否则{
地图[行][列]=“X”;
}
if(死区(地图、行、列)){
}
}
添加坐标(行、列);
//}
}而(!isEnded(map));
印刷(地图);
//删除打印坐标
打印坐标();
返回图;
}
public void startingPos(字符串[][]映射){
对于(int i=0;i映射[0]。长度){
返回false;
}
if(map[row][col].equals(“S”)){
返回false;
}
if(map[row][col].equals(“W”)){
返回false;
}
if(map[row][col].equals(“X”)){
返回false;
}
if(map[row][col].equals(“D”)){
返回false;
}
返回true;
}
公共布尔值isExplorable(字符串[][]映射,整数行,整数列){
整数平方=0;
if(可浏览(地图,行+1,列)){
正方形++;
}
if(可浏览(地图、行、列-1)){
正方形++;
}
if(可浏览(地图、行、列+1)){
正方形++;
}
if(可浏览(地图,第1行,第2列)){
正方形++;
}
如果(正方形>1){
返回true;
}否则{
返回false;
}
}
公共空添加坐标(整数行,整数列){
坐标。添加(新坐标(行、列));
}
公共空间坐标(){
对于(int i=0;i映射长度){
死区++;
}
如果(列>映射[0]。长度){
死区++;
}
if(map[row][col].equals(“S”)){
死区++;
}
if(map[row][col].equals(“W”)){
死区++;
}
if(map[row][col].equals(“X”)){
死区++;
}
if(map[row][col].equals(“D”)){
死区++;
}
如果(死区==4){
返回true;
}否则{
返回false;
}
}
公共无效findD(){
}
公共布尔值isEnded(字符串[][]映射){
对于(int i=0;i
我在solveButton的ActionListener下调用他的类,但我没有 知道如何在单击JLabel时更新字符串[][]映射。对于 例如,如果我单击(5,5)处的JLabel,映射将更新映射[5][5]至 “W”而不是空字符串

这里的问题是,您依赖的是不变的信息

public void mouseClicked(MouseEvent e) {
    if (clicked == false) {
        clicked = true;
        jlabel.setBackground(Color.LIGHT_GRAY);
        map[rowCount - 1][colCount - 1] = "W";

    } else {
        clicked = false;
        jlabel.setBackground(Color.BLACK);
        //map[rowCount - 1][colCount -1 ] = "";

    }
}
在代码中,您依赖于
rowCount
colCount
,这两个元素不会(也可能不应该)改变,而是只影响最后一个元素。
JLabel
和映射条目之间没有关系

你需要能够做的是提供某种链接。在这种情况下,我通常会使用某种类型的
Map
,键入
JLabel
,并维护我需要的引用
import java.util.ArrayList;

public class MyMazeSolver implements MazeSolver {
    private static ArrayList<Coordinate> coordinates = new ArrayList<Coordinate>();

    private int row;
    private int col;

    @Override
    public String[][] solve(String[][] map) {
        startingPos(map);
        //for (int i = 0; i < 15; i++) {
        do{
            makeMove(map);

            if (nextToF(map, row, col)) {
                System.out.println(row + "" + col);
                map[row][col] = "X";
            } else {

                if (isExplorable(map, row, col)) {
                    map[row][col] = "D";
                } else {
                    map[row][col] = "X";
                }

                if(deadEnd(map, row, col)){

                }
            }
            addCoordinates(row, col);
            //}
        }while(!isEnded(map));
        print(map);

        // delete printCoordinates
        printCoordinates();
        return map;
    }

    public void startingPos(String[][] map) {
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[0].length; j++) {
                if (map[i][j].contains("S")) {
                    row = i;
                    col = j;
                }
            }
        }
    }

    public boolean clear(String[][] map, int row, int col) {


        if (row <= 0 || col <= 0 ||row > map.length) {
            return false;
        }
        if (col <= 0 ||  col > map[0].length) {
            return false;
        }

        if ("S".equals(map[row][col])) {
            return false;
        }
        if ("W".equals(map[row][col])) {
            return false;
        }
        if ("X".equals(map[row][col])) {
            return false;
        }
        if ("D".equals(map[row][col])) {
            return false;
        }

        return true;
    }

    public void makeMove(String[][] map) {
        if (clear(map, row + 1, col)) {
            row++;
        } else if (clear(map, row, col - 1)) {
            col--;
        } else if (clear(map, row, col + 1)) {
            col++;
        } else if (clear(map, row, col)) {
            row--;
        }

    }

    public boolean explorable(String[][] map, int row, int col) {
        if (row > map.length) {
            return false;
        }
        if (col > map[0].length) {
            return false;
        }

        if (map[row][col].equals("S")) {
            return false;
        }
        if (map[row][col].equals("W")) {
            return false;
        }
        if (map[row][col].equals("X")) {
            return false;
        }
        if (map[row][col].equals("D")) {
            return false;
        }

        return true;
    }

    public boolean isExplorable(String[][] map, int row, int col) {
        int squares = 0;

        if (explorable(map, row + 1, col)) {
            squares++;
        }
        if (explorable(map, row, col - 1)) {
            squares++;
        }
        if (explorable(map, row, col + 1)) {
            squares++;
        }
        if (explorable(map, row - 1, col)) {
            squares++;
        }

        if (squares > 1) {
            return true;
        } else {
            return false;
        }
    }

    public void addCoordinates(int row, int col) {
        coordinates.add(new Coordinate(row, col));
    }

    public void printCoordinates() {
        for (int i = 0; i < coordinates.size(); i++) {

            System.out.println(coordinates.get(i));
        }
    }

    public boolean nextToF(String[][] map, int row, int col) {
        if ("F".equals(map[row + 1][col])) {
            // row++;
            map[row + 1][col] = ("RIP");
            return true;
        } else if ("F".equals(map[row][col - 1])) {
            // col--;
            map[row][col - 1] = "RIP";
            return true;
        } else if ("F".equals(map[row][col + 1])) {
            // col++;
            map[row][col + 1] = "RIP";
            return true;
        } else if ("F".equals(map[row - 1][col])) {
            // row--;
            map[row - 1][col] = "RIP";
            return true;
        }
        return false;
    }

    public boolean deadEnd(String[][] map, int row, int col) {
        int deadEnds = 0;

        if (row > map.length) {
            deadEnds++;
        }
        if (col > map[0].length) {
            deadEnds++;
        }
        if (map[row][col].equals("S")) {
            deadEnds++;
        }
        if (map[row][col].equals("W")) {
            deadEnds++;
        }
        if (map[row][col].equals("X")) {
            deadEnds++;
        }
        if (map[row][col].equals("D")) {
            deadEnds++;
        }

        if (deadEnds == 4) {
            return true;
        } else {
            return false;
        }

    }

    public void findD(){

    }

    public boolean isEnded(String[][] map) {
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map.length; j++) {
                if (map[i][j].equals("RIP")) {
                    return true;
                }
            }
        }
        return false;
    }

    public void print(String[][] map) {
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[0].length; j++) {
                System.out.print(map[i][j]);

            }
            System.out.println();
        }
    }

}
public void mouseClicked(MouseEvent e) {
    if (clicked == false) {
        clicked = true;
        jlabel.setBackground(Color.LIGHT_GRAY);
        map[rowCount - 1][colCount - 1] = "W";

    } else {
        clicked = false;
        jlabel.setBackground(Color.BLACK);
        //map[rowCount - 1][colCount -1 ] = "";

    }
}
String[][] map;
// 1
private Map<JLabel, Point> labelMap;
//...
private void createAndShowGui() {
    //...
    map = new String[rowCount][colCount];
    // 2
    labelMap = new HashMap<>(rowCount * colCount);
        //...
        // 3
        labelMap.put(jlabel, new Point(rows - 1, columns - 1));
        jlabel.addMouseListener(new MouseListener() {
jlabel.addMouseListener(new MouseListener() {
    boolean clicked = false;

    @Override
    public void mouseClicked(MouseEvent e) {
        // Get the source of the event
        JLabel label = (JLabel)e.getComponent();
        // Get the map indices associated with the given label
        Point point = labelMap.get(label);

        // Flip the clicked state
        clicked = !clicked;
        // Update the state accordingly...
        if (clicked) {
            jlabel.setBackground(Color.LIGHT_GRAY);
            map[point.x][point.y] = "W";
        } else {
            jlabel.setBackground(Color.BLACK);
            map[point.x][point.y] = "";
        }
    }
if ("S".equals(map[row][col]))) {
} else if (clear(map, row, col - 1)) {
//...
} else if (clear(map, row - 1, col)) {
public boolean clear(String[][] map, int row, int col) {
    if (row < 0 || row > map.length) {
        return false;
    }
    if (col < 0 || col > map[0].length) {
        return false;
    }