/** * Image Loader for the Moving Map Navigator Demonstrator Applet * @author Robert J Morton * @version 12 Dec 2007 */ import java.awt.Component; // Java Abstract Windowing Toolkit import java.awt.MediaTracker; // for the new-fangled 1.1 event handling import java.awt.image.BufferedImage; import javax.imageio.*; import java.io.*; // file input/output handling import java.net.*; // for downloading data from the remote server public class imgldr { private static imgldr il; private bs hf; // instance reference to the bs applet private msgpanel mp; // instance reference for message panel private MediaTracker tracker; // to track when the images have been loaded private int ls = 0; // load switch: 0=local file, 1=jar, 2=server private String cb = ""; // URL path to image file private String[] S = {"local directory", "jar file", "remote server"}; private boolean loadingImages = true, // starts loading as soon as constructed 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 imgldr(bs hf, msgpanel mp, int ls, String cb) { this.hf = hf; // instance reference to the bs applet this.mp = mp; // instance of message panel this.ls = ls; // load switch this.cb = cb; // URL path to image file /* 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); bs.IMG1 = getImg("map1.png"); // try to load the image file if(bs.IMG1 != null) tracker.addImage(bs.IMG1,0); bs.IMG2 = getImg("map2.png"); // try to load the image file if(bs.IMG2 != null) tracker.addImage(bs.IMG1,0); bs.IMG3 = getImg("map3.png"); // try to load the image file if(bs.IMG3 != null) tracker.addImage(bs.IMG1,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 mp.showMsg("Loading images...", false); loadFTT = false; // avoid repeating display every cycle } try {tracker.waitForAll();} // keep checking the tracker for a signal /* If the tracker does not say anything, return without servicing the app's run() thread, otherwise, will drop through when tracker gives a completion signal. */ catch (InterruptedException e) { return true; // image loading not yet finished } return false; // tracker says that images are loaded } // 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) { mp.showMsg("Error: couldn't load images.", true); 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(); mp.showMsg("Images Loaded.", false); } 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(bs.IMG1, 0); tracker.removeImage(bs.IMG2, 0); tracker.removeImage(bs.IMG3, 0); tracker = null; } } boolean TrackerError() {return trackerError;} }