Java Image Wait waitForImage(java.awt.Image image)

Here you can find the source of waitForImage(java.awt.Image image)

Description

Waits until image is fully loaded, so ready for drawing.

License

Open Source License

Declaration

private static void waitForImage(java.awt.Image image) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.//from w ww .  j  av a 2 s. c om
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import java.awt.image.ImageObserver;

public class Main {
    /**
     * Waits until image is fully loaded, so ready for drawing.
     */
    private static void waitForImage(java.awt.Image image) {
        BufferedImage bufferedImage = new BufferedImage(1, 1,
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = bufferedImage.createGraphics();
        // prepare observer
        final Object done = new Object();
        ImageObserver imageObserver = new ImageObserver() {
            public boolean imageUpdate(java.awt.Image img, int flags,
                    int x, int y, int width, int height) {
                if (flags < ALLBITS) {
                    return true;
                } else {
                    synchronized (done) {
                        done.notify();
                    }
                    return false;
                }
            }
        };
        // draw Image with wait
        synchronized (done) {
            boolean completelyLoaded = g2.drawImage(image, 0, 0,
                    imageObserver);
            if (!completelyLoaded) {
                while (true) {
                    try {
                        done.wait(0);
                        break;
                    } catch (InterruptedException e) {
                    }
                }
            }
        }
        // clean up
        g2.dispose();
    }
}

Related

  1. waitForImage(Image image)
  2. waitForImage(Image image)
  3. waitForImage(Image image, Component c)
  4. waitForImage(Image image, Component component)
  5. waitForImage(Image image, Component component)
  6. waitForImage(java.awt.Image image)
  7. waitForImageData(Image image, Component comp)
  8. waitImage(Image i)
  9. waitImage(Image image)