使用矩阵创建迷宫(JAVA) [英] Creating a maze using matrixes (JAVA)

查看:89
本文介绍了使用矩阵创建迷宫(JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图用Java制作一个迷宫(没有生成器),但遇到了障碍.我当前拥有的代码将使一个迷宫,并使其成为jframe,但它不会为它着色...有没有办法使着色工作??

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!

推荐答案

1-)不要在Switch实例上创建类,这不是一个好习惯.

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

2-)如果Class不继承JComponent,则它将无法覆盖paint或paintComponent方法,因为它没有它们.

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-)将类名的首字母大写,并使用有意义的名称.

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

4-)修改您的代码,如下所示:

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

这篇关于使用矩阵创建迷宫(JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆