Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用多维数组java的行和列_Java_Arrays_Swing_Multidimensional Array - Fatal编程技术网

使用多维数组java的行和列

使用多维数组java的行和列,java,arrays,swing,multidimensional-array,Java,Arrays,Swing,Multidimensional Array,我正在为我正在上的一个高中班级制作/创建一个基于Java的游戏,它的逻辑让我不知所措 我想做的是制作一个游戏,在JPanel上以24x24网格随机生成一个“地形”正方形(我发现这是最常见的绘制对象),完全靠它自己,无需外部(用户)帮助 我试图通过一个“for”循环来实现这一点,该循环将采用“coords[][]”,并使用值Ex:floor标记某个“虚构”位置 但当我运行它时,它所做的就是在0,0处绘制第一个20x20正方形 现在我听说/读到Java内部没有类似于示例Small Basic的“多维

我正在为我正在上的一个高中班级制作/创建一个基于Java的游戏,它的逻辑让我不知所措

我想做的是制作一个游戏,在JPanel上以24x24网格随机生成一个“地形”正方形(我发现这是最常见的绘制对象),完全靠它自己,无需外部(用户)帮助

我试图通过一个“for”循环来实现这一点,该循环将采用“coords[][]”,并使用值Ex:floor标记某个“虚构”位置

但当我运行它时,它所做的就是在0,0处绘制第一个20x20正方形

现在我听说/读到Java内部没有类似于示例Small Basic的“多维”数组。但我不明白数组中的数组具体做什么。如能就此作出解释,将不胜感激

另外,任何能使我的程序更容易/更流畅的建议都将非常感谢,因为我目前是一名新手程序员

在这些帖子中,我一直在寻找解决问题的方法,但都没有用

下面是应该为我执行此操作但当前失败的代码块:

class gamePanel extends JPanel
{   

public gamePanel()
{
    setBounds(115,93,480,480);
    setBackground(Color.white);
}

private Random generator = new Random();    

int floor = 0; //initializes the variable floor to zero for later use
int dirt = 1;
int stone = 2;
int water = 3;
int lava = 4;
int iron = 5;
int gold = 6;
int emerald = 7;
int diamond = 8;
int bedrock = 9;

int width = 24;
int height = 24;    
int x, y; // my x & y variables for coordinates

int[][] coords = new int[width][height]; //my array that I want to store the coordinates for later use in painting


int[] terrain = {floor, dirt, stone, water, lava, iron,       
                            gold, emerald, diamond, bedrock}; //my terrain that will determine the color of the paint

public void mapGen() //what should mark/generate the JPanel
{
    for(x = 0; x <= width; x++)
    {

        for(y = 0; y <= height; y++)
        {

            int z = generator.nextInt(20);// part of the randomization

            if(z <= 11)
            {
                coords[x][y] = terrain[0]; //marks the coordinates as floor

            }

            else{};

        }
    }

        coords[0][0] = terrain[0]; // sets coordinate 0,0 to floor //need to have these always be floor
        coords[24][24] = terrain[0]; // sets coordinate 24,24 to floor //^^^^^^^^^^
}   


@Override
public void paintComponent(Graphics g)//what will paint each 20x20 square on the grid what it is assigned
{
    super.paintComponent(g);

    if(coords[x][y] == terrain[floor])
    {
    g.setColor(new Color(46,46,46));
    g.fillRect((x*20),(y*20),20,20);    
    }

    else{};

}
}//end gamePanel
class gamePanel扩展了JPanel
{   
公共游戏小组()
{
挫折(115,93480480);;
挫折地面(颜色:白色);
}
专用随机生成器=新随机();
int floor=0;//将变量floor初始化为零,以便以后使用
int污垢=1;
int-stone=2;
int水=3;
int=4;
int铁=5;
int gold=6;
int祖母绿=7;
int钻石=8;
int=9;
整数宽度=24;
整数高度=24;
int x,y;//坐标的my x&y变量
int[][]coords=new int[width][height];//我要存储坐标以供以后在绘制中使用的数组
int[]地形={地面、泥土、石头、水、熔岩、铁、,
黄金、祖母绿、钻石、基岩};//我的地形将决定油漆的颜色
public void mapGen()//应该标记/生成什么JPanel
{
对于(x=0;x两个要点:

  • 绘制是破坏性的,每次调用
    paintComponent
    ,都需要重新绘制整个组件状态

  • x
    y
    不会在
    paintComponent
    中更改,它们与调用
    mapGen
    后的值相同
相反,你的绘画方法看起来更像

@Override
protected void paintComponent(Graphics g)//what will paint each 20x20 square on the grid what it is assigned
{
    super.paintComponent(g);

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            if (coords[x][y] == terrain[floor]) {
                g.setColor(new Color(46, 46, 46));
                g.fillRect((x * 20), (y * 20), 20, 20);
            } else ... {
                //...
            }
        }
    }

}
@覆盖
受保护的void paintComponent(图形g)//网格上的每个20x20正方形将被绘制成什么样的图形
{
超级组件(g);
对于(int x=0;x
有关详细信息,请参阅和

就个人而言,在
mapGen
中,我会创建一个
BuffereImage
24*20
24*20
BuffereImage,然后在
paintComponent
方法中绘制图像

基于可运行的示例。。。
  • 从不调用
    mapGen
    ,因此
    coord
    中的值总是
    0
    或'floor'
  • 用于(x=0;x两个要点:

    • 绘制是破坏性的,每次调用
      paintComponent
      ,都需要重新绘制整个组件状态

    • x
      y
      不会在
      paintComponent
      中更改,它们与调用
      mapGen
      后的值相同
    相反,你的绘画方法看起来更像

    @Override
    protected void paintComponent(Graphics g)//what will paint each 20x20 square on the grid what it is assigned
    {
        super.paintComponent(g);
    
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                if (coords[x][y] == terrain[floor]) {
                    g.setColor(new Color(46, 46, 46));
                    g.fillRect((x * 20), (y * 20), 20, 20);
                } else ... {
                    //...
                }
            }
        }
    
    }
    
    @覆盖
    受保护的void paintComponent(图形g)//网格上的每个20x20正方形将被绘制成什么样的图形
    {
    超级组件(g);
    对于(int x=0;x
    有关详细信息,请参阅和

    就个人而言,在
    mapGen
    中,我会创建一个
    BuffereImage
    24*20
    24*20
    BuffereImage,然后在
    paintComponent
    方法中绘制图像

    基于可运行的示例。。。
    • 从不调用
      mapGen
      ,因此
      coord
      中的值总是
      0
      或'floor'

    • 对于(x=0;x
      x
      y
      不要在
      paintComponent
      中更改,它们与您制作地图时保持不变。您可能想看一看,并了解更多详细信息,说明您没有收到此代码的错误吗(y
      循环应该超出
      coords
      数组(它们运行直到“
      x
      y
      paintComponent
      中没有更改,它们与您制作地图时保持不变。您可能想看一看,并了解更多详细信息,显示您没有在该代码中出错吗(x
    for(y
    循环应超出
    coords
    数组(它们运行到“感谢您的快速响应,我之前曾尝试在paintComponent中放置for循环,但每次编译时都会抛出错误。@L.Ballard好吧,如果没有可运行的示例来演示您的问题,就不可能提出任何其他建议。如果我向您提供该文件,它会有帮助吗?(对于此类网站来说非常新)BuffereImage的具体功能是什么?我是编辑原始帖子还是完全更改它?感谢您的快速响应,我以前曾尝试