I'm creating a healthbar with 2 rectangles overlapping each other. The inner shows the current health, the outer shows the total health.
Problem
The healthbar is blurry.
What I've got so far
I've read about e. g. having to add 0.5, but it only solves the problem partially.
A screenshot, top image is without the 0.5, bottom image is with the 0.5 added.
Here's the code:
private class HealthBar extends Pane {
Rectangle outerHealthRect;
Rectangle innerHealthRect;
public HealthBar() {
double height = 10;
double outerWidth = 60;
double innerWidth = 40;
// using 0.5, otherwise it would be blurry
double x=snap( 0.0);
double y=snap( 0.0);
outerHealthRect = new Rectangle( x, y, outerWidth, height);
outerHealthRect.setStroke(Color.BLACK);
outerHealthRect.setFill(Color.WHITE);
innerHealthRect = new Rectangle( x, y, innerWidth, height);
innerHealthRect.setStroke(Color.TRANSPARENT);
innerHealthRect.setFill(Color.LIMEGREEN);
getChildren().addAll( outerHealthRect);
getChildren().addAll( innerHealthRect);
}
public void setValue( double value) {
innerHealthRect.setWidth( snap( outerHealthRect.getWidth() * value));
}
private double snap( double value) {
return (int) value + 0.5;
}
}
After I applied scaling for debug purposes it became obvious that the transparent stroke isn't considered as transparent line of the same width like a stroke with color:
Questions
- Is there an option to snap to pixels, i. e. have sharp pixels in general without having to mess around with 0.5?
- How can I make the transparent stroke the same width as the outer stroke?
I could try a different approach with a canvas, but these are general questions which affect the way you have to set up a JavaFX application.
Thank you very much.