We help IT Professionals succeed at work.

getImage problem

milchew
milchew asked
on
 So, I want to load image file (gif) and draw it on Graphic object with drawImage. When i write in the "paint(Graphics g)" function of the applet:

Image img = getImage(getCodeBase(), "myimage.gif"):
g.drawImage(img, 0,0, this);

there is no problem. But if I try to make my own class with method "draw(Graphics g)" :

class TestImage {

    void draw(Graphics g) {
        Image img = getImage(getCodeBase(),"myimage.gif");
        g.drawImage(img,0,0,this);
    }
}
and call this method from the applet's "paint":

paint(Graphics g) {
    TestImage obj = new TestImage();
    obj.draw(g);
}

there is a problem and I becom NullPointerException
 
Please help...
Comment
Watch Question

Commented:
Can you show me the stack trace
CERTIFIED EXPERT
Top Expert 2004
Commented:
Images are loaded out of process...

You need to attatch a media tracker to this image.

and I wouldn't load the image inside the draw() routine...

Load the image in the init(), and draw it in draw() if it isn't equal to null...

So, you get:

--------------------------------------

class YourApplet extends Applet
{
  class TestImage
  {
    Image img ;
    public TestImage()
    {
      img = getImage(getCodeBase(),"myimage.gif");
      MediaTracker mt = new MediaTracker( this ) ;
      mt.addImage( img, 1 ) ;
      mt.waitForAll();
    }

    void draw(Graphics g)
    {
      if( img != null )
        g.drawImage(img,0,0,this);
    }
  }

  TestImage obj = null ;

  init()
  {
    obj = new TestImage();
  }

  paint(Graphics g)
  {
    if( obj != null )
      obj.draw(g);
  }
}

-------------------------------------

Hope this helps, and isn't wrong ;-)

If it is wrong, I am sure others will point you in a more correct direction :-)

Tim.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Where is your TestImage class 'located'.
I'm surprised it compiles as it can't find getCodeBase(), getImage() and it's not an ImageObserver.
Milchew

Use getGraphics inside the Class TestImage and use it.

Shyam
CERTIFIED EXPERT
Top Expert 2004

Commented:
objects...if it is an inner class, it will work won't it?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Tim>  I wouldn't think so, because the following class expects this to implement ImageObserver, which the inner class isn't.

g.drawImage(img,0,0,this);


CERTIFIED EXPERT
Top Expert 2004

Commented:
Oh yes...hehe how did I miss that? =)

Author

Commented:
 So, TestClass is not inner class.
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- To be PAQ'ed and points refunded

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
CERTIFIED EXPERT
Top Expert 2004

Commented:
I'm confused as to why some people get points refunded, and some don't...

Not arguing, just wondering :-)
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
 Hello Tim, I decided to recommend a refund since noone posted after the asker's last commend, which means that the asker still didn't have a clear view of what's going on and the experts abandoned the question. I just thought it would be fairer to refund the points since a) no expert clarified what's going on and b) the asker didn't get the answer he/she wanted.

  On the other hand I always recommend NO refunds if the asker has abandoned the question. :-)
CERTIFIED EXPERT
Top Expert 2004

Commented:
Ahhh :-)

Cool :-)

Thanks for explaining it to me :-)  Seems fair :-)

Keep up the good work fella!

Tim
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> noone posted after the asker's last comment

That comment didn't really make much sense.
And (s)he didn't reply to any of our q's.

Refunding is fine though :)
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
 Tim thx :-)

  Objects:

> That comment didn't really make much sense.

  Well if something does not make much sense to me I always ask back for clarifications :-)

  Anyway this is just my *personal* view to the matter and it's not necessarily correct. I guess objects knows how it works more or less since he has participated to that kind of clean up before.

  The point of doing that is also a kind of "ping" to the question. We hope that the asker will respond back to clarify things.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.