Mustafa
Mustafa

Reputation: 590

Should i use null for the ImageObserver parameter of the Graphics.drawImage() function?

I have the following variables: Image avatar; and URL url;

In the constructor, I set:

this.url = new URL("http://www.robottiger.com/user.PNG");

and

this.avatar = ImageIO.read(url);

Then in..

public void paint (Graphics g)

..is it correct to use the following?

g.drawImage(avatar, 20, 410, null);

Or should the null be this instead?

Upvotes: 1

Views: 3223

Answers (4)

Nazir Ahmmed
Nazir Ahmmed

Reputation: 1

If you're loading Image across the network (not from the file system, or constructed by program) and not explicitly waiting to load, you need to use an ImageObserver to make sure to completely draw an Image. Other than that- just use null. It should be fine.

Upvotes: 0

Andrew Thompson
Andrew Thompson

Reputation: 168825

In contrast to the other two replies, I recommend that if you have an ImageObserver, use it and make no presumptions as to whether the image was loaded synchronously (e.g. ImageIO.read(URL)), loaded asynchronously (e.g. Toolkit.createImage(URL)) or generated in memory.

Upvotes: 2

Devan Somaia
Devan Somaia

Reputation: 645

The constructor you are referring to for drawImage() is as follows:

public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer)

The fourth argument is just the image observer, which is the object to be notified as more of the image is converted. so it is fine for it to be null.

Upvotes: 2

elias
elias

Reputation: 15490

The 4th argument is the observer, the object to be notified as more of the image is converted. It can perfectly be null, assuming that is really only useful if you're fetching the Image parameter asynchronously.

Upvotes: 3

Related Questions