/** * Image Loader for the Moving Map Navigator Demonstrator Applet * @author Robert J Morton * @version 12 Dec 2007 * Extended for sandboxed jar-file operation Wed 01 Feb 2017 12:34:27 BRST */ import java.awt.Component; // Java Abstract Windowing Toolkit import java.awt.MediaTracker; // for the new-fangled 1.1 event handling import java.io.*; // file input/output handling import java.net.*; // for downloading data from remote server import java.awt.image.BufferedImage; import javax.imageio.*; public class imgldr { private movmap mm; // instance reference to the movmap applet private msgpanel mp; // instance reference for message panel private MediaTracker tracker; // to track when the images have been loaded private boolean loadingImages = true, trackerError = false, // true = that tracker encountered an error loadFTT = true, // to avoid erase + repaint of loading message errorFTT = true; // and load-error message once every refresh cycle private int ls, L; // load switch, language switch private String cb, // code base: protocol + file path of image files S[] = {"local directory","jar file","remote server"}, M[][] = { {"Loading images from server...", "Error: couldn't load images.", "Images Loaded." }, {"Carregando imagens do servidor...", "Erro: não foi possível carregar imagens.", "Imagens carregadas." } }; imgldr(movmap mm, msgpanel mp, int ls, int L, String cb) { this.mm = mm; // instance reference to the movmap applet this.mp = mp; // instance of message panel this.ls = ls; // loading switch this.L = L; // language switch this.cb = cb; // codebase URL /* Create a Media Tracker and register the images to be downloaded. The Media tracker monitors the progress of loading process for of each of the four image files. */ tracker = new MediaTracker(mm); movmap.IMG1 = getImg("clouds1.png"); if(movmap.IMG1 != null) { tracker.addImage(movmap.IMG1,0); } movmap.IMG2 = getImg("clouds2.png"); if(movmap.IMG2 != null) { tracker.addImage(movmap.IMG2,0); } movmap.IMG3 = getImg("clouds3.png"); if(movmap.IMG3 != null) { tracker.addImage(movmap.IMG3,0); } movmap.IMG4 = getImg("clouds4.png"); if(movmap.IMG4 != null) { tracker.addImage(movmap.IMG4,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(loadFTT) { // if this is the first time through here mp.showMsg(M[L][0],true,true); loadFTT = false; // avoid repeating display every cycle } // Keep checking the tracker for a completion signal. try { tracker.waitForAll(); } /* If the tracker does not say anything return without servicing the thread. */ catch(InterruptedException e){ return true; } return false; // drops through when tracker gives a completion signal } boolean imagesLoaded() { if(tracker == null) // If tracker has been killed return true; // say that loading has finished if(loadingImages) { // If loading still in progress /* if the loading process has completed but a media tracker error has occurred, indicate an image loading failure */ int ts = tracker.statusAll(false); if((ts & MediaTracker.ERRORED) != 0) { trackerError = true; /* If this is the first time through here, display the appropriate error message and set flag to avoid repeating display of the message every cycle. */ if(errorFTT) { mp.showMsg(M[L][1],false,true); errorFTT = false; } } /* if tracker has completed its task, indicate that image loading has terminated. */ if(ts == MediaTracker.COMPLETE) { loadingImages = false; } } /* Else the image loading process has just finished, so kill the tracker and refresh the message display if not already done. */ else { killTracker(); mp.showMsg(M[L][2],true,true); } return false; } void killTracker() { // remove (de-register) the images from the tracker if(tracker != null) { tracker.removeImage(movmap.IMG1,0); tracker.removeImage(movmap.IMG2,0); tracker.removeImage(movmap.IMG3,0); tracker.removeImage(movmap.IMG4,0); tracker = null; // then kill the tracker } } boolean TrackerError(){return trackerError;} }