1

我一直在尝试创建一个用于我们的 java 游戏的精灵。一切看起来都很正常,我在我的程序上没有看到任何错误,但是当它要运行时突然出现一个错误,它不能被实例化。有人可以告诉我它有什么问题吗?

@SuppressWarnings("unused")
public class TrueSprite
{
BufferedImage spriteSheet = ImageIO.read(new File("robin.png"));

int width = 240, height = 314, rows = 5 , columns = 5;

BufferedImage[] sprites = new BufferedImage[rows * columns];

public TrueSprite(int width, int height, int rows, int columns) throws IOException
{
    this.width = width;
    this.height = height;
    this.rows = rows;
    this.columns = columns;

    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < columns; j++)
        {
            sprites[(i * columns) + j ] = spriteSheet.getSubimage(i * width, j * height, width, height);
        }
    }

}

public void paint(Graphics g)
{
    g.drawImage(sprites[1], 100, 100, null);
}


}

这是错误:

load: really.sprite.TrueSprite.class can't be instantiated.
java.lang.InstantiationException: really.sprite.TrueSprite
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
4

3 回答 3

0

我认为您需要创建一个非参数构造函数,例如

公共 TrueSprite() 抛出 IOException

于 2013-10-11T12:32:49.477 回答
0

首先,您没有构造函数,这对于实例化您的类至关重要。把它放在你的类声明中:

public TrueSprite() {
    //code to run when your class is instantiated.
}

现在,这不会做任何特别的事情,但是您可以使用以下命令调用它:

TrueSprite sprite = new TrueSprite();

此外,您的班级需要清理。尝试将您的赋值代码放在我们刚刚构建的构造函数中,并将这些变量的声明放在它之外:

@SuppressWarnings("unused")
public class TrueSprite
{
    private final int width;
    private final int height;
    private final int rows;
    private final int cols;
    private BufferedImage bigImg;
    private BufferedImage[] sprites;

    public TrueSprite(int width, int height, int rows, int columns) {
        this.width = width;
        this.height = height;
        this.rows = rows;
        this.cols = columns;
        this.bigImg = ImageIO.read(new File("robin.png"));
        this.sprites = new BufferedImage[rows * cols];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                sprites[(i * cols) + j] = bigImg.getSubimage(j * width, i * height, width, height);    
            }
        }
    }

    public void paint(Graphics g)
    {
        g.drawImage(sprites[1], 100, 100, null);
    }
}

只需确保将四个有效参数传递给TrueSprite构造函数即可正确调用它:

TrueSprite sprite = new TrueSprite(200, 500, 20, 50);
于 2013-10-11T12:34:46.687 回答
0

BufferedImage[] sprites = new BufferedImage[rows * columns];在构造函数外部初始化了大小为 5*5=25 的 sprites 数组。如果您创建了具有更高行或列的 TrueSprit,例如TrueSprite(100,100,100,100),sprites[24] 之后的条目将不会被实例化。

您应该始终将初始化放在构造函数中。IE

private BufferedImage[] sprites;

public TrueSprite(int width, int height, int rows, int columns) throws IOException
{
    this.width = width;
    this.height = height;
    this.rows = rows;
    this.columns = columns;
    this.sprites = new BufferedImage[rows * columns];

    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < columns; j++)
        {
            sprites[(i * columns) + j ] = spriteSheet.getSubimage(i * width, j * height, width, height);
        }
    }
}

这将在使用值创建数组之前将行和列设置为正确的值。此外,如果您想拥有默认值,您应该使用默认构造函数。IE

public TrueSprite() throws IOException
{
    this.width = 240;
    this.height = 314;
    this.rows = 5;
    this.columns = 5;
    this.sprites = new BufferedImage[rows * columns];

    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < columns; j++)
        {
            sprites[(i * columns) + j ] = spriteSheet.getSubimage(i * width, j * height, width, height);
        }
    }
}
于 2013-10-11T12:45:04.200 回答