Search code examples
javaimageloadimage

Adding/Loading an image in java


I am trying to load an image to display on the screen (just to get an idea of how to do it).

The problem is that when the program tries to load "apple.png" (which is saved on my desktop), it cannot find the image - Where do image files need to be stored in order for them to be found? Here is my loading method:

private void loadImage() {
    ImageIcon appleIcon = new ImageIcon("apple.png");
    Image appleImage = appleIcon.getImage();
}

Solution

  • If you want to reach it from the desktop, you should use the complete path. The easiest way to handle resources would be to create a folder in you java project, which you can access via "folderName/fileName.example".