XandruDavid
XandruDavid

Reputation: 67

Adding image to JPanel in Eclipse

I have this in my main class:

panel.setBackground(Color.green);
ImagePanel background = new ImagePanel("Images/background.png");
panel.add(background);

But when I run it I only see the green background and get the exception:

"javax.imageio.IIOException: Can't read input file!"

This is the ImagePanel class:

public class ImagePanel extends JPanel {
private BufferedImage img;

public ImagePanel(String path) {
    // load the background image
    try {
        img = ImageIO.read(new File(path));
    } catch(IOException e) {
        e.printStackTrace();
    }
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    // paint the background image and scale it to fill the entire space
    g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
}
}

I'm using Eclipse and this is where my image is: src/Images/background.png


Ok, now i have:

ImagePanel background = new ImagePanel("src/Images/background.png");

and it don't show the exception anymore, but i still don't see the image, only the green background...

Here is the full method:

 private void createAndShowGUI() {

        frame = new JFrame("Java 2048 By Xandru");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLayout(null);

        panel = new JPanel();
        panel.setLayout(null);
        panel.setBounds(0, 0, HEIGHT, WIDTH);
        panel.setBackground(Color.green);
        frame.add(panel);

        //Add the background
        ImagePanel background = new ImagePanel("src/Images/background.png");
        panel.add(background);

        //Create the main Frame
        frame.pack();

        //Set dimensions
        frame.setSize(WIDTH, HEIGHT);

        //Center it
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((screen.getWidth() - frame.getWidth()) /2);
        int y = (int) ((screen.getHeight() - frame.getHeight()) /2);
        frame.setLocation(x, y); 

        //Set visible
        frame.setVisible(true);
    }

Upvotes: 0

Views: 6846

Answers (1)

Braj
Braj

Reputation: 46841

Looking an image 2.png from resources folder

Image image= ImageIO.read(new File("resources/2.png"));

OR

Try this one if the image is in the same package(folder) where the class is present

Image image = ImageIO.read(getClass().getResourceAsStream("2.png"));

Here is the project structure

enter image description here


-- EDIT-

Try in this way

EventQueue.invokeLater(new Runnable() {

    @Override
    public void run() {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
        } catch (InstantiationException ex) {
        } catch (IllegalAccessException ex) {
        } catch (UnsupportedLookAndFeelException ex) {
        }

        JFrame frame = new JFrame("Java 2048 By Xandru");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        // Add the background
        ImagePanel background = new ImagePanel("src/images/2.png");
        frame.add(background);

        // Create the main Frame
        frame.pack();

        // Set dimensions
        frame.setSize(new Dimension(width, height));

        // Center it
        frame.setLocationRelativeTo(null);

        // Set visible
        frame.setVisible(true);

    }
});

Upvotes: 2

Related Questions