java how to add image using getResource()?

2019-09-09 07:29发布

问题:

I want to add an image to a Jpanel Class, I use the method getResource() but it returns Exception in thread "main" java.lang.IllegalArgumentException: input == null!, because the path into the image folder that i give is not valid, now I want how to get the valid path, I'm aware that maybe it's really easy and it's not a question that I've to ask here but I'm spending a lot of time and trying different path but it doesn't work;

here the Jpanel class

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;


public class Application extends JPanel  {

    private BufferedImage image;

    public Application() {
          try {
                image = ImageIO.read(getClass().getResource("/ressources/image.png"));
          } catch (IOException ex) {
                System.out.println("problem! image can't be loaded!");
          }
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null);
    }
}

and here the main class

import javax.swing.*;
//import java.awt.*;

public class mainClass {

        public mainClass () {

            JFrame app = new JFrame();
            app=.setTitle("main window");
            app=.setSize(400, 400);
            app=.setLocationRelativeTo(null);
            app=.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            app=.add(new Application()); 
            app=.setVisible(true);
        }

        public static void main(String[] args) {
            mainClass class = new mainClass();
        }
    }

the error is clearly in the line image = ImageIO.read(getClass().getResource("/ressources/image.png"));

cause the path is not valid, I use a maven project, so the path of this classes is : projectName\src\main\java\application\mainClass.java projectName\src\main\java\application\Application.java

I created a folder "ressources" and i put the image there, here the path projectName\ressources\image.png

thank you.

回答1:

You need to place your image in src/main/resources/image.png for it to be seen as in the resource root of this project...and watch that spelling of ressource. See http://www.mkyong.com/java/java-read-a-file-from-resources-folder/ for a nice summary of resource loading.

Then this line should work;

image = ImageIO.read(getClass().getClassLoader().getResource("image.png")



回答2:

Answer of both Aidan and abu are correct, but there is something we could clarify here.

The standard root folder for a project is defined as "src" ( JavaProject/src ), so we just could put the image.png in this folder and type:

1st method: image = ImageIO.read(new File("image.png"));

from the point of view of your program (in Eclipse) this yould give the same result as this:

2nd method: image = ImageIO.read(getClass().getClassLoader().getResource("image.png"));

Now, the difference is that, when you are going to export your project in a .jar file, the first line will not export the image.png file, because the export only compiles resources at first and you will get errors because the programm doesn't see the image anymore. This is where the 2nd method is useful. Because it exports also images inside your jar file. You can watch with winrar inside to verify it.



回答3:

Put the ressources folder in src folder.