import java.awt.*;

    // A visual component that uses a MediaTracker to monitor
    // image loading. Set a `waiting' cursor while waiting for
    // the images to load.
class CursorImageLoader extends Frame {
    public CursorImageLoader(){
        final int width = 200, height = 200;
        setSize(width,height);
    }

        // Get the images.
    public Image[] retrieveImages(String[] files) {
        final int numImages = files.length;
        Image[] images = new Image[numImages];
        if(numImages > 0){
            // Replace the current cursor to indicate that a wait might be
            // required.
            final Cursor oldCursor = getCursor();
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

            // Use a Toolkit to load the images.
            Toolkit tk = Toolkit.getDefaultToolkit();
            // Use a MediaTracker to monitor the loading.
            MediaTracker tracker = new MediaTracker(this);
            for(int i = 0; i < numImages; i++){
                System.out.println("Loading: "+files[i]);
                images[i] = tk.getImage(files[i]);
                // Give each a separate ID for checking.
                tracker.addImage(images[i],i);
            }
            // Loading initiated, wait a limited time for them to arrive.
            try{
                final long waitingTime = 5*1000;
                tracker.waitForAll(waitingTime);
            }
            catch(InterruptedException e){
                System.out.println("Interrupted.");
            }
            // Restore the previous cursor.
            setCursor(oldCursor);
            System.out.println("Checking.");
            // Check each.
            for(int i = 0; i < numImages; i++){
                if(tracker.checkID(i)){
                    System.out.println("Loaded: "+files[i]);
                }
                else{
                    System.out.println("Failed to load: "+files[i]);
                    images[i] = null;
                }
            }
            if(tracker.isErrorAny()){
                System.out.println("There were errors.");
            }
        }
        return images;
    }
}
