Search code examples
javamultithreadingswingjframesetbackground

JFrame flashes when changes the background color


I'm making a window who changes the background color every 750 milliseconds... but the JFrame flashes like this...

error

I want something like this.....

enter image description here

I found some solutions like these:

1.-Frame.getContentPane().setBackground(Color);
2.-make a new Thread
3.-Frame.getContentPane().repaint();

but it doesn't work

my code ...

Thread ciclo=new Thread(new Runnable() {

        float c=1f;
        @Override
        public void run() {
            while(true){
            Frame.getContentPane().setBackground(Color.getHSBColor((c/360), 1, 1));
            Frame.getContentPane().repaint();                
            c=(c>=360)?1:c+5;
            try{Thread.sleep(750);}catch(Exception e){}
            }
        }
    });
    ciclo.start();

how can I fix this? thanks for your advices


Solution

  • You are violating Swing Threading rules by trying to change Swing state in a background thread.. Use a Swing Timer as this will likely solve your problem.

    new Timer(750, new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        contentPane.setBackground((Color.getHSBColor((c/360), 1, 1));
        contentPane.repaint();
        c= (c >= 360) ? 1 : c + 5;
      }
    }).start();
    

    Edit or better:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class BackgroundColorChange extends JPanel {
       private static final int PREF_W = 400;
       private static final int PREF_H = PREF_W;
       private static final int TIMER_DELAY = 75;
    
       public BackgroundColorChange() {
          new Timer(TIMER_DELAY, new ActionListener() {
             private int c = 1;
    
             @Override
             public void actionPerformed(ActionEvent arg0) {
                setBackground(Color.getHSBColor((float) c / 360, 1f, 1f));
                repaint();
                c = (c >= 360) ? 1 : c + 5;
             }
          }).start();
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       private static void createAndShowGui() {
          BackgroundColorChange mainPanel = new BackgroundColorChange();
    
          JFrame frame = new JFrame("BackgroundColorChange");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }