Loading Images

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Loading Images

 
0
  #1
Dec 14th, 2008
I'm trying to load a bitmap into an Image object using the following code:
  1. System.out.println("Point A");
  2. System.out.println("Point B");
  3. m_image = Toolkit.getDefaultToolkit().getImage(getClass().getResource(filename));
  4. System.out.println("Point C");
  5. MediaTracker mt = new MediaTracker(null);
  6. System.out.println("Point D");
  7. mt.addImage(m_image, 0);
  8. System.out.println("Point E");
  9. try
  10. {
  11. System.out.println("Point F");
  12. mt.waitForID(0);
  13. System.out.println("Point G");
  14. }
  15. catch (InterruptedException ie)
  16. {
  17. System.err.println(ie);
  18. System.exit(1);
  19. }
  20. System.out.println("Point H");
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):
  1. Exception in thread "main" java.lang.NullPointerException
  2. at java.awt.ImageMediaEntry.getStatus(MediaTracker.java:908)
  3. at java.awt.MediaTracker.statusID(MediaTracker.java:705)
  4. at java.awt.MediaTracker.waitForID(MediaTracker.java:653)
  5. at java.awt.MediaTracker.waitForID(MediaTracker.java:622)
  6. at Sprite.SetImage(Sprite.java:26)
  7. 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...".
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 413
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Loading Images

 
0
  #2
Dec 14th, 2008
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 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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: Loading Images

 
0
  #3
Dec 14th, 2008
Sorry about the lines, here's the original file:
  1. import java.awt.*;
  2.  
  3. public abstract class Sprite
  4. {
  5. private Image m_image;
  6. private Point m_position;
  7.  
  8. public Sprite(int x, int y)
  9. {
  10. m_position = new Point(x, y);
  11. }
  12.  
  13. public void SetImage(String filename)
  14. {
  15. System.out.println("Point A");
  16. System.out.println("Point B");
  17. m_image = Toolkit.getDefaultToolkit().getImage(getClass().getResource(filename));
  18. System.out.println("Point C");
  19. MediaTracker mt = new MediaTracker(null);
  20. System.out.println("Point D");
  21. mt.addImage(m_image, 0);
  22. System.out.println("Point E");
  23. try
  24. {
  25. System.out.println("Point F");
  26. mt.waitForID(0);
  27. System.out.println("Point G");
  28. }
  29. catch (InterruptedException ie)
  30. {
  31. System.err.println(ie);
  32. System.exit(1);
  33. }
  34. System.out.println("Point H");
  35. }
  36.  
  37.  
  38. public void GetInput(KeyboardListener kb)
  39. {
  40. }
  41.  
  42. public void Update()
  43. {
  44. }
  45.  
  46. public void Draw(Graphics2D g)
  47. {
  48. g.drawImage(m_image, m_position.x, m_position.y, null);
  49. }
  50.  
  51. public Point GetPosition()
  52. {
  53. return m_position;
  54. }
  55.  
  56. public int GetX()
  57. {
  58. return m_position.x;
  59. }
  60.  
  61. public int GetY()
  62. {
  63. return m_position.y;
  64. }
  65.  
  66. public void SetPosition(Point p)
  67. {
  68. m_position = p;
  69. }
  70.  
  71. public void SetX(int x)
  72. {
  73. m_position.x = x;
  74. }
  75.  
  76. public void SetY(int y)
  77. {
  78. m_position.y = y;
  79. }
  80. }

line 45 is just the call to this method from another file.

The code gets to point F
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:
  1. try
  2. {
  3. System.out.println("Point A");
  4. m_image = ImageIO.read(new File(filename));
  5. System.out.println("Point B");
  6. }
  7. catch(IOException e)
  8. {
  9. System.out.println("Point C");
  10. System.err.println(e);
  11. System.out.println("Point D");
  12. System.exit(1);
  13. }
I also changed m_image from an object of Image to BufferedImage
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...".
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 53
Reputation: neilcoffey will become famous soon enough neilcoffey will become famous soon enough 
Solved Threads: 6
neilcoffey neilcoffey is offline Offline
Junior Poster in Training

Re: Loading Images

 
0
  #4
Dec 14th, 2008
Erm... silly question, but are you sure the file exists...?
if you test (new File(filename)).exists(), what does it print?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: Loading Images

 
0
  #5
Dec 14th, 2008
Hmm... apparently Java isn't recognizing the file. But the file is in the same directory as the .java file.

  1. $ ls ~/workspace/Pong/src/
  2. Ball.java KeyboardListener.java Main.java Paddle.java Sprite.java paddle.bmp

and the message printed by
  1. if(!(new File(filename)).exists())
  2. {
  3. System.out.println(filename+" doesn't exist");
  4. }
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?
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 53
Reputation: neilcoffey will become famous soon enough neilcoffey will become famous soon enough 
Solved Threads: 6
neilcoffey neilcoffey is offline Offline
Junior Poster in Training

Re: Loading Images

 
0
  #6
Dec 14th, 2008
The location of the source file has nothing to do with it. Either make sure the file is in the working directory from which you run the program (if you're running from the command line, that's the directory you're in when you launch the program), or specify a complete path.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 413
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Loading Images

 
0
  #7
Dec 14th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: Loading Images

 
0
  #8
Dec 15th, 2008
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?
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...".
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 53
Reputation: neilcoffey will become famous soon enough neilcoffey will become famous soon enough 
Solved Threads: 6
neilcoffey neilcoffey is offline Offline
Junior Poster in Training

Re: Loading Images

 
0
  #9
Dec 15th, 2008
I have a feeling it's because you're passing in null to the constructor of MediaTracker.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Loading Images

 
0
  #10
Dec 15th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 598 | Replies: 9
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC