Thursday 14 May 2015

Loading Images from Files (ImageObserver) in Java



Java makes it easy to load images from files located on the server. Security restrictions vary between browsers, but even Netscape, which tends to have the tightest security, allows you to get images from files in the same directory from which the Java classes are loaded. The simple applet shown in Listing 17.32 loads in an image, monitors the loading with an ImageObserver, and then displays the image

Listing 17.32. Showing a GIF image.
import java.awt.*;
import java.applet.Applet;
import java.awt.image.*;


public class imagetest extends Applet implements ImageObserver, Runnable{
    Image the_picture;
    boolean image_ready;
    Thread the_thread;

    public void init()
    {
        //we'll use this flag to see when the image is ready to use
        image_ready = false;
        //get the image from the same location as the
        //HTML document this applet is on
        the_picture = getImage(getDocumentBase(),"test.gif");

    }
    //overriding this method allows you to monitor the
    //status of the loading of the image
    public boolean imageUpdate(Image img,int status, int x,
                        int y, int width, int height){

        if ((status & ALLBITS) == 0) {
            //Monitor the load status
            System.out.println("Processing image " + x + " " +
                y + " width, height " + width + " " + height);
             return true;
        } else {
            System.out.println("Image fully read in");
            return false;
        }
    }

    public void start() {
        if (the_thread == null) {
            the_thread = new Thread(this);
            the_thread.start();
        }
    }
    public void stop() {
        the_thread.stop();
    }

    public void run() {
        //Give the thread that's loading the image more cycles by minimizing the
        //priority of the applet's animation thread
        the_thread.setPriority(Thread.MIN_PRIORITY);
        //cycle while you're waiting for the image to load. You could put in a
        //message telling the user what's going on as well
        while (!image_ready) {
            repaint();
            try {
                the_thread.sleep(50);
            } catch(InterruptedException e) {};
        }
    }
    public void paint(Graphics g)
    {
        //Draw the image. The "this" assigns the applet
        //as the image observer for
        //this drawing.
        image_ready = g.drawImage(the_picture,0,0,this);
    }
}

Notice that the applet itself implements the ImageObserver interface so that when an ImageObserver is called for, as in the drawImage method call, this is used. The only other change you have to make to your applet is to override the imageUpdate method with one of your own to track the image-loading process. Although the behavior of this seems to vary a bit between platforms (probably due to differences in the way thread priorities are handled), you can use it to see when an image is ready to be displayed. But there is a better way, using the MediaTracker class, which you'll see in the next section.


No comments:

Post a Comment