Java Image Wait waitImage(Image image)

Here you can find the source of waitImage(Image image)

Description

wait Image

License

Apache License

Declaration

public static void waitImage(Image image) 

Method Source Code

//package com.java2s;
/**/* w w w .j  av a2s  . c  o  m*/
 * 
 * Copyright 2008 - 2009
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * @project loon
 * @author cping
 * @email?javachenpeng@yahoo.com
 * @version 0.1
 */

import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Panel;

import java.awt.Toolkit;

import java.awt.image.BufferedImage;

public class Main {
    final static public Toolkit toolKit = Toolkit.getDefaultToolkit();
    private static Panel context = new Panel();

    public static void waitImage(Image image) {
        if (image == null) {
            return;
        }
        if (image instanceof BufferedImage) {
            return;
        }
        MediaTracker mediaTracker = null;
        try {
            if (context != null) {
                mediaTracker = new MediaTracker(context);
                mediaTracker.addImage(image, 0);
                if ((mediaTracker.statusID(0, true) & MediaTracker.ERRORED) != 0) {
                    throw new Exception();
                }
            }
        } catch (Exception e) {
            if (mediaTracker != null) {
                mediaTracker.removeImage(image, 0);
                mediaTracker = null;
            }
        }
        waitImage(100, image);
    }

    private static void waitImage(int delay, Image image) {
        try {
            for (int i = 0; i < delay; i++) {
                if (toolKit.prepareImage(image, -1, -1, null)) {
                    return;
                }
                Thread.sleep(delay);
            }
        } catch (Exception e) {
        }
    }
}

Related

  1. waitForImage(Image image, Component component)
  2. waitForImage(java.awt.Image image)
  3. waitForImage(java.awt.Image image)
  4. waitForImageData(Image image, Component comp)
  5. waitImage(Image i)
  6. wallPaper(Component comp, Graphics g, Image image)