/** * Image Loader for the HF Broadcast Receiver 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 hfbrx hf; // instance reference to the hfbrx_swing applet private messagespanel sm; // instance reference for message panel private MediaTracker tracker; // to track when the images have been loaded private boolean loadingImages = true, 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 = 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"}; imgldr(hfbrx hf, messagespanel sm, int ls, String cb) { this.hf = hf; // instance reference to the movmap applet this.sm = sm; // 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); hfbrx.IMG1 = getImg("map.png"); // try to load the image file if(hfbrx.IMG1 != null) tracker.addImage(hfbrx.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 sm.showMsg("Loading image from server...", 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 thru when tracker gives a completion signal. */ } 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) { sm.showMsg("Error: couldn't load image.", true); errorFTT = false; } } /* if tracker has completed its task, set flag to indi- cate 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(); sm.showMsg("Image Loaded.", false); } return false; } /* If this instance of the Media Tracker still exists, remove (de-register) the image from the tracker then destroy (garbage) the tracker. */ void killTracker() { if(tracker != null) { tracker.removeImage(hfbrx.IMG1,0); tracker = null; } } boolean TrackerError() {return trackerError;} }