0

Hello i was wondering of how to change the background color of a JFrame dynamically as in this Website when you click on the button Ok, Let's Go and then select any option, the background change dynamically, i would like to do it, but using a JFrame. I was thinking on create a cicle to do it. If someone do knows how to do it, i'll be thankful

This is what i want: enter image description here

Jonathan Solorzano
  • 6,812
  • 20
  • 70
  • 131

3 Answers3

2
YourJFrame.getContentPane().setBackground(Color.colorName);

Where "YourJFrame" is the name of your component and "colorName" is the name of one of the colors available in the Color class.

g.carvalho97
  • 332
  • 1
  • 9
  • I know how to change the background color, what i don't know is how to change it dynamically, as in the gif example – Jonathan Solorzano Jul 12 '14 at 16:53
  • I don't think that Java can do that alone. Check JavaFX or other libraries, maybe there's something there related to your problem. Also, be sure to check: http://stackoverflow.com/questions/21270610/java-smooth-color-transition and http://docs.oracle.com/javafx/2/api/javafx/animation/Transition.html – g.carvalho97 Jul 12 '14 at 17:04
2

Could you please make an example?

Below is an example switching from blue to green and green to blue. There's bug with the first click to green. And it's probably not the most robust code, but It's just an example that I'm too lazy to improve on right now. You can play with it

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class ChangeColor {

    public ChangeColor() {
        JFrame frame = new JFrame();
        frame.add(new ColorPanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public class ColorPanel extends JPanel {

        private static final int DELAY = 30;
        private static final int INCREMENT = 5;
        private Color currentColor = Color.BLUE;
        boolean isBlue = true;
        boolean isGreen = false;
        private int r,g,b;

        private Timer timer = null;
        private JButton greenButton = null;
        private JButton blueButton = null;

        public ColorPanel() {
            r = 0; g = 0; b = 255;

            greenButton = createGreenButton();
            blueButton = createBlueButton();

            timer = new Timer(DELAY, new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    if (isBlue) {
                        if (b == 0) {
                            stopTimer();
                            enableButtons();
                        } else {
                            blueToGreen();
                            setColor(new Color(r, b, g));
                        }
                    } 

                    if (isGreen) {
                        if (g == 0) {
                            stopTimer();
                            enableButtons();
                        } else {
                            greenToBlue();
                            setColor(new Color(r, b, g));
                        }
                    }

                    repaint();
                }
            });

            add(blueButton);
            add(greenButton);
        }

        public JButton createBlueButton() {
            JButton button = new JButton("BLUE");
            button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    if (currentColor != new Color(0, 255, 0)) {
                        System.out.println("turn blue");
                        isBlue = true;
                        isGreen = false;
                        diableButtons();
                        startTimer();   
                    }
                }
            });
            return button;
        }

        public void diableButtons() {
            blueButton.setEnabled(false);
            greenButton.setEnabled(false);
        }

        public void enableButtons() {
            blueButton.setEnabled(true);
            greenButton.setEnabled(true);
        }

        public JButton createGreenButton() {
            JButton button = new JButton("GREEN");
            button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    if (currentColor != new Color(0, 0, 255)) {
                        System.out.println("turn green");
                        isGreen = true;
                        isBlue = false;
                        diableButtons();
                        startTimer();

                    }
                }
            });
            return button;
        }

        private void blueToGreen() {
            b -= INCREMENT;
            g += INCREMENT;
        }

        private void greenToBlue() {
            g -= INCREMENT;
            b += INCREMENT;
        }



        public void setColor(Color color) {
            this.currentColor = color;
        }

        public void startTimer() {
            timer.start();
        }

        public void stopTimer() {
            timer.stop();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(currentColor);
            g.fillRect(0, 0, getWidth(), getHeight());
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new ChangeColor();
            }
        });

    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Hey, really thanks, but could you please explain me how to know what r,g,b int values should i use for other colours? – Jonathan Solorzano Jul 12 '14 at 18:11
  • This is probably the simplest solution as the colors I chose are strictly 255. but with other colors, it gets more complicated. You're going to need to do some more logic. I don't really have the time to figure it out, but basically you have to figure out a stopping point based of the calculation. The way I'm doing it is probably not the best solution for it. The timer code will get very verbose. So you need to figure out the calculations and figure out how the increment each change while ensuring there is a stopping point for the change. – Paul Samsotha Jul 12 '14 at 18:17
0

Add this to the actionListeners of your buttons JFrame.getContentPane().setBackground(Color.colorName);

And change the colours at will.

Sociopath
  • 13,068
  • 19
  • 47
  • 75