0

I want to make background changing on a ContentPane over some period of time.

I have set default bg color in class

getContentPane().setBackground(Color.white);                

and then on ButtonActionPerformed I want user to choose another color and then if not null the background should change to that color (the default one and the chosen one)

 Color bgC = JColorChooser.showDialog(null, "Choose color: ", Color.yellow);
    if (bgC != null)

    {
    Timer bT = new Timer (TIMER_DELAY, new ActionListener() {

     @Override
    public void actionPerformed(ActionEvent e) {
    getContentPane().setBackground(bgC);
    }
    });

    bT.start();

At the moment this works but it only changes the default color to new chosen color in time specified.

Hintham
  • 1,078
  • 10
  • 29
mth
  • 3
  • 1
  • 3

2 Answers2

0

You can use the following code:

import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.function.Consumer;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.Timer;

public class Main {
    public static class Blinker {
        private Color color1;
        private Timer timer;
        private Consumer<Color> setColor;
        private int timesBlinked;

        public Blinker(int delay, int blinkTimes, Color color1, Color color2, Consumer<Color> setColor) {
            this.color1 = color1;
            this.setColor = setColor;
            timesBlinked = 0;
            timer = new Timer(delay, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (timesBlinked < blinkTimes) {
                        if (timesBlinked % 2 == 0)
                            setColor.accept(color2);
                        else
                            setColor.accept(color1);
                        ++timesBlinked;
                    }
                    else
                        stop();
                }
            });
        }

        private void stop() {
            timer.stop();
        }

        public void start() {
            setColor.accept(color1);
            timer.start();
        }
    }

    public static void startBlink(int delay, int blinkTimes, Color color1, Color color2, Consumer<Color> setColor) {
        new Blinker(delay, blinkTimes, color1, color2, setColor).start();
    }

    public static class YourJFrame extends JFrame {
        public YourJFrame(String title) {
            super(title);

            JButton button = new JButton("Choose color");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Color bgC = JColorChooser.showDialog(null, "Choose color: ", Color.YELLOW);
                    if (bgC != null)
                        startBlink(500, 7, getContentPane().getBackground(), bgC, c -> getContentPane().setBackground(c)); //Change the number arguments at will.
                }
            });

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            getContentPane().setLayout(new GridBagLayout()); //Just to keep the button centered.
            getContentPane().add(button);
            getContentPane().setBackground(Color.WHITE);
            pack();
            setLocationRelativeTo(null);
            setVisible(true);
        }
    }

    public static void main(final String[] args) {
        new YourJFrame("Blinker frame").setSize(250, 250);
    }
}

The delay argument of startBlink method determines the speed of the blinking.
The lower the number, the greater the speed.

The blinkTimes argument of startBlink method determines the number of times the colors will be blinked (alternated).
Remember to keep this an odd number of times, if you want the color to be finaly changed.

You may just copy the class Blinker and the method startBlink to your code to get started.

gthanop
  • 3,035
  • 2
  • 10
  • 27
0

Something like:

Timer bT = new Timer (TIMER_DELAY, new ActionListener() {
    static final int[] startRGB = { 255, 255, 255 };
    int[] endRGB = { bg.getRed(), bg.getGreen(), bg.getBlue() };
    int pct = 0;

    @Override
    public void actionPerformed(ActionEvent e) {
        ++pct;

        int[] rgb = new int[3];
        for (int i = 0; i < 3; ++i) {
            rgb[i] = (pct*startRGB[i] + (100 - pct)*endRGB[]i]) / 100;
        }
        getContentPane().setBackground(new Color(rgb[0], rgb[1], rgb[2]));
        repaint(50L);
        if (pct == 100) {
            stop();
        }
    }
});

Here 100 steps, using a percentage pct and step size 1.

Timer should have setRepeats(true).

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138