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;
}
Cupple Kay
  • 155
  • 2
  • 14
  • "player" + x + ".png" you know that x is an `int` here right, not a string? – jhobbie Jul 16 '14 at 13:31
  • Have you checked if they're being correctly created when you populate the array? – resueman Jul 16 '14 at 13:31
  • @resueman How would you do that. When i check the first one it says imageFile="player0.png" and i have that image in the same package. – Cupple Kay Jul 16 '14 at 13:33
  • 2
    @jhobbie So? That's how string concatenation works – BackSlash Jul 16 '14 at 13:33
  • 1
    I am assuming it would return a -1 when the image file has not been found, I would create a `RES` folder which has all of your image resources and etc. All you have to do is reference it like: "res/*NAME*.extension". If you are surrounding the image creation by a try and catch in your actual code, remove it and see if an error is thrown – Cartier Jul 16 '14 at 13:36
  • Try just putting in a print of the playersSprites[x].getWidth() after you create it, to check that it's being loaded correctly. Loading Images can be tricky in Java sometimes. – resueman Jul 16 '14 at 13:40
  • @Cartier1288 There wasn't an error and it changed nothing and i didnt use a try catch. – Cupple Kay Jul 16 '14 at 13:42
  • @CuppleKay: Please have a look at how to [add Images to Java Project](http://stackoverflow.com/a/9866659/1057230), might be this be able to help you somewhat in your endeavour :-) – nIcE cOw Jul 16 '14 at 14:04

2 Answers2

2

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...

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • The second example didn't work, because I got a nullpointerexception. The images are in a folder(res) parallel to src. I don't understand because other people also said to do it like that. – Cupple Kay Jul 16 '14 at 14:28
  • please share the project hierarchy(structure) if possible. – Braj Jul 16 '14 at 14:31
  • As I said. I chose basing on the location and it didn't work. – Cupple Kay Jul 16 '14 at 14:32
  • sorry but can you share project structure. have you tried other options. click or read more for project structure. – Braj Jul 16 '14 at 14:33
  • 1
    It's ok i just found the problem. I had tried someone else his suggestion where he checked an image without a name so I always got -1. It works good now thanks – Cupple Kay Jul 16 '14 at 14:36
1

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.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720