title: BackGroundImage scaling
body: <p>This is my code for a program which has a background image and takes components on it:</p>
  
  <p>1.FirstWindow class:</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.Color;
  import java.awt.Component;
  import java.awt.Dimension;
  import java.awt.Font;
  import java.awt.GridLayout;
  import java.awt.Image;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import javax.imageio.ImageIO;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JOptionPane;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.JTextField;
  
  
  public class FirstWindow {
     private int option;
     private int numberOfNames;
     private String options[] = {"OK","Cancel"};
     private JPanel aPanel;
  
     public FirstWindow(){
        JPanel panel = new JPanel();
        JLabel lbl = new JLabel("Give the number of names:");
        JTextField text = new JTextField(10);
        panel.add(lbl);
        panel.add(text);
        option = JOptionPane.showOptionDialog(null,panel,"Number of Names",JOptionPane.NO_OPTION,JOptionPane.QUESTION_MESSAGE,null, options,options[0]);
        if (option == 0){
            String number = text.getText();
            if (number == null || number.equals("")){
                JOptionPane.showMessageDialog(null, "No input given.\nProgram will terminate.","No Input Error",JOptionPane.ERROR_MESSAGE);
                System.exit(0);
            }
            try{
                numberOfNames = Integer.parseInt(number);
                if (numberOfNames&gt;=0 &amp;&amp; numberOfNames&lt;4){
                    JOptionPane.showMessageDialog(null, "No reason for holding a draw.\nProgram will terminate.",JOptionPane.PLAIN_MESSAGE);
                    System.exit(0);
                }
                else if (numberOfNames&gt;=4){
                    //frame
                    JFrame firstWindowFrame = new JFrame();
                    firstWindowFrame.setPreferredSize(new Dimension(600,600));
                    firstWindowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    firstWindowFrame.setResizable(true);
                    firstWindowFrame.setLayout(new BorderLayout());
                    firstWindowFrame.setVisible(true);
                    Image image = ImageIO.read(new File("/home/george/Desktop/chl.jpg"));
                    BackGroundPanel bp = new BackGroundPanel(image);
                    firstWindowFrame.add(bp,BorderLayout.CENTER);
                    bp.setLayout(new BorderLayout());
                    firstWindowFrame.add(new JScrollPane(bp),BorderLayout.CENTER);
                    JPanel aPanel = new JPanel();
                    aPanel.setOpaque(false);
                    bp.add(aPanel,BorderLayout.CENTER);
                    aPanel.setLayout(new GridLayout(0,2));
                    for (int i=0;i&lt;150;i++){
                        aPanel.add(new JLabel("NAME"));
                    }
  
                    Component array[][] = new Component[numberOfNames][2];
                    for (int i=0;i&lt;array.length;i++){
                        array[i][0] = new JLabel("Name"+(i+1)+":");
                        array[i][1] = new JTextField(10);
                    }
                    for (int i=0;i&lt;array.length;i++){
                        array[i][0].setFont(new Font(array[i][0].getFont().getFontName(),Font.BOLD,array[i][0].getFont().getSize()+5));
                        array[i][0].setForeground(Color.WHITE);
                    }
                    firstWindowFrame.pack();
                }
                else if (numberOfNames&lt;0){
                    JOptionPane.showMessageDialog(null, "Invalid Number Given.\nProgram will terminate","Negative Number Error",JOptionPane.ERROR_MESSAGE);
                    System.exit(0);
                }
  
  
            }
            catch(Exception invalidInput){
                JOptionPane.showMessageDialog(null, "Invalid Input.\nTry again","Input Error",JOptionPane.ERROR_MESSAGE);
                option = JOptionPane.showOptionDialog(null,panel,"Number of Names",JOptionPane.NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[0]);
            }
          }
          else if (option==1){
              System.exit(0);
          }
      }
  }
  </code></pre>
  
  <p>2.BackGroundPanel Class:</p>
  
  <pre><code>import java.awt.Dimension;
  import java.awt.Graphics;
  import java.awt.Image;
  import java.awt.image.BufferedImage;
  import javax.swing.JPanel;
  
  
  public class BackGroundPanel extends JPanel{
      Image image;
      public BackGroundPanel(Image image){
          this.image = image;
      }
      protected void paintComponent(Graphics g){
          super.paintComponent(g);
          Image scaled = image.getScaledInstance(getWidth(), getHeight(), Image.SCALE_FAST);
          g.drawImage(scaled, 0, 0, null);
      }
  
      public Dimension getPreferredSize(){
          Dimension d = super.getPreferredSize();
          int w = d.width &gt; image.getWidth(null)? d.width:image.getWidth(null);
          int h = d.height &gt; image.getHeight(null)?d.height:image.getHeight(null);
          return new Dimension(w,h);
      }
  
      public Image getImage(){
          return image;
      }
  
      public void setImage(BufferedImage image){
          this.image = image;
      }
  }
  </code></pre>
  
  <ol start="3">
  <li><p>TeamsDrawSim class:</p>
  
  <pre><code>public class TeamsDrawSim {
      public static void main(String[] args) {
          // TODO Auto-generated method stub
          FirstWindow firstWindow = new FirstWindow();
      }
  }
  </code></pre></li>
  </ol>
  
  <p>In this code after all the procedure which has to do with the number of names, i create a window with a background image uploaded from my files which contains a vertical scrollbar when needed and a panel with false opaque on which i place all my components(At first stage i create jlabels).The problem is that when i create too many labels, so that the vertical scrollbar needs to appear, the background image stretches following the edges of the scrollbar. What i want to do is to show the whole image in any point of the scrollbar. Any idea why this is happening? Below i give you two screenshots of the problem, one for small number of labels and one for large number of labels. The window is resizable and the same thing is happening when in full screen mode.</p>
  
  <p>Small Number of Labels:</p>
  
  <p><a href="https://i.stack.imgur.com/inNsK.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/inNsK.jpg" alt="http://i.stack.imgur.com/inNsK.jpg"></a></p>
  
  <p>Large Number of Labels</p>
  
  <p><a href="https://i.stack.imgur.com/KONxu.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KONxu.jpg" alt="http://i.stack.imgur.com/KONxu.jpg"></a></p>