0

So I have made a health bar that changes colour depending on the health of the individual. But I wanna make it so that when the player walks under the health bar, it turns it translucent and I am wondering how do I do that if the health bar is made out of HSB colour?

private static float HEALTH = 100;

public void render(Graphics g) {
    g.setColor(Color.GRAY);
    g.fillRect(15, 15, 200, 32);

    color = Color.getHSBColor( (1f * HEALTH) / 360, 1f, 1f);

    g.setColor(Color.getHSBColor( (1f * HEALTH) / 360, 1f, 1f));
    g.fillRect(15, 15, (int) HEALTH * 2, 32); 

    g.setColor(Color.white);
    g.drawRect(15, 15, 200, 32);
}

1 Answers1

1

After obtaining the Color object from Color.getHSBColor(...) you could create a second Color object with the same RGB values but a different Alpha value. The Alpha value is what defines transparency. For example:

color = Color.getHSBColor( (1f * HEALTH) / 360, 1f, 1f);
color = new Color( color.getRed(), color.getBlue(), color.getGreen(), 128 );
TrogDor
  • 984
  • 6
  • 14