Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Altering Killer Game Programing ImageLoader class  (Read 2348 times)
0 Members and 1 Guest are viewing this topic.
Offline Dream Of Sleeping
« Posted 2012-03-18 15:55:45 »

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

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

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
 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.

Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-03-18 16:08:58 »

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

Offline Dream Of Sleeping
« Reply #2 - Posted 2012-03-18 16:21:41 »

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-03-18 16:24:15 »

Yeah it doesn't matter where your class is. And thanks!! Grin

Offline Dream Of Sleeping
« Reply #4 - Posted 2012-03-18 16:39:16 »

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.

Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2012-03-18 16:53:21 »

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

Offline Dream Of Sleeping
« Reply #6 - Posted 2012-03-18 18:53:54 »

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

1  
 new ImageLoader(this, "fileName.txt")


is easy.

Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2012-03-18 19:04:10 »

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

Offline Dream Of Sleeping
« Reply #8 - Posted 2012-03-18 19:10:33 »

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

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

Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2012-03-18 19:13:32 »

Oh no don't worry, I'm only glad to help Grin

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (57 views)
2014-11-26 15:22:04

toopeicgaming1999 (50 views)
2014-11-26 15:20:36

toopeicgaming1999 (9 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (26 views)
2014-11-25 11:26:43

Gibbo3771 (23 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (75 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!