0

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);
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
XandruDavid
  • 67
  • 1
  • 8
  • From a design POV, `public ImagePanel(String path) {` should be `public ImagePanel(Image image) {` (IMO). That way, the panel can handle an image from file, URL or ..generated in memory. – Andrew Thompson Apr 15 '14 at 08:28

1 Answers1

2

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);

    }
});
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Don't know if this is the right way to call your attention, i'm new on stackoverflow... anyway, i have updated the question :) – XandruDavid Apr 14 '14 at 19:53
  • No problem. Its the right way. Welcome to StackOverflow. – Braj Apr 14 '14 at 19:54
  • Thanks, this worked for the exception, so now i have the image loaded correctly but i still can't get it on screen... :( – XandruDavid Apr 14 '14 at 19:59
  • 1
    @XandruDavid you should probably show how you are adding the panel – Paul Samsotha Apr 14 '14 at 20:03
  • Don't use `null` layout. Don't use `setBounds`. Why are you using an extra `panel` in between? – Braj Apr 14 '14 at 20:10
  • the null layout and the bounds are becouse i want the panel to fill the frame... for the panel... i'll replace the main panel with the ImagePanel... – XandruDavid Apr 14 '14 at 20:14
  • use `frame.setLocationRelativeTo(null);` to center it. – Braj Apr 14 '14 at 20:16
  • `JFrame` has default `BorderLayour` that add a component in the center by default. – Braj Apr 14 '14 at 20:17
  • Always use `EventQueue.invokeLater` in Swing application. – Braj Apr 14 '14 at 20:19
  • You can set Look & Feel by calling `UIManager.setLookAndFeel()`. – Braj Apr 14 '14 at 20:19
  • Use this way to set the size `frame.setSize(new Dimension(width, height));` – Braj Apr 14 '14 at 20:20
  • Ok, thank you all, it warks without the extra panel... Now i have a little problem, the windows is WIDTHxHEIGHT but i wanted the working area to be WIDTHxHEIGHT, how can i do it? – XandruDavid Apr 14 '14 at 20:23
  • Sorry I didn't get it. Please explain a bit more. – Braj Apr 14 '14 at 20:25
  • Where you have defined HEIGHT, WIDTH? I think its referring to `ImageObserver.HEIGHT` and `ImageObserver.WIDTH` that is 2 and 1 respectively. – Braj Apr 14 '14 at 20:28
  • I have frame.setSize(new Dimension(width, height)); but it sets the dimensions of the entire window (with title, minimize button, maximize button etc)... That's not what I wanted couse my ImagePanel will not be width x height this way... – XandruDavid Apr 14 '14 at 20:29
  • Why you want to set it. Just make it a re-sizable window. because height of title bar will be different on different OS and themes. – Braj Apr 14 '14 at 20:31
  • Look at this thread [Java Upper Boundary Graphics](http://stackoverflow.com/questions/22722777/java-upper-boundary-graphics/22722949#22722949) You will get the answer of your new question. – Braj Apr 14 '14 at 20:35
  • I've got it... The problem is that i don't want the user to resize it, all i want is a 450x450 image to fill the Frame... and it have to be all times that size, even if i make a borderless frame or a normal frame, how can i do it? – XandruDavid Apr 14 '14 at 20:40
  • You want the exact height/width of image then try this in `paintComponent()` method. `g.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), this);` or `g.drawImage(img, 0, 0, 450, 450, this);` – Braj Apr 14 '14 at 20:42
  • I've bounded the panel to 450x450 but it goes out of view because window is still 450x450 (with borders and title bar)... – XandruDavid Apr 14 '14 at 20:54
  • You want to set the height of image or panel or frame? – Braj Apr 14 '14 at 20:54
  • I want to see a 450 by 450 image inside my frame, that's all xD so i have to set frame something like 455 by 475 but this sucks... – XandruDavid Apr 14 '14 at 20:57
  • [link](http://stackoverflow.com/questions/2451252/swing-set-jframe-content-area-size) That was it... Worked with the preferredSize method... sorry for my bad english and thank you very much for your help ;) – XandruDavid Apr 14 '14 at 21:04
  • Have you tried `g.drawImage(img, 0, 0, 450, 450, this);` just this one nothing else. no other size of panel or frame. – Braj Apr 14 '14 at 21:05