/** * Image Loader for the Search Engine Applet and Search Engine Applet * @author Robert J Morton * @version 12 Dec 2007, 10 April 2012 * Modified for encapsulation in jar file Thu 02 Feb 2017 12:51:07 BRST */ 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 search hf; // instance reference to the search applet private img1panel img1; // instance reference for summary panel private img2panel img2; // instance reference for message panel private img3panel img3; // instance reference for summary number panel // To track the progress and status of the image loading process. private MediaTracker tracker; private boolean // TRUE INDICATES THAT: loadingImages = true, // the process of loading images is running trackerError = false, // the tracker encountered an error // These two flags are to avoid the repeated erase and repaint of the loadFTT = true, // 'loading of in progress' message errorFTT = true; // 'loading-error' message // IMAGE LOADER CONSTRUCTOR imgldr(search hf, img1panel img1, img2panel img2, img3panel img3) { this.hf = hf; // instance reference to the movmap applet this.img1 = img1; // instance of message panel this.img2 = img2; // instance of message panel this.img3 = img3; // instance of message panel // create a media tracker and register THE 4 images with it tracker = new MediaTracker(hf); /* The boolean flag 'hf.JarredImages' is a switch received either from the terminal command line from which the search engine appl- ication is initiated or from an applet tag parameter within the HTML page within which the search engine applet is embedded. */ if(hf.JarredImages) { // load images from the applet's own jar file hf.IMG0 = hf.getImage(getClass().getResource("map-s0.png")); hf.IMG1 = hf.getImage(getClass().getResource("map-s1.png")); hf.IMG2 = hf.getImage(getClass().getResource("map-s2.png")); hf.IMG3 = hf.getImage(getClass().getResource("map-s3.png")); } else { // load images directly from the remote server hf.IMG0 = hf.getImage(hf.getDocumentBase(),"map-s0.png"); hf.IMG1 = hf.getImage(hf.getDocumentBase(),"map-s1.png"); hf.IMG2 = hf.getImage(hf.getDocumentBase(),"map-s2.png"); hf.IMG3 = hf.getImage(hf.getDocumentBase(),"map-s3.png"); } tracker.addImage(hf.IMG0,0); // Start a media tracker to monitor the tracker.addImage(hf.IMG1,0); // progress of the loading process for tracker.addImage(hf.IMG2,0); // each of the four image files. tracker.addImage(hf.IMG3,0); } boolean awaitingImages() { // Display the 'Loading images' message on the first pass only. if(loadFTT) { img2.showMsg("Loading images from server...",false); loadFTT = false; } /* Keep checking the tracker for a signal. If no signal is received during this pass, 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 } // RETURNS 'true' WHEN IMAGE LOADING HAS TERMINATED boolean imagesLoaded() { if(tracker == null) // if tracker is dead return true; // exit immediately /* If the image-loading process is still running, get the status of the image-loading process. */ if(loadingImages) { int ts = tracker.statusAll(false); /* If the media tracker detects an image loading error, set the loading error flag then display the error message once only. The errorFTT flag avoids the message being redisplayed on every pass of this method. */ if((ts & MediaTracker.ERRORED) != 0) { trackerError = true; if(errorFTT) { img2.showMsg("Error: couldn't load image.",true); errorFTT = false; } } /* if tracker has completed its task, set flag to indicate that image loading has terminated. */ if(ts == MediaTracker.COMPLETE) { loadingImages = false; } /* Else (if no longer in the process of loading images) then destroy this instance of the media tracker and inform the message panel that it can now display its background image. */ } else { killTracker(); img2.setImagesLoaded(); img2.showMsg("Images Loaded.",false); } return false; // indicates that the loading process is still running } /* DESTROY THE IMAGE TRACKER WHEN ALL THE IMAGES HAVE BEEN LOADED If the image tracker still has a non-null reference then it still exists. So it is necessary to remove or de-register each of the 4 images in turn from the tracker and then set the tracker's reference to null so that its code and data get wiped by Java's automatic garbage collector. */ void killTracker() { if(tracker != null) { tracker.removeImage(hf.IMG0,0); tracker.removeImage(hf.IMG1,0); tracker.removeImage(hf.IMG2,0); tracker.removeImage(hf.IMG3,0); tracker = null; } } boolean TrackerError() { return trackerError; } }