How to display image as background on JPanel

I have a problem displaying an image on a JPanel when I distort the image according to the size of the JPanel. Image did not appear.

   public class createGUII extends JFrame{
        String [] background = {"c1.jpg","c2.jpg","c3.jpg","c4.jpg"};                       
        ArrayList<String> bgPicturesFiles   = new ArrayList<String>(Arrays.asList(background)); 


    JPanel panel;
    ImagePanel imgBg;

    public createGUII(){
        GridBagLayout m = new GridBagLayout();
        Container c = getContentPane();
        c.setLayout (m);
        GridBagConstraints con = new GridBagConstraints();

       //Panel for background
        panel = new JPanel();       
        panel.setSize(600, 600);
        con = new GridBagConstraints();
        con.anchor=GridBagConstraints.CENTER;
        con.gridy = 1;      con.gridx = 0;
        con.gridwidth = 1;  con.gridheight = 1;     
        m.setConstraints(panel, con);   
        c.add(panel);

       //randomized the image files
        Random r = new Random();                        
        int random = r.nextInt(bgPicturesFiles.size());

       //rescale the image according to the size of the JPanel 
        imgBg = new ImagePanel(new ImageIcon(bgPicturesFiles.get(random)).getImage().getScaledInstance(panel.getHeight(), panel.getWidth(),Image.SCALE_SMOOTH));            
        panel.add(imgBg);

        setResizable(false);
        setVisible(true);       
        setExtendedState(getExtendedState()|JFrame.MAXIMIZED_BOTH);             

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new createGUII();       
            }
        });
    }
}



class ImagePanel extends JPanel {
    private Image img;

    public ImagePanel(String img) {
        this(new ImageIcon(img).getImage());
    }

    public ImagePanel(Image img) {
        this.img = img;
        Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);
        setLayout(null);
        }

    public void paintComponent(Graphics g) {
        g.drawImage(img, 0, 0, null);
        }
}     
+3
source share
4 answers

Look at this: JPanel background image , JPanel with background image, with other panels overlaid

, . . paintComponent, , , BufferedImage, , blit paintComponent, ( - , ). , , paintComponent , .

+1

, , .

: . . getScaledInstance() Image.getScaledInstance().

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/** @see http://stackoverflow.com/questions/4170463 */
public class LoadImage extends JPanel {

    private Image image;

    public LoadImage() {
        super(new GridLayout());
        try {
            image = ImageIO.read(new File("image.jpg"));
        } catch (IOException ex) {
            ex.printStackTrace(System.err);
        }
        int w = image.getWidth(null) / 2;
        int h = image.getHeight(null) / 2;
        this.add(new JLabel(new ImageIcon(
            image.getScaledInstance(w, h, Image.SCALE_SMOOTH))));
    }

    private void display() {
        JFrame f = new JFrame("LoadImage");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new LoadImage().display();
            }
        });
    }
}
0

, Java . - :

  • , (-1, -1). , ImagePanel .
  • (.. new Dimension(600, 600)), .
  • JFrame . , , , Swing JFrame.

, :

new ImageIcon(img).getImage();

this.img = img; ImagePanel.

, - , , . , - .

, :

, .

0

. , , , , .

, .

Container con = getContentPane();
final String backgroundPath = "C:\\background.jpg";

ImageIcon imh = new ImageIcon(backgroundPath);
setSize(imh.getIconWidth(), imh.getIconHeight());

JPanel pnlBackground = new JPanel()
{
    public void paintComponent(Graphics g) 
    {
        Image img = new ImageIcon(backgroundPath).getImage();
        g.drawImage(img, 0, 0, null);
    }
};

con.add(pnlBackground);
pnlBackground.setBounds(0, 0, imh.getIconWidth(), imh.getIconHeight());
0

Source: https://habr.com/ru/post/1774642/


All Articles