Search code examples
javaswingmaze

Creating a maze using matrixes (JAVA)


So i'm trying to make a Single maze (No generators) In Java, and I've hit a roadblock. the current code i have will make a maze, and make a jframe, but it won't color it... is there a way to make the coloring work??

    import java.awt.*;
    import javax.swing.*;
    import java.lang.*;

    public class ayy{

    public static void main(String [] args){

    JFrame frame = new JFrame("Maze");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setSize(1000,1000);
    frame.setVisible(true);

    int width = 1;
    int height = 1;

    int [][] map= {
             {0,0,0,0,0,0,0,0,0,0,},
             {0,0,0,0,0,0,0,0,0,0,},
             {2,1,1,1,0,0,0,0,0,0,},
             {0,0,0,1,0,0,0,1,1,2,},
             {0,0,0,1,0,0,0,1,0,0,},
             {0,0,0,1,0,0,0,1,0,0,},
             {0,0,0,1,1,1,1,1,0,0,},
             {0,0,0,0,0,0,0,0,0,0,},
             {0,0,0,0,0,0,0,0,0,0,},
             {0,0,0,0,0,0,0,0,0,0,}
           };

    for(int i=0;i<map.length;i++){
       for(int j=0;j<map.length;j++){
         switch(map[i][j]){
          case 0:
          class rectangle{

       public void paint(Graphics g){
      g.drawRect(1,1,1,1);
      g.setColor(Color.red);
    }  
    }
   break;
  case 1:
  class rectangle2{

       public void paint(Graphics g){
      g.drawRect(1,1,1,1);
      g.setColor(Color.yellow);
    }  
    }       break;
  case 2:
 class rectangle3{

       public void paint(Graphics g){
      g.drawRect(1,1,1,1);
      g.setColor(Color.blue);
    }  
    }       break;
         }
       }  
    }
    }
    }

Any help will do Thanks!


Solution

  • 1-)Do not create Classes on Switch cases, it is not a good practice.

    2-)If the Class is not inheriting JComponent then it will not be able to override paint nor paintComponent methods because it does not have them.

    3-)Uppercase the first letter of class names, and use meaningful names.

    4-)Modify your code like the following:

    public class MazeApp extends JFrame {
    
    public static void main(String[] args) {
    
        JFrame frame = new JFrame("Maze");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setSize(1000, 1000);
        Maze brd = new Maze();
        frame.add(brd);
        frame.setVisible(true);
    }
    }
    
    
    class Maze extends JPanel {
    
    public Maze() {
    }
    
    protected void paintComponent(Graphics g) {
        int width = 1;
        int height = 1;
    
        int[][] map = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                { 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, },
                { 0, 0, 0, 1, 0, 0, 0, 1, 1, 2, },
                { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, },
                { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, },
                { 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, },
                { 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, },
                { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, },
                { 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, } };
    
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map.length; j++) {
                int factori = i * 50;
                int factorj = j * 50;
                switch (map[i][j]) {
                case 0: {
                    g.setColor(Color.red);
                    g.fillRect(factori, factorj, 2, 2);
    
                }
                    break;
                case 1: {
                    g.setColor(Color.yellow);
                    g.fillRect(factori, factorj, 2, 2);
    
                }
                    break;
                case 2: {
                    g.setColor(Color.blue);
                    g.fillRect(factori, factorj, 2, 2);
    
                }
                    break;
                }
            }
        }
    }
    }