【问题标题】:The type of the expression must be an array type but it resolved to BufferedImage表达式的类型必须是数组类型,但它解析为 BufferedImage
【发布时间】:2012-11-22 00:27:17
【问题描述】:

真的很奇怪,错误在textures[x]

表达式的类型必须是数组类型,但它解析为 BufferedImage

这里的代码有什么问题?

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

【问题讨论】:

    标签: java compiler-errors bufferedimage


    【解决方案1】:

    您正在重用您为数组指定的名称,用于您计划打包成单个元素的图像。您应该给它一个不同的名称以使其工作:

    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】:

      看起来模棱两可。将局部变量名称从 BufferedImage textures 更改为 BufferedImage texture

      【讨论】:

        【解决方案3】:

        您在此处创建一个名为 textures 的新变量:

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

        它不是像static 变量那样的二维数组。 for-loop 中的 textures[x][y] 引用了 this 变量,这解释了错误。重命名其中一个以解决问题。

        顺便说一句,这叫做variable shadowing

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-24
          • 2019-10-25
          • 1970-01-01
          • 1970-01-01
          • 2016-02-20
          • 1970-01-01
          • 2012-04-20
          • 2018-08-14
          相关资源
          最近更新 更多