title: JPanel background image not filling panel after resize
body: <p>I am trying to make the background image resize to always fit the JPanel in which it resides. When the program is ran it looks purrfect:</p>
  
  <p><img src="https://i.stack.imgur.com/vZJRj.png" alt="enter image description here"></p>
  
  <p>The problem I'm having is that after I resize the window it looks like so:</p>
  
  <p><img src="https://i.stack.imgur.com/Ea6zO.png" alt="enter image description here"></p>
  
  <p>Obviously, the image is resizing but it isn't being fully drawn for some reason. This is part of a much larger application, but I have isolated the smallest subset of code that creates the problem:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import java.awt.geom.AffineTransform;
  import java.awt.image.*;
  import java.io.File;
  import javax.imageio.ImageIO;
  import javax.swing.*;
  
  public class ScaleTest extends JPanel {
  static JFrame window;
  static JPanel mainPanel;
  
  BufferedImage originalImage;
  BufferedImage scaledImage;
  
  ScaleTest() {
      setPreferredSize(new Dimension(320, 200));
      try {
          originalImage = ImageIO.read(new File("cat.jpg"));
      } catch(Exception e){}
  
      addComponentListener(new ResizerListener());
  }
  
  public void resize() {
      double widthScaleFactor = getWidth() / (double)originalImage.getWidth();
      double heightScaleFactor = getHeight() / (double)originalImage.getHeight();
      double scaleFactor = (widthScaleFactor &gt; heightScaleFactor)? heightScaleFactor : widthScaleFactor;
  
      AffineTransform at = new AffineTransform();
      at.scale(scaleFactor, scaleFactor);
  
      AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
      scaledImage = scaleOp.filter(originalImage, scaledImage);
  
      repaint();
  }
  
  @Override
  public void paintComponent(Graphics g) {
      super.paintComponent(g);
  
      g.drawImage(scaledImage, 0, 0, null);
  }
  
  public static void main(String[] args) {
      javax.swing.SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run(){
              window = new JFrame("Scale Test");
              window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  
              mainPanel = new ScaleTest();
              window.getContentPane().add(mainPanel);
  
              window.pack();
              window.setLocationRelativeTo(null);  // positions window in center of screen
              window.setVisible(true);
          }
      });
  }
  
  class ResizerListener implements ComponentListener {
      @Override
      public void componentResized(ComponentEvent e) {
          resize();
      }
  
      @Override public void componentHidden(ComponentEvent e) {}
      @Override public void componentMoved(ComponentEvent e) {}
      @Override public void componentShown(ComponentEvent e) {}
  }
  }
  </code></pre>
  
  <p>How can I make the image draw and fill the entire JPanel?</p>