943,812 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1072
  • Java RSS
Dec 14th, 2008
0

Loading Images

Expand Post »
I'm trying to load a bitmap into an Image object using the following code:
Java Syntax (Toggle Plain Text)
  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):
Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Dec 14th, 2008
0

Re: Loading Images

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
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Dec 14th, 2008
0

Re: Loading Images

Sorry about the lines, here's the original file:
Java Syntax (Toggle Plain Text)
  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.

Quote ...
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:
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Dec 14th, 2008
0

Re: Loading Images

Erm... silly question, but are you sure the file exists...?
if you test (new File(filename)).exists(), what does it print?
Reputation Points: 120
Solved Threads: 7
Junior Poster in Training
neilcoffey is offline Offline
53 posts
since Dec 2008
Dec 14th, 2008
0

Re: Loading Images

Hmm... apparently Java isn't recognizing the file. But the file is in the same directory as the .java file.

Java Syntax (Toggle Plain Text)
  1. $ ls ~/workspace/Pong/src/
  2. Ball.java KeyboardListener.java Main.java Paddle.java Sprite.java paddle.bmp

and the message printed by
Java Syntax (Toggle Plain Text)
  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?
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Dec 14th, 2008
0

Re: Loading Images

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.
Reputation Points: 120
Solved Threads: 7
Junior Poster in Training
neilcoffey is offline Offline
53 posts
since Dec 2008
Dec 14th, 2008
0

Re: Loading Images

weird, off topic, question are you making Pong or Break Out?
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Dec 15th, 2008
0

Re: Loading Images

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.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Dec 15th, 2008
0

Re: Loading Images

I have a feeling it's because you're passing in null to the constructor of MediaTracker.
Reputation Points: 120
Solved Threads: 7
Junior Poster in Training
neilcoffey is offline Offline
53 posts
since Dec 2008
Dec 15th, 2008
0

Re: Loading Images

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
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Netbeans:Importing existing code conundrum...
Next Thread in Java Forum Timeline: java error printing to a text document





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC