/** * Image Loader for the HF Broadcasr Receiver Demonstrator Applet * @author Robert J Morton YE572246C * @version 12 Dec 2007 */ import java.awt.*; // abstract window tool kit for applet import java.awt.MediaTracker; // for the new-fangled 1.1 event handling public class imgldr { private hfbrx_swing hf; // instance reference to the hfbrx_swing applet private messagespanel sm; // instance reference for message panel // to track when the progress of image loading private MediaTracker tracker; private boolean loadingImages = true, trackerError = false, // true = tracker encountered an error loadFTT = true, // To avoid an erase and repaint of loading and errorFTT = true; // load-error messages every refresh cycle. imgldr(hfbrx_swing hf, messagespanel sm) { this.hf = hf; // instance reference to the movmap applet this.sm = sm; // instance of message panel //create a media tracker and register images with it tracker = new MediaTracker(hf); if(hf.JarredImages) // if loading images from the applet's own jar file hf.IMG1 = hf.getImage(getClass().getResource("map.png")); else // else we're loading images from remote server hf.IMG1 = hf.getImage(hf.getDocumentBase(),"map.png"); // add the images to the tracker's watch list tracker.addImage(hf.IMG1,0); } boolean awaitingImages() { if(loadFTT) { // if this is the first time through here sm.showMsg("Loading image from server...", false); loadFTT = false; // avoid repeating display every cycle } /* Keep checking the tracker for a signal. If the tracker does not say anything, return without servicing the applet's thread. */ try { tracker.waitForAll(); } catch (InterruptedException e) { return true; } return false; // will drop thru when tracker gives a completion signal } boolean imagesLoaded() { if(tracker == null) // Returns true when image return true; // loading has terminated. if(loadingImages) { // if image loading process still running // Get the current status of the image loading process. int ts = tracker.statusAll(false); // If the image loading process has terminated in an error state if((ts & MediaTracker.ERRORED) != 0) { trackerError = true; // indicates image loading failure if(errorFTT) { // if this is the first time through here sm.showMsg("Error: couldn't load image.", true); errorFTT = false; // avoid repeating display every cycle } } /* if the tracker has completed its task, set flag to indicate that the image loading process has terminated. */ if(ts == MediaTracker.COMPLETE) { loadingImages = false; } } else { // Else, the image loading process has just finished, killTracker(); // so refresh display if this hasn't already been done sm.showMsg("Image Loaded.", false); } return false; } void killTracker() { if(tracker != null) { // if tracker object still exists tracker.removeImage(hf.IMG1,0); // Remove (de-register) the image tracker = null; // from it then kill the tracker } } boolean TrackerError() { return trackerError; } }