/** * Image Loader for the Search Engine App * @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 * JFramed Sat 04 Mar 2017 12:03:33 BRT */ import java.awt.*; // abstract window tool kit for applet import java.io.*; // file input/output handling import java.net.*; // for downloading data from the remote server import javax.imageio.*; import java.awt.MediaTracker; // for the new-fangled 1.1 event handling import java.awt.image.BufferedImage; public class imgldr { private search hf; // instance reference to the search applet private img2panel img2; // instance reference for message panel private MediaTracker tracker; // to track when the images have been loaded private boolean loadingImages = true, // true while in the process of loading images trackerError = false, // true indicates that tracker encountered an error loadFTT = true, // to avoid an erase and repaint of the loading of errorFTT = true; // load-error message once per refresh cycle private int ls; // load switch 0=file, 1=jar, 2=server private String cb; // code base [URL path to image files on server] private String[] S = {"local directory", "jar file", "remote server"}; imgldr(search hf, img2panel img2, int ls, String cb) { this.hf = hf; // instance reference to the movmap app this.img2 = img2; // instance of message panel this.ls = ls; // load switch this.cb = cb; // code base [URL path] /* Create a media tracker and register images with it. Media tracker monitors the progress of loading process for each image files. */ tracker = new MediaTracker(hf); System.out.println("Loading images..."); img2.showMsg("Loading images...", false); search.IMG0 = getImg("map-s0.png"); // try to load the image file if(search.IMG0 != null) tracker.addImage(search.IMG0,0); search.IMG1 = getImg("map-s1.png"); // try to load the image file if(search.IMG1 != null) tracker.addImage(search.IMG1,0); search.IMG2 = getImg("map-s2.png"); // try to load the image file if(search.IMG2 != null) tracker.addImage(search.IMG2,0); search.IMG3 = getImg("map-s3.png"); // try to load the image file if(search.IMG3 != null) { tracker.addImage(search.IMG3,0); } } private BufferedImage getImg(String F) { BufferedImage I = null; try { switch(ls) { case 0: I = ImageIO.read(new File(F)); break; case 1: I = ImageIO.read(getClass().getResourceAsStream(F)); break; case 2: I = ImageIO.read(new URL(cb + F)); } } catch (IOException e) { System.out.println("Couldn't load " + F + " from " + S[ls] + "."); System.out.println(e); } return I; } boolean awaitingImages() { if(tracker == null) return false; if(loadFTT) { // if this is the first time through here System.out.println("Tracking progress..."); loadFTT = false; // avoid repeating display every cycle } try { /* Keep checking the tracker for a signalIf the tracker does not say anything, return without servicing the app's run() thread, otherwise, will drop thru when tracker gives a completion signal. */ tracker.waitForAll(); } catch (InterruptedException e) { return true; } return false; } // Returns true when image loading has terminated. boolean imagesLoaded() { if(tracker == null) return true; /* If the image loading process still running, get the status of the image loading process. */ if(loadingImages) { int ts = tracker.statusAll(false); /* if the loading process has completed with an error, set the image loading failure flag */ if((ts & MediaTracker.ERRORED) != 0) { trackerError = true; /* If this is the first time through on this image loading attempt, show the error message and set the error display flag false to avoid re-displaying the message each pass. */ if(errorFTT) { img2.showMsg("Error: couldn't load image.", true); System.out.println("Error: couldn't load images."); errorFTT = false; } } /* if tracker has completed its task, set flag to set to indicate that image loading has terminated successfully. */ if(ts == MediaTracker.COMPLETE) loadingImages = false; } /* else, image the loading process has just finished, so dispense with this instance of the Media Tracker and refresh message display if not already done. */ else { killTracker(); System.out.println("Images ready."); } return false; } /* If this instance of the Media Tracker still exists, remove (de-register) the image from it and then destroy (garbage) it. */ void killTracker() { if(tracker != null) { tracker.removeImage(search.IMG1, 0); tracker = null; } } boolean TrackerError() {return trackerError;} }