Altering Killer Game Programing ImageLoader class

Hello. I’ve made a class that is very similiar to the ImageLoader class that is used in Killer Game Programing in java. For anyone who doesn’t know it reads image names from a text file and loads them into maps. I’ve altered how it reads so I can use my own formatting in the text file. Also the original uses this bit of code…

 BufferedImage im =  ImageIO.read( 
                      getClass().getResource(IMAGE_DIR + fnm) );

Which is great if the images are in the same directory as the class file as the image loader. But I wanted to have the image loader in a different package. So I pass an Object, any object into the constructor and it gives that the name locator, so I can load images in a similar like this…

 BufferedImage im = ImageIO.read(locator.getClass().getResource(fileName));

I was wondering if passing any object to help locate the image files is considered a code smell? It seemed like a very easy way to do it.

My second question is about using a MediaTracker. I have added a MediaTracker, and I wanted to check that I have used it correctly or if I even need it at all.

 private BufferedImage loadImage(String fileName) {
            
        try {
            BufferedImage im = ImageIO.read(locator.getClass().getResource(fileName));
            mediaTracker.addImage(im, imageCount++ );
           
            int transparency = im.getColorModel().getTransparency();
            BufferedImage copy = graphicsConfiguration.createCompatibleImage(
                    im.getWidth(), im.getHeight(), transparency);
            
            Graphics2D g2d = copy.createGraphics();
            
            g2d.drawImage(im, 0, 0, null);
            g2d.dispose();
            return copy;
       
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
    }

I’m not sure what is happening. When imageIo reads the image, does it wait for it to read before continuing? If that is the case then would I really need a media tracker. I could just add a boolean method in the ImageLoader class called finishedLoading() and continue asking in the loop before drawing the images. Or maybe there is a way to just pause the thread like the MediaTracker method waitForAll(). I really need to relearn about threads.

Any help will be much appreciated.

There is no need for MediaTracker since ImageIO.read doesn’t return until the image is read and loaded.

You don’t need to use an object to use as the “locator”, using a forward slash at the beginning of the file name will make it search from the root, or you could load from the ClassLoader:


getClass().getClassLoader().getResource(fileName);

that code is the same as ‘getClass().getResource("/" + fileName);’

EDIT: Searching from the root means the root of the project, aka the current working directory, such as the “bin” folder in Eclipse or the folder and root of the JAR file when standalone.

Thanks ra4king.

As ImageLoader is in a different package, not in the same directory as the class files for this project, if I pass it the file name with forward slash at the beginning, will it be able to find the image even though it’s not in that folder?

By the way, I’m addicted to your doodle jump game. Well done it plays great.

Yeah it doesn’t matter where your class is. And thanks!! ;D

I can’t get it to work. I’ve kept my locator object to find the text file, which it does fine. But I’ve changed my load image code to…

BufferedImage im = ImageIO.read(getClass().getClassLoader().getResource(fileName));

and it can’t find it. I also tried the foward slash version.

But when I change it back to this BufferedImage im = ImageIO.read(locator.getClass().getResource(fileName)); it works.

Make sure you have the path right. For example, if the file is under com.example.mypackage, then do: “com/example/mypackage/MyFile.txt”

As far as I understand, (although I admit there is a lot I do not know!), I would have to send the ImageLoader the path of the text file. And then create a URL for each of the images so it could be read by ImageIO.

I think I like my way. It might be a bit unconventional, but creating an ImageLoader like this…

 new ImageLoader(this, "fileName.txt")

is easy.

Whatever you like! I’m only providing suggestions for easier loading, but if you prefer using relative paths then go ahead :slight_smile:

I hope I didn’t come across as ungrateful. Because I am very grateful for your advice even though I decided to stick with what I originally had. I even clicked the little appreciate link. lol

Oh no don’t worry, I’m only glad to help ;D