Search code examples
javaprogress-barjprogressbar

Progress Bar as Health Bar


Possible Duplicate:
Progress Bar used as Health Bar

Refer to: Progress Bar used as Health Bar

When I read the JProgressBar api, it show the maximum as the end of a task. I want the maximum to be a player's health, such as 90/100. How could I be able to do that?

Basically, I want the bar to display 90/100 when the player's health is 90.

I tried:

JProgressBar health = new JProgressBar(0, 50);
health.setStringPainted(true);
health.setForeground(Color.GREEN);
// ???

Solution

  • You can define your own string to display in the bar, using the setString method.

    You set the value in the progress monitor with the method setValue.

    Should be straightforward to make a string that displays a percentage, based on the range of possible health values and the current value.

    If you want to reverse the right-left orientation, and the api doesn't let you make left-to-right be descending, you can make the progress value be = TotalPossibleHealth - ActualHealth, rather than ActualHealth.

    So, it should be possible to either have the left or the right be the indicator of total health or total loss of health.