【问题标题】:java change control's color graduallyjava逐渐改变控件的颜色
【发布时间】:2012-05-11 08:56:50
【问题描述】:

任何人都可以帮助组件渐变颜色变化。我想改变颜色

new Color(255,0,0)  

new Color(0,0,0)  

尝试了不同的计时器变体,但卡住了。顺便说一句,用计时器来做这件事是正确的方法吗?

提前致谢!

【问题讨论】:

  • 你没听说过JLayer 吗? :-)
  • @nIcEcOw 由于 OP 计划采纳您的建议(从他对答案的评论中阅读),很高兴将其发布为答案 :-)
  • @Sanjay:我想写一个字吗?我本可以将其作为答案,尽管@@PerryMonschau 在他的回答中做了一些艰苦的工作,而一个字的答案是我的意识不允许我这样做的。所以我很困惑:(但是在给定的行中也可以提供一个示例的人肯定可以将其发布为答案。
  • @nIcEcOw 除了意识,考虑一下这个问题的未来访客,他们会错过一个好的答案:)

标签: java swing colors


【解决方案1】:

请考虑使用JLayer 作为您的任务的替代方案:-)

【讨论】:

    【解决方案2】:

    类似的东西:

    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,但我还不确定。

    (这个函数应该属于你要改变颜色的组件,你应该在每个计时器滴答声中调用它)

    【讨论】:

    • 感谢您的意见,但我想我会按照@nIcE cOw 的建议使用 Jlayer。
    • :D 我也不知道。
    • 这样的计算在 HSB 颜色模型中可能更容易,如 here 所示。
    猜你喜欢
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多