[Slick2D] Level Bar Algorithm in Slick2D

Here is my code:


public void draw(Graphics g) {
	g.setColor(Color.gray);
	g.fillRect(x, GuiController.guiY, 200, 32);
	g.setColor(Color.black);
	g.drawRect(x, GuiController.guiY, 200, 32);
	g.setColor(Color.red);
	g.fillRect(x, GuiController.guiY, (PlayerStatController.xp / PlayerStatController.xpToGet) * 200, 32);
}

PlayerStatController is a class with static player variables and GuiController is basically a simple ArrayList class thing.

Anyways, when I run this code, theXP is increased, but the bar never changes. Therefore, you can’t see the change in your XP.

I’ve already looked a few tutorials and the code looked something similar to this:


int barWidth = 200;
int max = 100;
int current = //Current health
g.fillRect(x, y, (current / max) * 200, barHeight);

I tried to incorporate this as best as I could into my game. I think I put it in correctly, but it still isn’t working. So, I turned to you awesome guys.

it could be the problem with using integer division when youre trying to get a number between 0 and 1…
try casting things as float when you need some precision.

g.fillRect(x, y, (float)(current / max) * 200, barHeight);

It worked! Thank you so much. I was getting so worked up over this. Turns out it wasn’t some major error. Just a lack of understanding of the language. :stuck_out_tongue: Anyways, thanks Nitro.