【问题标题】:Rows and columns with multidimensional array java具有多维数组java的行和列
【发布时间】:2016-08-19 13:48:44
【问题描述】:

我正在为我正在上的高中课程制作/创建一个基于 Java 的游戏,它的逻辑让我无法理解。

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

我正在通过一个“for”循环尝试此操作,该循环将采用“coords[][]”并使用值 Ex: 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 正方形被标记为由“coords[x][y]”表示的那些特定坐标的特定地形

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

在执行此操作时,程序应该为玩家创建一个地图以供他们通过。

相反,它的作用是只用“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

【问题讨论】:

  • xypaintComponent 中不会发生变化,它们与您制作地图时保持一致。您可能想查看Painting in AWT and SwingPerforming Custom Painting 了解更多详情
  • 这段代码怎么没有出错? for (xfor (y 循环应该超出 coords 数组(它们一直运行到“coords[24][24] 应该超出数组的大小。我可能在逻辑上遗漏了一些东西。

标签: java arrays swing multidimensional-array


【解决方案1】:

两个要点:

  • 绘制是破坏性的,每次调用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 或 `floor
  • for (x = 0; x &lt;= width; x++) {for (y = 0; y &lt;= height; y++) { 将导致 IndexOutOfBoundsException,因为数组是从零开始的 (0-23)
  • 出于与上述相同的原因,coords[24][24] = terrain[0]; 将导致 IndexOutOfBoundsException

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

【讨论】:

  • 感谢您的快速响应,我之前曾尝试在paintComponent 中放置一个for循环,但每次编译时都会出错。
  • @L.Ballard 好吧,如果没有可运行的示例来证明您的问题,就不可能提出其他建议
  • 如果我把文件交给你会有帮助吗?(对这样的网站非常陌生)BufferedImage 究竟是做什么的?
  • 我是直接编辑原帖还是完全改掉?
猜你喜欢
  • 2016-08-19
  • 2011-02-17
  • 2020-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-28
  • 2012-04-27
  • 1970-01-01
相关资源
最近更新 更多