Omarkk
Omarkk

Reputation: 83

load image to JPanel not working

For two days I was trying to load an image into JPanel from a file. I couldn't!

I used JLabel and Icon and it's loaded okay, but I need to load the image to a JPanel directly, is that impossible?

Because almost I saw many and many related problems like this and many people recommended the person who asks the question to load the image into a label!

this is the code :

public class ReadingImage extends JPanel  {
JPanel panel;
JFrame frame; 
JPanel secPanel;
private BufferedImage img;


public ReadingImage(String path){
    frame = new JFrame();
    frame.setVisible(true);
    frame.setLocation(300, 300);
    frame.setSize(300, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());

    secPanel  = new JPanel();
    secPanel.setVisible(true);
    //secPanel.setLayout(new FlowLayout());

    secPanel.repaint();

    frame.getContentPane().add(secPanel);

    try{
        FileImageInputStream fi = new FileImageInputStream(new File(path));
        //System.out.println(path);
        img = ImageIO.read(fi);
        this.repaint();
    }
    catch (IOException io ){ io.printStackTrace();}
}

@Override
protected void paintComponent(Graphics g){
    super.paintComponents(g);
    if (img!=null){
        g.drawImage(img, 0, 0, this);
        repaint();
    }
}
}

It's not throwing any exception, but it's not displaying the image in the JPanel!

I adjusted the code many and many times..

any help in this :)

thanks,

Upvotes: 0

Views: 345

Answers (3)

Sai Vigneshu
Sai Vigneshu

Reputation: 1

first load the image in Image Icon, lets say the object as 'pic'.

panel1.add(new JLabel(pic));

add and set panel1 to visible.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347184

You are calling super.paintComponents(g); in paintComponent, not the s at the end, this is going to cause a StackOverflowException eventually, but don't actually ever add the ReadingImage JPanel to anything, so it's never actually painted

This means that there doesn't seem to be any point any point to the secPane

You should also avoid creating frames from within other components, especially from within the constructor, it provides a very limit use case for the component

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageInputStream;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ReadingImage extends JPanel {

    private BufferedImage img;

    public ReadingImage(String path) {
        try {
            FileImageInputStream fi = new FileImageInputStream(new File(path));
            img = ImageIO.read(fi);
            this.repaint();
        } catch (IOException io) {
            io.printStackTrace();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (img != null) {
            g.drawImage(img, 0, 0, this);
            repaint();
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame();
                frame.add(new ReadingImage("Your image"));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocation(300, 300);
                frame.setSize(300, 500);
                frame.setVisible(true);

            }
        });
    }

}

I'm not sure exactly what you are trying to achieve, but I would also encourage you to override the ReadingImage's getPreferredSize method and return the size of the image, this makes it easier to layout

Upvotes: 2

user4668606
user4668606

Reputation:

Classic confusion caused by extending JPanel and using another JPanel. Replace frame.getContentPane().add(secPanel) with frame.add(this , BORDERLAYOUT.CENTER) and everything should work fine.

Upvotes: 2

Related Questions