-1

This is my code for a program which has a background image and takes components on it:

1.FirstWindow class:

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>=0 && numberOfNames<4){
                  JOptionPane.showMessageDialog(null, "No reason for holding a draw.\nProgram will terminate.",JOptionPane.PLAIN_MESSAGE);
                  System.exit(0);
              }
              else if (numberOfNames>=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<150;i++){
                      aPanel.add(new JLabel("NAME"));
                  }

                  Component array[][] = new Component[numberOfNames][2];
                  for (int i=0;i<array.length;i++){
                      array[i][0] = new JLabel("Name"+(i+1)+":");
                      array[i][1] = new JTextField(10);
                  }
                  for (int i=0;i<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<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);
        }
    }
}

2.BackGroundPanel Class:

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 > image.getWidth(null)? d.width:image.getWidth(null);
        int h = d.height > 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;
    }
}
  1. TeamsDrawSim class:

    public class TeamsDrawSim {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            FirstWindow firstWindow = new FirstWindow();
        }
    }
    

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.

Small Number of Labels:

https://i.stack.imgur.com/inNsK.jpg

Large Number of Labels

https://i.stack.imgur.com/KONxu.jpg

1
  • Please add a minimal but complete code example, I don't want to have to hunt for your problem in that code dump Dec 4, 2015 at 16:34

1 Answer 1

0

What i want to do is to show the whole image in any point of the scrollbar.

You need to paint the background image on the viewport of the scrollpane instead of the panel. Then the image will always be scaled to the viewport size. You will also need to make the panel non-opaque so the image will be visible. Something like:

JPanel panel = new JPanel(...);
panel.setOpaque( false );
panel.add(...);

JViewport viewport = new JViewport()
{
    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
    }
};

JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewport(viewport);
scrollPane.setViewportView( panel );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.