Declare a global variable and call it fading
.
Color fading = Color.white;
In your timer's body:
if(fading.getRed()>0){
fading = new Color(fading.getRed()-1,fading.getGreen()-1,fading.getBlue()-1);
//do whatever you have to do with your fading color
}
Everytime your timer executes this code it will decrease the RGB value from white to black.
The result:
LOOP NO | COLOR
1: R=255, G=255, B=255
2: R=254, G=254, B=254
3: R=253, G=253, B=253
4: R=252, G=252, B=252
5: R=251, G=251, B=251
6: R=250, G=250, B=250
.
.
.
255: R=0, G=0, B=0
EDIT
Example:
import java.awt.Color;
public Class YourClass{
Color fading = Color.white;
public YourClass(){
TimerTask task = new TimerTask() {
@Override
public void run() {
if (fading.getRed() > 0) {
fading = new Color(fading.getRed() - 1, fading.getGreen() - 1, fading.getBlue() - 1);
//do other stuff here
}
}
};
Timer timer = new Timer();
timer.scheduleAtFixedRate(task, 0, 100);
}
}