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

标签 java compiler-errors bufferedimage

这真的很奇怪,错误出现在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();
    }
}

最佳答案

您正在重复使用您为图像指定的数组名称,并将该图像打包为各个元素。您应该给它一个不同的名称以使其正常工作:

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

关于java - 表达式的类型必须是数组类型,但它解析为 BufferedImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13504225/

相关文章:

java - 标记包含空标记的字符串

c# - 带有委托(delegate)的不明确的 C# 方法调用

java - getResourceAsStream 在 Eclipse IDE 中有效,但在 JAR 格式中无效

java - 在 Java 中不使用 ImageIcon 提供可滚动 ImageView (波形)的简单方法?

java - 关于 BufferedImage.getSubimage(int x, int y, int w, int h) 方法的指导?

java - 我的第一个应用程序上的错误消息计算了从YouTube视频中学习到的2个值

java - 从未收到 SSH 服务器标识 - 握手死锁 [SSHJ]

java - 如何在 Java 哈希集中查找和返回对象

c - 错误 : assignment of read-only location

c++ - 错误 : No matching function found