Search code examples
javapath-finding

MouseClicked() method not working for pathfinding algorithm?


I am trying to write a pathfinding maze algorithm to attempt to implement A* into a JPanel interface. The code is as follows. As you can see, I randomly generate the color of the squares for the maze using a random number generator. The following is a rudimentary implementation of the source code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Scanner; 
import java.util.Random;


public class algo extends JPanel
implements MouseListener, MouseMotionListener
{


static int[][] map;

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    this.setBackground(Color.WHITE);

    //draw a for loop to print the map
    for (int i = 0; i < map.length; i++) {
        for(int j = 0; j < map[i].length; j++) {
            g.setColor(Color.WHITE);

            if(map[i][j] == 1) {
                g.setColor(Color.BLACK);
            }

            g.fillRect(j * 20, i * 20, map[i].length * 20, map.length *20);

        }
    }

}

public static void main(String[] args) {
    System.out.println("Welcome to the A* Shortest Pathfinding Robot Program \n *****"
            + "**************************"
            + "********************\n");
    System.out.println("How large would you like your graph to be? Enter 2 consecutive numbers, one for length, one for width:\n");
    Scanner sizeScan = new Scanner(System.in);
    int length = sizeScan.nextInt();
    int width = sizeScan.nextInt();
    map = new int[length][width];
    Random gridGenerate = new Random();
    for(int i = 0; i < map.length; i++) {
        for (int j = 0; j < map[i].length; j++) {
            map[i][j] = gridGenerate.nextInt(2);
            System.out.print(map[i][j] + " ");
        }
        System.out.println();

    }
    JFrame f = new JFrame("A Star Pathfinder");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    algo star = new algo();
    f.add(star);
    f.setSize(length * 20, width * 20);
    f.setVisible(true);

}

@Override
public void mouseDragged(MouseEvent e) {}

@Override
public void mouseMoved(MouseEvent e) {}

@Override
public void mouseClicked(MouseEvent e) {
    System.out.println("Successfully Clicked");
    if (SwingUtilities.isLeftMouseButton(e)) {
        System.out.println("This is the left mouse button that is clicked");
    }
    }



@Override
public void mouseEntered(MouseEvent e) {}

@Override
public void mouseExited(MouseEvent e) {}

@Override
public void mousePressed(MouseEvent e) {}

@Override
public void mouseReleased(MouseEvent e) {}

}

When I ran the main() method, I was able to successfully generate the maze:

Here is the "Maze" Generated from the code

However, when I tried to implement the MouseClick() action on the maze nothing happens. I have print statements to try to test this out, and every possible solution has not solved the issue.

  1. Attempted to implement the run() method inside the main code
  2. Attempted to implement a run() method inside the class
  3. Attempted to make a private handler class?

Any other ideas as to why the mouseHandler is not responding to my requests?


Solution

  • You have to explicitly add the mouse listener to the JPanel.

    public class algo extends JPanel implements MouseListener, MouseMotionListener {
    
        public algo() {
            this.addMouseListener(this);
            this.addMouseMotionListener(this);
        }
        // other stuff
    }