What is an ImageObserver?
First of all, ImageObserver
is an interface. According to docs:
An asynchronous update interface for receiving notifications about Image information as the Image is constructed.
In other words, it's an object-oriented way to use Images which can be modified before fully created. Method imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
is called any time the image is modified. It returns true
if it wants to be notified about further changes and false
otherwise. This method can be used to force size, resolution, colours etc. It also gives you some control of the errors (ERROR
flag). For more info see this.
The observer may also process important information about the image - for example if we're drawing an image on the screen and change it to a bigger one before the rendering is complete, there has to be a way to inform whatever we're drawing on that the dimension has changed (allocate more space) and that it has to deal with the changes. The fact that ImageObserver
is asynchronous is extremely important in that case.
ImageObserver
is an interface that has methods for handling notification of state of image loading. It can use this for redisplay as needed. JFrame
and Applet
both implement ImageObserver
interface.
To keep users informed regarding the loading of an image
ImageObserver
interface – Enables the monitoring of the loading process so that users can be informed and the image can be used asap once it is loaded.Loading an image asynchronously – how to know when the image is ready.
An image is ready –
getImage()
method returns, long before anything is known about the image.imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
Note:
java.awt.Component
implementsImageObserver
, all the subclasses do as well!g.drawImage(imge, 0,0, this)
-- this refers to theImageObserver
instance.imageUpdate()
– Called by theImageObserver
whenever necessary. You do not call it explicitly!- If the image is complete, returns
false
. - If the image is not complete and needs to be updated, returns
true
.
- If the image is complete, returns
ImageObserver.ALLBITS = 32
Various constants are combined to form the
infoflags
argument, which indicates whether all information is available or not.
Take a look at this and Oreilly's explanation.