This worked for me at a time i needed to create a cursor for a touch screen, that faded out when the screen were no longer being touched.
It wasn't done in Slick 2D but Java 2D, but it seems to work fine here too.
Code:
private int timer;
private int timerLast = 1500;
boolean fade;
public void update(GameContainer gc, int delta) throws SlickException {
if (fade) {
timer += delta;
if (timer > timerLast) {
fade = !fade;
}
} else {
timer -= delta;
if (timer < 0) {
fade = !fade;
}
}
}
public void render(GameContainer gc, Graphics g) throws SlickException {
float timerPercent = (float) timer / timerLast;
int alphaPercent = (int) (255 * timerPercent);
g.setColor(new Color(255, 255, 255, alphaPercent));
g.drawString("test", 20, 30);
}
Edit: added code to illustrate fading in and out.