java - 多维数组java的行和列

标签 java arrays swing multidimensional-array

我正在为我正在上的高中类(class)制作/创建一个基于 Java 的游戏,但我不知道它的逻辑。

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

我正在通过“for”循环尝试此操作,该循环将采用“coords[][]”并用值标记某个“虚构”位置,例如:floor。

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

现在我听说/读到 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 方 block 都被标记为由“coords[x][y]”表示的特定坐标的特定地形

然后,在标记了坐标之后,它应该返回并分别为“paintComponent”中分配的“地形”绘制每个坐标

在执行此操作时,程序应该创建一个 map 供玩家通过。

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

这是该程序的工作模型及其问题:

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

最佳答案

两个要点:

  • 绘画每次都具有破坏性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高,将所有瓷砖涂在上面和 paintComponent 中方法,绘制图像

基于可运行的示例...

  • mapGen从未被调用,因此 coord 中的值总是0或`地板
  • for (x = 0; x <= width; x++) {for (y = 0; y <= height; y++) {将导致IndexOutOfBoundsException因为数组是从零开始的 ( 0-23 )
  • coords[24][24] = terrain[0];将导致IndexOutOfBoundsException出于与上述相同的原因。

一旦更正,代码可以生成类似...的内容

Random Patch of Earth

关于java - 多维数组java的行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36852964/

相关文章:

java - groovy 如何使用名称创建数组?

c - 在 C 中使用 va_list 作为数组

java - jScrollPane 中的 jPanel 不起作用

java - JTextField setText 不起作用

Java- 键绑定(bind)不工作/actionPerformed 未被调用

java - 当前线程调用多个方法

java - 如何在Json Parser android中设置请求超时

java - 某种数据持久性

java - 为什么 spring rest 文档使用 MockMvcBuilders.webAppContextSetup?

java - 如何在同一个二维数组中使用两个构造函数? java