1

I am trying to render a health bar with Slick. I want a red rectangle with a green one in front of it to show the health remaining. This is what I have so far:

        Rectangle healthBG = new Rectangle(healthX, healthY, healthWidth, healthHeight);
        ShapeFill healthFill = new GradientFill(0, healthHeight / 2, Color.red, healthWidth, healthHeight - 1, Color.orange, true);

        float healthGAWidth = ((float) health / (float) maxHealth) * (float) healthWidth;
        Rectangle healthGA = new Rectangle(healthX, healthY, healthGAWidth, healthHeight);
        ShapeFill healthGAFill = new GradientFill(0, healthHeight / 2, Color.green, healthGAWidth, healthHeight - 1, Color.green, true);

        //g.setColor(Color.red);
        g.draw(healthBG, healthFill);
        //g.setColor(Color.green);
        g.draw(healthGA, healthGAFill);

This is what is rendered on the screen:

Screenshot of health bar in action

4

1 回答 1

2

The problem is that you are using the wrong rendering method. In Graphics there are 2 types of methods draw() and fill(). Your rendering line should be:

g.fill(healthBG, healthFill);

Alternatively you could skip making a Rectangle or ShapeFill, because Graphics has a method for filling rectangles:

float healthGAWidth = ((float) health / (float) maxHealth) * (float) healthWidth;
g.fillRect(healthX, healthY, healthWidth, healthHeight);
g.fillRect(healthX, healthY, healthGAWidth, healthHeight);
于 2012-07-26T19:54:52.970 回答