无法在Slick2D中将.png图像加载为矩形(在Java中)

时间:2019-02-15 14:51:10

标签: java slick slick2d

我正在尝试将“ grass.png”图像添加到矩形(它是图块精灵)上,以便该矩形具有纹理而不是纯绿色。

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException 
{
        renderBackground(g);
}

private void renderBackground(Graphics g) throws SlickException 
{
        //Attempt to load textures to the rectangle
        Shape rectangle = null;
        Image groundTexture = new Image("res/grassMid.png");
        g.texture(rectangle, groundTexture);
        g.draw(rectangle);
}

它给出错误java.lang.NullPointerException。我可以知道我在哪里做错了吗?

1 个答案:

答案 0 :(得分:0)

我发现仅使用fillRect的方法:

    Shape rectangle = null;
    Image groundTexture = new Image("res/grassMid.png");

    //parameters are x position, y position, width, height, image, offsetX, offsetY
    g.fillRect(0.0f, Window.HEIGHT * 5.3f / 6.0f, (float) Window.WIDTH, Window.HEIGHT / 8.0f, groundTexture, 50, 0);