Example usage for java.awt MediaTracker isErrorID

List of usage examples for java.awt MediaTracker isErrorID

Introduction

In this page you can find the example usage for java.awt MediaTracker isErrorID.

Prototype

public synchronized boolean isErrorID(int id) 

Source Link

Document

Checks the error status of all of the images tracked by this media tracker with the specified identifier.

Usage

From source file:MainClass.java

public void load() throws MalformedURLException {
    URL url = new URL("image address");
    Image im = Toolkit.getDefaultToolkit().getImage(url);

    MediaTracker mt = new MediaTracker(this);
    mt.addImage(im, 0);//w  w w.j a v a  2s.c  o  m
    try {
        mt.waitForID(0);
    } catch (InterruptedException e) {
        System.err.println("Unexpected interrupt in waitForID!");
        return;
    }
    if (mt.isErrorID(0)) {
        System.err.println("Couldn't load image file " + url);
        return;
    }
}

From source file:MainClass.java

public void load() throws MalformedURLException {
    URL url = new URL("image address");
    Toolkit t = Toolkit.getDefaultToolkit();
    Image im = t.getImage(url);/*from   www  . j a  v  a2 s. c  o  m*/

    MediaTracker mt = new MediaTracker(this);
    mt.addImage(im, 0);
    try {
        mt.waitForID(0);
    } catch (InterruptedException e) {
        System.err.println("Unexpected interrupt in waitForID!");
        return;
    }
    if (mt.isErrorID(0)) {
        System.err.println("Couldn't load image file " + url);
        return;
    }
}

From source file:ImageView.java

public void loadImage() {

    URL url = getClass().getResource(fileName);
    im = Toolkit.getDefaultToolkit().getImage(url);

    // ----- This part omitted from course notes for brevity -----
    // Use a MediaTracker to show the "best"? way of waiting
    // for an image to load, and how to check for errors.
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(im, 0);/*from ww  w .  j  a va 2 s .  c  om*/
    try {
        mt.waitForID(0);
    } catch (InterruptedException e) {
        System.err.println("Unexpected interrupt in waitForID!");
        return;
    }
    if (mt.isErrorID(0)) {
        System.err.println("Couldn't load image file " + fileName);
        return;
    }

    // Now that we know the image has been loaded,
    // it is safe to take its width and height.
    // ----- End of part omitted from course notes for brevity -----
    width = im.getWidth(this);
    height = im.getHeight(this);
    setSize(width, height);
}

From source file:net.sradonia.gui.SplashScreen.java

/**
 * Creates a new splash screen.//from w  w w. j a v  a  2 s .c  o  m
 * 
 * @param image
 *            the image to display
 * @param title
 *            the title of the window
 */
public SplashScreen(Image image, String title) {
    log.debug("initializing splash screen");

    if (image == null)
        throw new IllegalArgumentException("null image");

    // create the frame
    window = new JFrame(title) {
        private static final long serialVersionUID = 2193620921531262633L;

        @Override
        public void paint(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.drawImage(splash, 0, 0, this);
        }
    };
    window.setUndecorated(true);

    // wait for the image to load
    MediaTracker mt = new MediaTracker(window);
    mt.addImage(image, 0);
    try {
        mt.waitForID(0);
    } catch (InterruptedException e1) {
        log.debug("interrupted while waiting for image loading");
    }

    // check for loading errors
    if (mt.isErrorID(0))
        throw new IllegalArgumentException("couldn't load the image");
    if (image.getHeight(null) <= 0 || image.getWidth(null) <= 0)
        throw new IllegalArgumentException("illegal image size");

    setImage(image);

    window.addWindowFocusListener(new WindowFocusListener() {
        public void windowGainedFocus(WindowEvent e) {
            updateSplash();
        }

        public void windowLostFocus(WindowEvent e) {
        }
    });

    timer = new SimpleTimer(new Runnable() {
        public void run() {
            log.debug(timer.getDelay() + "ms timeout reached");
            timer.setRunning(false);
            setVisible(false);
        }
    }, 5000, false);
    timeoutActive = false;
}