任何人都可以帮助组件逐渐变色。我希望改变颜色
new Color(255,0,0)
到
new Color(0,0,0)
尝试使用定时器的不同变体,但卡住了。顺便说一句,用计时器做这件事是正确的吗?
提前致谢!
答案 0 :(得分:4)
这些方面的东西:
Color before = new Color(255,0,0);
Color after = new Color(0,0,0);
Color current = before;
int maxtime = 1000;
int step = 0;
public void step()
{
if(step == maxtime)
return;
step++;
double percentComplete = step/maxtime;
double percentGone = 1-percentComplete;
int red = (int)(before.getRed()*percentGone+after.getRed()*percentComplete);
int green = (int)(before.getGreen()*percentGone+after.getGreen()*percentComplete);
int blue = (int)(before.getBlue()*percentGone+after.getBlue()*percentComplete);
int alpha = (int)(before.getAlpha()*percentGone+after.getAlpha()*percentComplete);
current = new Color(red, green, blue, alpha);
//set component's color to current
}
注意,您可能需要进行一些检查以确保红绿和蓝不超过255,但我还不确定。
(这个函数应该属于你正在改变颜色的组件,你应该在每个计时器滴答时调用它)
答案 1 :(得分:4)
请考虑使用JLayer替代您的任务: - )