public class SnakePanel extends JPanel{ private static final long serialVersionUID = 1L; int x_width = 40; int y_height = 30; boolean[][]map = new boolean[y_height][x_width]; void initMap(){ for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[i].length; j++) { if(i==0 || i==map.length-1 || j==0 || j==map[i].length-1) map[i][j] = true; //else // map[i][j] = false;默认为false } } } int cell_width = 20; int cell_height = 20; @Override public void paint(Graphics g) { super.paint(g); //外层循环控制的是行数即当前坐标的纵坐标y for (int i = 0; i < map.length; i++) { //内层循环控制的是列数即当前坐标的横坐标x for (int j = 0; j < map[i].length; j++) { if(map[i][j]) g.setColor(Color.DARK_GRAY); else g.setColor(Color.WHITE); g.fill3DRect(j*cell_width, i*cell_height, cell_width, cell_height, true); } } } public static void main(String[] args) { SnakePanel sp = new SnakePanel(); for (int i = 0; i < sp.map.length; i++) { for (int j = 0; j < sp.map[i].length; j++) { System.out.print(sp.map[i][j]); } System.out.println(); } } }