无法实例化

时间:2013-10-11 12:22:26

标签: java sprite

我一直在尝试创建一个用于我们的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)

3 个答案:

答案 0 :(得分:0)

我认为你需要创建一个像

这样的非参数构造函数

public TrueSprite()抛出IOException

答案 1 :(得分: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);

答案 2 :(得分:0)

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

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

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

在使用值创建数组之前,这会将行和列设置为正确的值。此外,如果要使用默认值,则应使用默认构造函数。即。

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