Error while creating array of BufferedImage in Java

huangapple go评论74阅读模式
英文:

Error while creating array of BufferedImage in java

问题

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.IOException;

public class ImageLoader {
    public static BufferedImage loadImage(String path) {
        try {
            return ImageIO.read(ImageLoader.class.getResource(path));
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        return null;
    }
}

public class Assets {

    BufferedImage BackgroungImage, PlayerSprite, Enemy1, Enemy2, Enemy3, Enemy4;
    BufferedImage[] EnemySprites = new BufferedImage[4];

    public static void init() {
        BackgroundImage = ImageLoader.loadImage("/Assets/Images/BG.jpg");

        PlayerSprite = ImageLoader.loadImage("/Assets/Sprites/WaveRider.png");

        Enemy1 = ImageLoader.loadImage("/Assets/Sprites/Enemy1.png");
        Enemy2 = ImageLoader.loadImage("/Assets/Sprites/Enemy2.png");
        Enemy3 = ImageLoader.loadImage("/Assets/Sprites/Enemy3.png");
        Enemy4 = ImageLoader.loadImage("/Assets/Sprites/Enemy4.png");

        EnemySprites = new BufferedImage[]{Enemy1, Enemy2, Enemy3, Enemy4};
    }
}
英文:
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.IOException;

public class ImageLoader {
	public static BufferedImage loadImage(String path)
	{
		try {
			return ImageIO.read(ImageLoader.class.getResource(path));
		} catch(IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
		return null;
	}
}

public class Assets {

	BufferedImage BackgroungImage, PlayerSprite, Enemy1, Enemy2, Enemy3, Enemy4;
	BufferedImage[] EnemySprites = new BufferedImage[4];

	public static void init()
	{
		BackgroundImg = ImageLoader.loadImage("/Assets/Images/BG.jpg");

		PlayerSprite = ImageLoader.loadImage("/Assets/Sprites/WaveRider.png");

		Enemy1 = ImageLoader.loadImage("/Assets/Sprites/Enemy1.png");
		Enemy2 = ImageLoader.loadImage("/Assets/Sprites/Enemy2.png");
		Enemy3 = ImageLoader.loadImage("/Assets/Sprites/Enemy3.png");
		Enemy4 = ImageLoader.loadImage("/Assets/Sprites/Enemy4.png");

		EnemySprites = {Enemy1, Enemy2, Enemy3, Enemy4};		
	}

}

I have been trying to create an array of BufferedImage using the code above but I am getting the error bellow.I am getting the same type of errors for all the variables I am trying to store in the array. I can not trace the errors in the code. I tried assigning the values of the array while declaring but it also gave me the same errors.

Assets.java:19: error: illegal start of expression
		EnemySprites = {Enemy1, Enemy2, Enemy3, Enemy4};		
		               ^
Assets.java:19: error: not a statement
		EnemySprites = {Enemy1, Enemy2, Enemy3, Enemy4};		
		                ^
Assets.java:19: error: ';' expected
		EnemySprites = {Enemy1, Enemy2, Enemy3, Enemy4};		
		                      ^
Assets.java:19: error: not a statement
		EnemySprites = {Enemy1, Enemy2, Enemy3, Enemy4};		
		                        ^
Assets.java:19: error: ';' expected
		EnemySprites = {Enemy1, Enemy2, Enemy3, Enemy4};		
		                              ^		
		                                              ^

答案1

得分: 1

问题1: 需要注意的是,使用 {"literal", "array", "value"} 进行数组初始化只允许在定义时内联进行,也就是在你定义数组时进行。

你需要像这样进行初始化:new YourType[]{这里是值..};


问题2: 需要注意的是,所有这些变量:

BackgroungImage、PlayerSprite、Enemy1、Enemy2、Enemy3、Enemy4;

都是实例(非静态)字段,而你正试图从静态上下文中引用它们,但你不能这样做,因为实例成员属于实例,而静态成员属于类。

你可以选择:

  1. 创建一个 Assets 的新实例并访问该实例的成员;
  2. 将你的字段定义为静态的。

问题3:

我也认为这是一个问题:请不要为类成员的命名使用帕斯卡命名法(嵌套类除外)。

英文:

You have several problems in your code.

Problem 1: Note, also, that array initialization with the {"literal", "array", "value"} is only allowed inline with the definition, i.e. when you define your array.

You will have to initialize it like: new YourType[]{values here..};.


Problem 2: Note, that all these variables:

BackgroungImage, PlayerSprite, Enemy1, Enemy2, Enemy3, Enemy4;

are Instance(non-static) fields, and you are trying to refer them from a Static Context, which you cannot do as instance member belongs to instance, and static member belongs to the class.

You can do either:

  1. create a new instance of Assets and access the members of that instance;
  2. define your fields as static.

Problem 3:

I will call this a problem as well: Please, do NOT use PascalCase for naming your class members (except for nested classes).

答案2

得分: 0

为什么不尝试使用索引进行赋值?

EnemySprites[0] = Enemy1;
...
...
EnemySprites[3] = Enemy4;

英文:

Why don't you try assignments with indices?

EnemySprites[0] = Enemy1;
..
..
EnemySprites[3] = Enemy4;

huangapple
  • 本文由 发表于 2020年8月27日 22:58:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63618794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定