使用矩阵创建迷宫(java)

1cosmwyk  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(362)

所以我试图用java制作一个迷宫(没有生成器),但遇到了一个障碍。我现在的代码会做一个迷宫,也会做一个jframe,但是它不会给它上色。。。有没有办法使着色工作??

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;
         }
       }  
    }
    }
    }

任何帮助都可以,谢谢!

ee7vknir

ee7vknir1#

1-)不要在switch case上创建类,这不是一个好的做法。
2-)如果类没有继承jcomponent,那么它将无法重写paint或paintcomponent方法,因为它没有这些方法。
3-)大写类名称的第一个字母,并使用有意义的名称。
4-)按以下方式修改代码:

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;
            }
        }
    }
}
}

相关问题