问题
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