0

I have a background for my title screen of my program, and want the colour of it to slowly change over time. This is how the background's drawn:

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

So at the moment, the background's red. I want it to slowly fade to green, then blue, and then back to red. I have tried this:

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);

I did have more code to make it so if any of the values reached 0, they would be added to and if they reached 255 they would be subtracted, but you get the idea. And this was in a render method which was called 60 times a second. (the timer variable was created outside of the render method)

Thanks!

sparklyllama
  • 1,276
  • 4
  • 20
  • 28

2 Answers2

0

Use a swing Timer to set the new background colour perodically. For calculating the new colour you can use Color.HSBtoRGB(), changing the hue component each time the timer is activated.

kiheru
  • 6,588
  • 25
  • 31
0

As suggested by kiheru you should use Timer .

Here is an example.

when you run this it will change panel background color every second (I am using random color)

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);
    }
}
Makky
  • 17,117
  • 17
  • 63
  • 86
  • While technically correct, this answer violates the single thread rules of Swing. Take a read through [The Event Dispatching](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html) for more details – MadProgrammer Jul 15 '13 at 11:14