Class BlockingImageObserver

java.lang.Object
org.bzdev.imageio.BlockingImageObserver
All Implemented Interfaces:
ImageObserver

public class BlockingImageObserver extends Object implements ImageObserver
Class providing an image observer with a method that blocks until an image, its height and/or width, or some properties of the image are available.

Users should construct an instance of this object, pass it to a method that expects an image observer and that retrieves an image, and then call the method waitUntilDone(), which will block until the image is available, an error occurs, or the thread is interrupted. Instances of BlockingImageObserver must be used with only a single image.

An alternative is to use a media tracker. This class provides a simpler API: the Java classes that take an image obsever as an argument may return before the image is completely loaded. An example of usage is the following:


    Image image = ...;
    BlockingImageObserver bio =
         new BlockingImageObserver(true, true, true, true);
    int width = image.getWidth(bio);
    bio.waitUntilDone();
    // repeat in case the image was not there on the first
    // call to getWidth()
    width = image.getWidth(bio);
    int height = image.getHeight(bio);
 

This class should not be used when blocking would noticeably impact performance. That could be an issue if an image is downloaded over a computer network, but much less so if the image is stored locally. Blocking is also less likely to be an issue when a computation is compute bound and the additional time due to blocking is small compared to the CPU time.

  • Constructor Details

    • BlockingImageObserver

      public BlockingImageObserver(boolean needWidth, boolean needHeight, boolean needProperties, boolean needImage)
      Constructor.
      Parameters:
      needWidth - true if waitUntilDone() should return when the width of an image is available; false if the width is not needed
      needHeight - true if waitUntilDone() should return when the height of an image is available; false if the height is not needed
      needProperties - true if waitUntilDone() should return when the properties of an image are available; false if the properties are not needed
      needImage - true if waitUntilDone() should return when the complete image is available; false if the image itself is not needed
  • Method Details

    • imageUpdate

      public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
      This method is called when information about an image which was previously requested using an asynchronous interface becomes available. Asynchronous interfaces are method calls such as getWidth(ImageObserver) and drawImage(img, x, y, ImageObserver) which take an ImageObserver object as an argument. Those methods register the caller as interested either in information about the overall image itself (in the case of getWidth(ImageObserver)) or about an output version of an image (in the case of the drawImage(img, x, y, [w, h,] ImageObserver) call).

      This method should return true if further updates are needed or false if the required information has been acquired. The image which was being tracked is passed in using the img argument. Various constants are combined to form the infoflags argument which indicates what information about the image is now available. The interpretation of the x, y, width, and height arguments depends on the contents of the infoflags argument.

      The infoflags argument should be the bitwise inclusive OR of the following flags: WIDTH, HEIGHT, PROPERTIES, SOMEBITS, FRAMEBITS, ALLBITS, ERROR, ABORT.

      Specified by:
      imageUpdate in interface ImageObserver
      Parameters:
      img - the image being observed.
      infoflags - the bitwise inclusive OR of the following flags: WIDTH, HEIGHT, PROPERTIES, SOMEBITS, FRAMEBITS, ALLBITS, ERROR, ABORT.
      x - the x coordinate.
      y - the y coordinate.
      width - the width.
      height - the height.
      Returns:
      false if the infoflags indicate that the image is completely loaded; true otherwise.
      See Also:
    • waitUntilDone

      public void waitUntilDone() throws IOException, InterruptedException
      Wait until this image observer has been notified that all the requested quantities are available.
      Throws:
      IOException - - if an IO error has occurred
      InterruptedException - - if some thread has interrupted the current thread before or while this method was waiting for a notification (the interrupted status of the current thread will be cleared when this exception is thrown)