1

这是我的代码:

int[][] coordX = new int[10000][10000];
int[][] coordY = new int[10000][10000];
int cX = 0;
int cY = 0;

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) (screenSize.getWidth());
int height = (int) (screenSize.getHeight());
BufferedImage dirt = null;
{
    try {
        dirt = ImageIO.read(new File("res/dirt.png"));
    } catch (IOException e) {
    }
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.RED);
    for (int y = 0; y <= height; y = y + 50) {
        for (int x = 0; x <= width; x = x + 50) {
            g.drawImage(dirt, x, y, 50, 50, Color.BLACK, this);
            coordX[cX][cY] = cX;
                coordY[cX][cY] = cY; 
            cX = cX + 1;
            System.out.println("X=" + coordX[cX][cY]);
        }
        cY = cY + 1;
        cX = 0;
        System.out.println("Y=" + coordY[cX][cY]);
    }
}

控制台将所有“X=”值输出为零,“Y=”也相同,但是,当我自己打印“cX”和“cY”值时,它们按预期计数。只有当我尝试将“cX”和“cY”值存储在数组中时,才会出现问题。

4

2 回答 2

0

您正在递增cX并且cY在打印之前。尝试这个:

coordX[cX][cY] = cX;
coordY[cX][cY] = cY; 
System.out.println("X=" + coordX[cX][cY]);
System.out.println("Y=" + coordY[cX][cY]);
cX = cX + 1;
于 2013-07-04T15:39:04.150 回答
0

您在打印之前增加 cX

   cX = cX + 1;
   System.out.println("X=" + coordX[cX][cY]);

即你没有打印你刚刚填充的元素。您将始终获得默认的(未初始化的)int 元素值 (0)。

于 2013-07-04T15:39:28.973 回答