Making a n * n tiled map for the maze

I need to create an nxn column / row tiled map. First, the program asks the user how many tiles he wants, then creates a tiled map. After that, the user clicks on one tile and the tiles change color. Then he clicks on anothe tile and the color changes as well. After that, the program will find a solution from the selected tile to another.

Currently I have created a memory card with a Graphics2D component, but when I click on a tile, it is a whole graphic that changes color, not just one tile ... Could you tell me what happened? What's a great way to draw a tile map? Thank! The maze should look like this:

enter image description here

I still need to enter the wall code and find a solution. This is the code of my JPanel where I am creating the map.

public LabyrintheInteractif (){
    addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            click=true;
            repaint();
            xClick=e.getX();
            yClick=e.getY();
        }
    });

    tiles=Integer.parseInt(JOptionPane.showInputDialog("How many tiles ?"));
    Quadrilage", JOptionPane.YES_NO_OPTION);

    setPreferredSize(new Dimension(734, 567));
    setVisible(true);
}

@Override
public void paintComponent(Graphics g) {

    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.white);

    rect = new Rectangle2D.Double(0, 0,getWidth(), getWidth());
    g2d.fill(rect);
    g2d.setColor(Color.black);

    for (row = 0; row <tuiles; row++) {
        for (column = 0; column < tuiles; column++) {
            g2d.setStroke(new BasicStroke(3));
            g2d.draw( square=new Rectangle2D.Double(column*100 , row*100,100,100));
        }
        if(click){
            g2d.setColor(Color.green);
            g2d.fill(square);
            repaint();
    }
}

      

+3


source to share


1 answer


The problem is that you are not checking which fragment the user clicked on. Instead, you just check to see if he actually clicked on it.

What you need to do is find width

and height

fragments. Then you need to check which fragment the user clicked on the nested view of this type.

for (row = 0; row <tuiles; row++) {
   for (column= 0; column<tuiles; column++) {
      if(clicked){

         //check if the click x position is within the bounds of this tile
         if(column * tileWidth + tileWidth > xClick && column * tileWidth < xClick){

            //check if the click y position is within the bounds of this tile
            if(row * tileHeight + tileHeight > yClick && row * tileHeight < yClick){
               //mark this tile as being clicked on.
               clicked = false;
            }
         }
      }
   }
}

      



Then you will need to store boolean values ​​that will indicate if a particular tile was clicked. Thus, when you paint the tile, you can use something like this:

if(thisTileHasBeenClicked){

   //if the tile has been clicked on
   g2d.setColor(Color.green);
   g2d.fill(square);
}else{

   //if the tile has not been clicked on
   g2d.setColor(Color.gray);
   g2d.fill(square);
}

      

+2


source







All Articles