简体   繁体   中英

Java swing Image always has height and width of -1

I know that the method returns -1 when it couldn't get the width or height of the image, but I hope you can tell me why it can't manage to do that. Here I create a few ImageIcons and save them in an Image Array:

for(int x = 0; x < playerSprites.length; x++){
    playerSprites [x] = new ImageIcon("player" + x + ".png").getImage()
}

Later I create an instance of the class which only creates this Array at the moment. When I then want to get the images from the Array in the other class I check their height and width and I always get -1 on both:

public Image nextImage(String name){
    Image image = null;
    if(name.equals("player")){
        if(counter == animationImageManager.getPlayerSprites().length-1){
            counter = 0;
        }
        image = animationImageManager.getPlayerSprites()[counter];
        counter++;
    }
    return image;
}

If image is not found then still it return -1 for height and width.

Try below sample code to reproduce the issue:

System.out.println(new ImageIcon("").getImage().getWidth(null)); // print -1

It's worth reading Java Tutorial on Loading Images Using getResource


May be it's not loading the images properly.

You can try any one based on image location.

// Read from same package 
ImageIO.read(getClass().getResourceAsStream("c.png"));

// Read from images folder parallel to src in your project
ImageIO.read(new File("images/c.jpg"));

// Read from src/images folder
ImageIO.read(getClass().getResource("/images/c.png"))

// Read from src/images folder
ImageIO.read(getClass().getResourceAsStream("/images/c.png"))

Read more...

The width/height for Image will return -1 if the image is null. When there's no image.

Suggestions:

  • use ImageIO.read() which will throw an IOException if something goes wrong with the IO, like the path being wrong.

  • If the image is a resource for your application, then don't read it as a file, read it as a resource via URL. For instance, if the image is in src/images , then you could do

     URL url = getClass().getResource("/images/myimage/png"); BufferedImage image = ImageIO.read(url); 
  • Important thing to note with ImageIcon is when you pass a String to the constructor, it will look for the file in the local file system. It may work when you are developing, but once you deploy the application with the images in the jar, it won't work anymore, with the file path, because it will no longer be valid. You could pass the URL to ImageIcon just the same as above, but like I said, ImageIO allows for more error detection.

  • Just so you understand what's going on in your current code, by you specifying just the image file name as the path to the ImageIcon, the search will look for the image in the root of the project folder (if you're working in an IDE) because that's the working directory. So if your images aren't there, the images won't be found.

  • Another thing to note about my second bullet point is how the image is search for. You can see the path I used "/images/myimage/png" . What the / in the front does, is bring the search to root of the classpath, which in development view, is the src . The calling class will normally be in some package on the classpath, say com.hello.somepackage.SomeClass . So if SomeClass tries to call the getclass getresource without the / , the search will begin from the location of the class, which is in the package.

The are just some things to consider when using resources/images. But the first couple points should get you going.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM