I have a class called “Trail” in my game. In it I want to slowly make a texture more transparent to simulate a fade out. But using AlphaComposite doesn’t work and the obect just doesn’t gets rendered!
private float alpha = 1;
private float fade = .05f;
public void update(){
alpha -= fade;
}
public void render(java.awt.Graphics2D graphics) {
if (alpha > 0) {
graphics.setComposite(makeTransparent(alpha));
graphics.drawImage(image,x,y,null);
graphics.setComposite(makeTransparent(1));
}
}
private AlphaComposite makeTransparent(float alpha) {
return AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
}
If I use this code with something like graphics.fillRect(…) it works perfectly, but it just doesn’t want to work with a BufferedImage!
PS: The update method get’s called 60 times a second and the render method as often as the CPU allows it. I’m sure that it’s a problem with this code snippet.