Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Java Graphics.drawImage() work and what is the role of ImageObserver

Tags:

How should Java's drawImage() be used? I do not find the JDK documentation very forthcoming. For example all drawImage signatures require an ImageObserver but the documentation for this is not very helpful for new users.

like image 371
peter.murray.rust Avatar asked Nov 05 '09 23:11

peter.murray.rust


People also ask

What is ImageObserver in Java?

An asynchronous update interface for receiving notifications about Image information as the Image is constructed.

What is graphics in Java?

A Graphics object encapsulates state information needed for the basic rendering operations that Java supports. This state information includes the following properties: The Component object on which to draw. A translation origin for rendering and clipping coordinates.


1 Answers

You can get away with Graphics.drawImage(img, x, y, null) [or similar]. The ImageObserver parameter is a callback to inform you of the progress of the draw operation; and is really only useful if you're fetching the Image parameter asynchronously.

To be clearer, if you call drawImage with an incompletely loaded Image it will:

  1. return false (immediately)
  2. draw as much of the Image as possible (all that is loaded)
  3. and, at some future point, call into the ImageObserver when more of the Image is available

Basically, if you're working with in memory Images (either loaded from the file system, or constructed by your program) don't worry about the ImageObserver parameter. If you're loading Images across the network and not explicitly waiting for them to load, you'll need to employ an ImageObserver to make sure "completely" draw an Image.

like image 180
Kevin Montrose Avatar answered Sep 29 '22 11:09

Kevin Montrose