【问题标题】:Change RGB Values of Colour Over Time随时间改变颜色的 RGB 值
【发布时间】:2013-07-13 04:36:04
【问题描述】:

我的程序标题屏幕有一个背景,并且希望它的颜色随着时间的推移而慢慢变化。这就是背景的绘制方式:

g.setColor(255, 0, 0);
g.fillRect(0, 0, 640, 480); //g is the Graphics Object

所以此刻,背景是红色的。我希望它慢慢变绿,然后变蓝,然后变回红色。我试过这个:

int red = 255;
int green = 0;
int blue = 0;

long timer = System.nanoTime();
long elapsed = (System.nanoTime() - timer) / 1000000;

if(elapsed > 100) {
   red--;
   green++;
   blue++;
}

g.setColor(red, green, blue);
g.fillRect(0, 0, 640, 480);

我确实有更多的代码来制作它,所以如果任何值达到 0,它们将被添加,如果它们达到 255,它们将被减去,但你明白了。这是在一个每秒调用 60 次的渲染方法中。 (定时器变量是在渲染方法之外创建的)

谢谢!

【问题讨论】:

  • 您是否尝试过任何方法来实现这一目标?
  • @Makky 我刚刚添加了。
  • 那不行吗?
  • 看看this,它有一个混合颜色的算法,可以用来动画颜色变化

标签: java colors


【解决方案1】:

使用swing Timer 来定期设置新的背景颜色。要计算新颜色,您可以使用Color.HSBtoRGB(),每次激活计时器时更改色调组件。

【讨论】:

    【解决方案2】:

    按照 kiheru 的建议,您应该使用 Timer 。

    这是一个例子。

    当你运行它时,它会每秒改变面板背景颜色(我使用的是随机颜色)

    import java.awt.Color;
    import java.awt.EventQueue;
    import java.util.Date;
    import java.util.Random;
    import java.util.Timer;
    import java.util.TimerTask;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class TestColor {
    
        private JFrame frame;
        private JPanel panel;
        private Timer timer;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        TestColor window = new TestColor();
                        window.frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the application.
         */
        public TestColor() {
            initialize();
        }
    
        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
    
            frame = new JFrame();
            frame.setBounds(100, 100, 450, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(null);
    
            panel = new JPanel();
            panel.setBounds(23, 61, 354, 144);
            frame.getContentPane().add(panel);
            timer = new Timer();
            TimerClass claa = new TimerClass();
            timer.scheduleAtFixedRate(claa, new Date(), 1000);
        }
    
        private class TimerClass extends TimerTask {
    
            @Override
            public void run() {
    
                panel.setBackground(randomColor());
    
            }
    
        }
    
        public Color randomColor() {
            Random random = new Random(); // Probably really put this somewhere
                                            // where it gets executed only once
            int red = random.nextInt(256);
            int green = random.nextInt(256);
            int blue = random.nextInt(256);
            return new Color(red, green, blue);
        }
    }
    

    【讨论】:

    • 虽然技术上是正确的,但这个答案违反了 Swing 的单线程规则。阅读The Event Dispatching了解更多详情
    猜你喜欢
    • 2022-07-11
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    相关资源
    最近更新 更多