title: Use Paint Component to fit image in JFrame
body: <p>My java code below uses a panel to display a button and image. The problem is the image is not scaling to fit in the screen. Meaning the image in full is not being display only a part of the image is being displayed. I am pretty sure you have to use paintComponent to do this. </p>
  
  <p><a href="https://i.stack.imgur.com/4Qsro.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4Qsro.png" alt="Pic"></a></p>
  
  <pre><code>import java.awt.*;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.io.IOException;
  import javax.imageio.ImageIO;
  import javax.swing.*; 
  
  public class frame {  
      JFrame f;  
  
      frame() throws IOException{  
          f = new JFrame();  
          JButton b1 = new JButton("NORTH");;  
          JLabel b2 = new JLabel("SOUTH");;  
          ImageIcon img = new ImageIcon("a.png");
          b2 = new JLabel("", img, JLabel.CENTER);
          f.add(b1,BorderLayout.NORTH);  
          f.add(b2,BorderLayout.CENTER);  
          f.setSize(300,300);  
          f.setVisible(true);  
      } 
  
      public static void main(String[] args) throws IOException {  
          new frame();  
      } 
  
      protected void paintComponent(Graphics g) {
          super.paintComponent(g);
      }
  }  
  </code></pre>