import java.awt.*; import java.net.*; import java.util.*; import java.io.*; /** * A class that uses a MediaTracker to load pictures given via the methods add(). Usage in an applet-security-environment assumed. * @author Torbjörn Nilsson * @version 1.0 */ public class ImageLoader { MediaTracker mt; static String castleURL = "http://www.lysator.liu.se/~nexus/FOD/PICS/fortress.jpg"; //test static String mapURL = "http://www.lysator.liu.se/~nexus/FOD/PICS/map4.jpg"; //test Toolkit tk = Toolkit.getDefaultToolkit(); String[] urls; Frame fr; String host; /** * Give the name of a host, so every picture URL can be transformed into a new URL with that host as address of the server. */ public ImageLoader(String hostname) { super(); fr = new Frame(); mt = new MediaTracker(fr); host = hostname; } /** * Add the given URL to list of pics this ImageLoader should try loading. Convert WWW-server name of URL to the host given in the contructor. */ public Image add(String url) { Image im = null; // System.out.println(url+"\n"+host); url = "http://" + host + url.substring(url.indexOf("se/")+2); System.out.println(url); try { im = tk.getImage(new URL(url)); } catch(MalformedURLException e) {System.out.println("ERROR! "+e);} mt.addImage(im,0); System.out.println(im); return im; } /** * Wait for all images added with add() to be loaded, then return call. */ public void loadAllImages() { //while() //image.getWidth(null) check if -1 is returned --- for all pics // sleep() try {mt.waitForAll();} catch (InterruptedException e){} } /* * Get an image objext and start the downloading public static synchronized Image loadFullImage(String url, Component comp) { Image im = null; try { im = tk.getImage(new URL(url)); } catch(MalformedURLException e) {System.out.println("ERROR! "+e);} mt.addImage(im,0); try mt.waitForAll(); catch (InterruptedException e); return im; } */ /** * For test purposes. */ public static void main(String[] a) { try { ImageLoader imlo = new ImageLoader(InetAddress.getLocalHost().getHostName()); Image im = imlo.add(castleURL); Image im2 = imlo.add(mapURL); imlo.loadAllImages(); System.out.println("hej"); Frame f = new Frame("MediaTrackern"); f.setLayout(new BorderLayout()); f.resize(300,300); f.add("Center", new BkgPanel(im)); Frame ff = new Frame("MediaTrackern"); ff.setLayout(new BorderLayout()); ff.resize(300,300); ff.add("Center", new BkgPanel(im2)); f.show(); ff.show(); } catch (Exception e) { System.out.println("Fel..."+e); } } }