| | |
Loading Images
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
I'm trying to load a bitmap into an Image object using the following code:
m_image is a private java.awt.Image object of the class I'm in and filename is a String argument of the method I'm in. The filename I pass to this method when I call it is the name of a bitmap in the same directory of the .java file. If it matters, I'm using eclipse to run this.
The code gets to point F, and gives me a NullPointerException (or so I think that's the issue, based on my knowledge of Java error output):
Any help getting the above code to work (or suggesting a alternate method of loading images) is appreciated.
Java Syntax (Toggle Plain Text)
System.out.println("Point A"); System.out.println("Point B"); m_image = Toolkit.getDefaultToolkit().getImage(getClass().getResource(filename)); System.out.println("Point C"); MediaTracker mt = new MediaTracker(null); System.out.println("Point D"); mt.addImage(m_image, 0); System.out.println("Point E"); try { System.out.println("Point F"); mt.waitForID(0); System.out.println("Point G"); } catch (InterruptedException ie) { System.err.println(ie); System.exit(1); } System.out.println("Point H");
The code gets to point F, and gives me a NullPointerException (or so I think that's the issue, based on my knowledge of Java error output):
Java Syntax (Toggle Plain Text)
Exception in thread "main" java.lang.NullPointerException at java.awt.ImageMediaEntry.getStatus(MediaTracker.java:908) at java.awt.MediaTracker.statusID(MediaTracker.java:705) at java.awt.MediaTracker.waitForID(MediaTracker.java:653) at java.awt.MediaTracker.waitForID(MediaTracker.java:622) at Sprite.SetImage(Sprite.java:26) at Main.main(Main.java:45)
Any help getting the above code to work (or suggesting a alternate method of loading images) is appreciated.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
which line is line 45? there are only 20 here.
what type of image are you trying to load and store to?
what point is it printing up to? (point a,b,c,d,e,f,g,h)
as an alternative why not use
of course i don't know if this works with resources
what type of image are you trying to load and store to?
what point is it printing up to? (point a,b,c,d,e,f,g,h)
as an alternative why not use
BufferedImage img=ImageIO.read(new File("myFile.jpg")); ?of course i don't know if this works with resources
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
Sorry about the lines, here's the original file:
line 45 is just the call to this method from another file.
I'm loading a bitmap.
I tried the method you suggested and I get this exception:
javax.imageio.IIOException: Can't read input file!
Here's the code:
I also changed m_image from an object of Image to BufferedImage
Java Syntax (Toggle Plain Text)
import java.awt.*; public abstract class Sprite { private Image m_image; private Point m_position; public Sprite(int x, int y) { m_position = new Point(x, y); } public void SetImage(String filename) { System.out.println("Point A"); System.out.println("Point B"); m_image = Toolkit.getDefaultToolkit().getImage(getClass().getResource(filename)); System.out.println("Point C"); MediaTracker mt = new MediaTracker(null); System.out.println("Point D"); mt.addImage(m_image, 0); System.out.println("Point E"); try { System.out.println("Point F"); mt.waitForID(0); System.out.println("Point G"); } catch (InterruptedException ie) { System.err.println(ie); System.exit(1); } System.out.println("Point H"); } public void GetInput(KeyboardListener kb) { } public void Update() { } public void Draw(Graphics2D g) { g.drawImage(m_image, m_position.x, m_position.y, null); } public Point GetPosition() { return m_position; } public int GetX() { return m_position.x; } public int GetY() { return m_position.y; } public void SetPosition(Point p) { m_position = p; } public void SetX(int x) { m_position.x = x; } public void SetY(int y) { m_position.y = y; } }
line 45 is just the call to this method from another file.
•
•
•
•
The code gets to point F
I tried the method you suggested and I get this exception:
javax.imageio.IIOException: Can't read input file!
Here's the code:
Java Syntax (Toggle Plain Text)
try { System.out.println("Point A"); m_image = ImageIO.read(new File(filename)); System.out.println("Point B"); } catch(IOException e) { System.out.println("Point C"); System.err.println(e); System.out.println("Point D"); System.exit(1); }
Last edited by CoolGamer48; Dec 14th, 2008 at 8:48 pm.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Hmm... apparently Java isn't recognizing the file. But the file is in the same directory as the .java file.
and the message printed by
was "paddle.bmp doesn't exist"
Is putting the file in the .java file's directory not sufficient? Does it have to be in a PATH directory?
Java Syntax (Toggle Plain Text)
$ ls ~/workspace/Pong/src/ Ball.java KeyboardListener.java Main.java Paddle.java Sprite.java paddle.bmp
and the message printed by
Java Syntax (Toggle Plain Text)
if(!(new File(filename)).exists()) { System.out.println(filename+" doesn't exist"); }
Is putting the file in the .java file's directory not sufficient? Does it have to be in a PATH directory?
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
weird, off topic, question are you making Pong or Break Out?
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
alright, so apparently eclipse uses the project's root folder as the working directory by default, so I made the path local to that and it worked. Many thanks.
Oh, and I'm making Pong.
edit:
hmm, well actually my original method still doesn't work.... I'll used the other method for now, but any other ideas why the first one doesn't work?
Oh, and I'm making Pong.
edit:
hmm, well actually my original method still doesn't work.... I'll used the other method for now, but any other ideas why the first one doesn't work?
Last edited by CoolGamer48; Dec 15th, 2008 at 12:28 am.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
You'll save yourself a lot of relative path pain if you use getResource() to load your images: http://java.sun.com/docs/books/tutor...ml#getresource
![]() |
Similar Threads
- Random Images on Refresh (JavaScript / DHTML / AJAX)
- Vista won't load several images (Windows Vista and Windows 7)
- passing values and displaying images in a GUI (Java)
- PHP Dynamic Images Loads Slowly! (PHP)
- Loading images (Java)
- Loading images in AWT (Java)
Other Threads in the Java Forum
- Previous Thread: Netbeans:Importing existing code conundrum...
- Next Thread: java error printing to a text document
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp eclipse error event exception fractal freeze game gameprogramming givemetehcodez graphics gui health html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print problem program programming project qt recursion scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor






