Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct path to display an ImageIcon png file for Windows 7?

I wanted to test having a program with a simple png image on it. I wrote a short program that does this, but I can't seem to get the path right. I have checked, checked again, rechecked, and quadruple checked my path name as to not get it right, but this image will not display, no matter what I do. I used a short class wrote by Oracle in the ImageIcon documentation (the creaetImageIcon()) to accomplish this, but it doesn't seem to help. I'll post the entire program below, as it is very short.

package practiceImages;

import java.awt.BorderLayout;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ImageIconGUITest {

    public static void main(String[] args) {
        ImageIconGUITest gui = new ImageIconGUITest();
        gui.display();
    }

    private ImageIcon createImageIcon(String path, String description) {
        java.net.URL imgURL = getClass().getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    private void display() {
        JFrame frame = new JFrame();
        JLabel label = new JLabel(createImageIcon(
                "Users/Evan/javaItems/Sprites_and_Other_Art/green.png", "the color green"));

        frame.add(BorderLayout.CENTER, label);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }
}
like image 231
user2398233 Avatar asked May 19 '13 05:05

user2398233


People also ask

How to use ImageIcon in java swing?

Creates an ImageIcon from an array of bytes which were read from an image file containing a supported image format, such as GIF, JPEG, or (as of 1.3) PNG. Creates an ImageIcon from an array of bytes which were read from an image file containing a supported image format, such as GIF, JPEG, or (as of 1.3) PNG.


1 Answers

The getResource(String) method will only find resources that are on the run-time class-path of the application. Since this image seems like an application resource (i.e. supplied by you as part of the application) it should be put on the run-time class-path.

E.G. Most IDEs have a place you can put resources within the project structure, that will automatically be included at run-time. Move (or copy) the image to that path.

Then it becomes a matter of providing the correct String. Let us imagine your project is set up something like this:

  • bin
  • src
    1. com
      • our
        1. Application.java
    2. resources
      • green.png

So Application.java is in package com.our;, while the image is in the path resources/green.png.

If accessing the image from the Application, the correct path would be (drum roll please..)

"/resources/green.png"

Notes

  1. The leading / is important. It tells the JRE we want to look for the image from the 'root of the class-path', as opposed to using a path relative to the package of the class itself.
  2. Correct case is also vital. A string of "/resources/green.png" will not locate an image named "/resources/Green.png" or "/resources/green.PNG".

Eclipse paths

  1. Right click on the src directory, select Properties at the bottom of the menu.
    Eclipse properties
  2. Navigate (using the normal way you'd use without Eclipse) to the directory of the Location. Eclipse properties showing project Location
  3. Then go to the parent directory.
  4. You should see a bin directory that contains classes and (hopefully) the image.
like image 166
Andrew Thompson Avatar answered Oct 04 '22 01:10

Andrew Thompson