title: Java Fade In and Out two JPanels at the same time
body: <p>I have a list of JPanels that I want to display as a "slideshow" where one JPanel fades out and the next JPanel in the list fades in. This is the code I am fiddling with:</p>
  
  <pre><code>  public float opacity = 0f;
    private Timer fadeTimer;
    private boolean out;
  
    public void fadeIn()
    {
      out = false;
      beginFade();
    }
  
    public void fadeOut ()
    {
      out = true;
      beginFade();
    }
  
    private void beginFade()
    {
      fadeTimer =
        new javax.swing.Timer(75,this);
      fadeTimer.setInitialDelay(0);
      fadeTimer.start();
    }
  
    public void actionPerformed(ActionEvent e)
    {
      if (out)
      {
        opacity -= .03;
        if(opacity &lt; 0)
        {
          opacity = 0;
          fadeTimer.stop();
          fadeTimer = null;
        }
      }
      else
      {
        opacity += .03;
        if(opacity &gt; 1)
        {
          opacity = 1;
          fadeTimer.stop();
          fadeTimer = null;
        }
      }
  
  
      repaint();
    }
    public void paintComponent(Graphics g)
    {
      ((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
      g.setColor(getBackground());
      g.fillRect(0,0,getWidth(),getHeight());
    }
  </code></pre>
  
  <p>The problem is that it fades some times, and other times it does not, and other times the transition is very laggy. What I would prefer is that there is a fraction of a second where the screen goes white, between when one JPanel fades out and the next JPanel fades in. Does anyone know how I can solve this? Thanks in advance.</p>