/** * 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.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 movmap mm; // instance reference to the movmap applet private msgpanel mp; // instance reference for message panel private MediaTracker MT; // to track when the images have been loaded private int ls, L; // load switch, language switch private boolean LdgImgs = true, MTerror = false, // true = that MT encountered an error loadFTT = true, // to avoid erase + repaint of loading message errorFTT = true; // and load-error message once every refresh cycle 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 media MT and register the images. The Media Tracker monitors the progress of load- ing process for of each of the four image files. */ MT = new MediaTracker(mm); movmap.IMG1 = getImg("clouds1.png"); if(movmap.IMG1 != null) MT.addImage(movmap.IMG1,0); movmap.IMG2 = getImg("clouds2.png"); if(movmap.IMG2 != null) MT.addImage(movmap.IMG2,0); movmap.IMG3 = getImg("clouds3.png"); if(movmap.IMG3 != null) MT.addImage(movmap.IMG3,0); movmap.IMG4 = getImg("clouds4.png"); if(movmap.IMG4 != null) MT.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 } try { MT.waitForAll(); // keep checking the MT for a signal } catch(InterruptedException e){ // if the MT does not say anything return true; // return without servicing the thread } return false; // drops thru when MT gives a completion signal } boolean imagesLoaded() { if(MT == null) // If MT has been killed return true; // say that loading has finished if(LdgImgs) { // If loading still in progress int ts = MT.statusAll(false); /* if the loading process has completed but a media MT error has occurred, indicate an image loading failure */ if((ts & MediaTracker.ERRORED) != 0) { MTerror = 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 MT has completed its task, indicate that image loading has terminated. */ if(ts == MediaTracker.COMPLETE) {LdgImgs = false;} } /* Else the image loading process has just finished, so kill the MT 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 MT if(MT != null) { MT.removeImage(movmap.IMG1, 0); MT.removeImage(movmap.IMG2, 0); MT.removeImage(movmap.IMG3, 0); MT.removeImage(movmap.IMG4, 0); MT = null; // then kill the MT } } boolean TrackerError(){return MTerror;} }