具有多维数组java

时间:2016-04-25 23:38:53

标签: java arrays swing multidimensional-array

我正在为我正在学习的高中课程制作/创建一个基于Java的游戏,它的逻辑正在逃避我。

我想要做的目标是制作一个游戏,在JPanel上随机生成一个24x24网格中的“地形”正方形(因为我发现它是最常见的绘制对象)所有这一切都在它自己身上没有外部(用户)的帮助。

我正在通过“for”循环尝试这个,它将采用“coords [] []”并用值Ex:floor标记某个“虚构”位置。

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

现在我听说/读过Java中没有“多维”数组,例如Small Basic。但我不明白数组内部的数组具体是什么。关于这方面的一些解释将不胜感激。

此外,任何可以使我的程序更容易/更顺畅的替代建议都会受到赞赏,因为我现在是一名新手程序员。

我在这些帖子中找到了解决问题的方法,但无济于事。

how to create dynamic two dimensional array in java?

How to insert values in two dimensional array programmaticaly?

how to automatically populate a 2d array with numbers

How to fill a two Dimensional ArrayList in java with Integers?

ArrayList.toArray() method in Java

How can I dynamically add items to a Java array?

Automatic adding of elements to an array

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

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

如果在响应中对其进行了解释,那么在Java编程中不被视为“基本”的任何和所有术语/代码都将非常受欢迎。

请尽可能详细地说明您的回复,因为我还在学习Java及其工作原理。

编辑
所以这段代码有点起作用,但它没有完成它应该做的事情。

这个程序(至少部分)应该工作的方式是循环每个单独的20x20方格被标记为由“coords [x] [y]”表示的特定坐标的特定地形

然后在标记了坐标后,它应该返回并分别为“paintComponent”中指定的“terrain”绘制每个

在这样做的过程中,程序应该为玩家创建一个地图,让他们一路走来。

相反它的作用是覆盖整个JPanel只有“terrain [dirt]”而没有其他

以下是该计划的工作模型及其问题:

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

public 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 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}; //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]; //should mark the coordinates as floor

                }

                if(z == 12)
                {
                    coords[x][y] = terrain[3];//should mark the coordinates as water
                }

                if(z >= 13 && z <= 17)
                {
                    coords[x][y] = terrain[2];//should mark the coordinates as stone
                }

                if(z >= 18 && z <= 20)
                {
                    coords[x][y] = terrain[1];//should mark the coordinates as dirt
                }

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

        for(int x = 0; x < width; x++)
        {
            for(int y = 0; y < height; y++)
            {
                if(coords[x][y] == terrain[floor])//should paint the floor color at marked coordinates
                {
                    g.setColor(new Color(46,46,46));
                    g.fillRect((x*20), (y*20), 20, 20); 

                }

                if(coords[x][y] == terrain[dirt]);//should paint the dirt color at marked coordinates
                {
                    g.setColor(new Color(135,102,31));
                    g.fillRect((x*20), (y*20), 20, 20);
                }

                if(coords[x][y] == terrain[stone])//should paint the stone color at marked coordinates
                {
                    g.setColor(new Color(196,196,196));
                    g.fillRect((x*20),(y*20),20,20);
                }

                if(coords[x][y] == terrain[water])//should paint the water color at marked coordinates
                {
                    g.setColor(new Color(85,199,237));
                    g.fillRect((x*20),(y*20),20,20);
                }
            }
        }

    }//end paintComponent

public static void main(String[] args)
{
    gamePanel panel = new gamePanel();
    JFrame frame = new JFrame();
    frame.setSize(480,480);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(panel);
    frame.setVisible(true);

}//end main

}// end gamePanel

1 个答案:

答案 0 :(得分:2)

两个要点:

  • 绘画具有破坏性,每次调用paintComponent时,都需要重新绘制整个组件状态
  • xy不会改变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 ... {
                //...
            }
        }
    }

}

有关详细信息,请参阅Painting in AWT and SwingPerforming Custom Painting

就个人而言,在mapGen中,我会创建一个宽BufferedImage宽且24*20高的24*20,将所有切片绘制到其中{{1}方法,绘制图像

基于可运行的示例...

    永远不会调用
  • paintComponent,因此mapGen中的值始终为coord或“floor
  • 0for (x = 0; x <= width; x++) {将导致for (y = 0; y <= height; y++) {,因为数组基于零(IndexOutOfBoundsException
  • 由于上述原因,
  • 0-23会导致coords[24][24] = terrain[0];

更正后,代码可以生成类似......

的内容

Random Patch of Earth