The type of the expression must be an array type but it resolved to BufferedImage

我的梦境 提交于 2019-12-11 01:12:53

问题


It's being really weird, the error is at textures[x].

The type of the expression must be an array type but it resolved to BufferedImage

What is wrong with the code here?

static BufferedImage textures[][] = new BufferedImage[20][20];

public static void loadTextures()
{
    try 
    {
        //Loads The Image
        BufferedImage textures = ImageIO.read(new URL("textures.png"));

        for (int x = 0; x < 1280; x += 1)
        {
            for (int y = 0; y < 1280; y += 1)
            {
                textures[x][y] = textures.getSubimage(x*64, y*64, 64, 64);
            }
        }

    } catch (Exception e) 
    {
        e.printStackTrace();
    }
}

回答1:


You are reusing the name that you gave to your array for the image that you are planning to parcel into individual elements. You should give it a different name to make it work:

BufferedImage fullImage = ImageIO.read(new URL("textures.png"));

for (int x = 0; x < 1280; x += 1) {
    for (int y = 0; y < 1280; y += 1) {
        textures[x][y] = fullImage.getSubimage(x*64, y*64, 64, 64);
    }
}



回答2:


It looks like an ambiguity going on.. change the local variable name from BufferedImage textures to BufferedImage texture




回答3:


You create a new variable called textures here:

BufferedImage textures = ImageIO.read(new URL("textures.png"));

which is not a 2D array like the static variable. textures[x][y] in the for-loop is referencing this variable, which explains the error. Rename one of them to solve the problem.

By the way this is called variable shadowing.



来源:https://stackoverflow.com/questions/13504225/the-type-of-the-expression-must-be-an-array-type-but-it-resolved-to-bufferedimag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!