/** * Image Loader for the Plink 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 * JFramed Sat 04 Mar 2017 12:03:33 BRT */ import java.awt.*; // abstract window tool kit for applet import java.awt.MediaTracker; // for the new-fangled 1.1 event handling import javax.imageio.*; import java.awt.image.BufferedImage; import java.io.*; // file input/output handling import java.net.*; // for downloading data from the remote server public class imgldr { private address hf; // instance reference to search applet private MediaTracker tracker; // to track when 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(address hf, int ls, String cb) { this.hf = hf; // instance reference to the movmap app 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..."); hf.img = getImg("intapp.png"); // try to load the image file if(hf.img != null) { tracker.addImage(hf.img,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 finished() { // returns true when image loading has finished if(tracker == null) return true; if(loadingImages()) return false; if(trackingImages()) return false; return true; } private boolean loadingImages() { if(loadFTT) { // if this is the first time through here System.out.println("Tracking progress..."); 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) { System.out.println("Error: could not finish loading image."); System.out.println("" + e); return true; } return false; } // Returns true when image loading has terminated. private boolean trackingImages() { /* 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) { System.out.println("Error: couldn't load image."); errorFTT = false; } } /* if tracker has completed its task, set flag to indicate that image loading has terminated successfully. */ if(ts == MediaTracker.COMPLETE) { loadingImages = false; } } /* else, the image loading process has just finished, so dispense with this instance of the Media Tracker and refresh message display if not already done. */ else killTracker(); 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(hf.img,0); tracker = null; } } boolean TrackerError() {return trackerError;} }