What is the correct way to display an ImageIcon PNG file for Windows 7?

I wanted to test a program with a simple png image on it. I wrote a short program that does this, but I cannot find the right path. I checked, checked again, double-checked, and the four checked my path name to not get it right, but this image will not be displayed, no matter what I do. I used a short class written by Oracle in the ImageIcon documentation ( creaetImageIcon() ) to accomplish this, but it doesn't seem to help. I will post the whole 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); } } 
+4
source share
6 answers

The getResource(String) method will find resources that are in the path of the application's execution class. Since this image looks like an application resource (i.e., provided by you as part of the application), it should be placed in the path of the runtime class.

eg. Most IDEs have a place where you can put resources in a project structure that will be automatically included at runtime. Move (or copy) the image to this path.

Then it will become a question of the correct String . Imagine your project is configured like this:

  • Ben
  • CSI
    • com
      • our
        • Application.java
    • resources
      • green.png

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

When accessing the image from Application correct path will be (drum roll, please ..)

"/resources/green.png"

Notes

  • Lead / important. He tells the JRE that we want to look for an image from the "root of the class path", as opposed to using the path relative to the package of the class itself.
  • The right case is also vital. The string "/resources/green.png" will not find the image with the name "/resources/green.png" or "/resources/green.png" .

Eclipse Paths

  • Right-click on the src directory, select Properties at the bottom of the menu.
    Eclipse properties
  • Navigate (using the normal method that you use without Eclipse) to the Location directory. Eclipse properties showing project Location
  • Then go to the parent directory.
  • You should see a bin directory that contains classes and (hopefully) an image.
+9
source

First, you provided a relative path, so the system looks for an image relative to the location you performed.

Secondly, the path must have a disk specification, or at least a leading / . Depending on your setup, something like "C: /Users/Evan/javaItems/Sprites_and_Other_Art/green.png" should work (you may need to change the disk specification to suit your system)

Thirdly, make sure that the file exists at the specified location, System.out.println(new File("C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png").exists()) should return true , another wise file is in the wrong place.

Relative path basically means the location of the path relative to program execution. So, if you ran the program from C:/Program Files/MyAwesomeApplication , for example, the relative path Users/Evan/javaItems/Sprites_and_Other_Art/green.png became the absolute path C:/Program Files/MyAwesomeApplication/Users/Evan/javaItems/Sprites_and_Other_Art/green.png . This describes the path from the root directory to the corresponding file / folder.

You can verify this using System.out.println(new File("C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png").getAbsolutePath(β€Œβ€‹)) , which will give you the full path.

+3
source

Using this fixed for me:

 JButton btnBanana = new JButton("New button"); btnBanana.setIcon(new ImageIcon("D:\\Android\\Company\\images\\bananas-icon.png")); 
0
source

use double slash instead of one, i had this problem and i fixed it. bad to show you an example:

public Driver () {

  ImageIcon us = new ImageIcon("C:\saeed.gif"); // OS cant find it ImageIcon uk = new ImageIcon("C:\\saeed0.gif"); // OS can JButton button = new JButton ("Click here " , us ) ; button.setRolloverIcon(uk); add(button); } 
0
source

To get the path to the image in the text box, this code will help you

 txtPath.setText(lblImage.getIcon().toString()); 

// txtPath is a text field using the todiplay path // lblImage is a label that shows the image

0
source

You need to do C:\\Test\\test.png , not C:/Test/test.png

-2
source

Source: https://habr.com/ru/post/1481585/


All Articles