0

I'm trying to create a health bar using java.awt but the width of the bar doesn't display. The first line of code works fine and creates a rectangle as expected, but the second doesn't draw anything. It doesn't throw an error either. The only difference between the working rect and the non working rect is the width parameter.

Health is an int and 400 * (health/5) always produces an int.

I've tried creating a variable that calculates this and putting it in place of the calc in the fillRect but it makes no difference. Someone please explain why this doesn't work!

g.fillRect(WIDTH/4, HEIGHT - 80, 400, 40);
g.setColor(Color.red);
g.fillRect(WIDTH/4,HEIGHT - 80, 400 * (health/5), 40);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Without any more evidence... health/5 is equal to zero. Maybe multiply by 400 first, then divide by 5 if health is less than 5. – matt Apr 15 '20 at 15:35
  • That's it -- what @matt suggested: `(400 * health) / 5` is what you need since you're doing int division (see the duplicate) and getting an int result. – Hovercraft Full Of Eels Apr 15 '20 at 15:37

0 Answers0