What is an ImageObserver?

Troubleshoot picture Troubleshoot · Oct 6, 2013 · Viewed 32.9k times · Source

When you draw an image it requires an image observer. From what I understand so far a BufferedImage is an image observer. But my question is, what defines an image observer and what does it do? I'm quite confused.

Answer

Mateusz picture Mateusz · Oct 6, 2013

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.