package org.nzdl.gsdl.GsdlCollageApplet; import java.awt.*; import java.io.*; import java.net.*; import java.util.*; import java.awt.image.*; import java.awt.geom.*; /** * @author Katrina Edgar * @author David Bainbridge * * Stores images that are downloaded but have not yet been displayed * in the applet. Provides methods to add to and remove from this list * and check it's size. It uses an inner class to store the data for these * images, comprising of three parts: the image itself, it's source url * and the name of the image */ public class DownloadImages { /** Data structure to store an image that has been downloaded and is * waiting to be used. */ public class ImageUrlTriple { Image image_; String urlString_; String name_; URL url_; /** Constructs a downloaded image structure from the given parameters * @param image the graphical image * @param url the source url for this image * @param name the name of the image file */ public ImageUrlTriple (Image image, URL url, String urlString, String name) { image_ = image; url_ = url; urlString_ = urlString; name_ = name; } Image image() { return image_; } String urlString() { return urlString_; } String name() { return name_; } URL url(){ return url_; } } ImageUrlTriple downloaded_image; CollageImage collage_image; /** Refers to applet */ GsdlCollageApplet app_ = null; boolean isJava2_; int num_of_downloads; boolean stopDownload; /** Initialises an empty vector to store downloaded images in and sets * the verbosity for this class */ public DownloadImages(GsdlCollageApplet app, int verbosity, boolean isJava2) { app_=app; isJava2_= isJava2; } /** Push a new image into the vector of downloaded images * @param image the graphical image to add * @param url the source url of the image * @param name the name of the image to add */ public synchronized int downloadImage(MediaTracker tracker, URL url, String urlString, String name) { while (downloaded_image != null ){ System.out.println(Thread.currentThread().getName() + " wait to download..."); try{ wait(); } catch(InterruptedException e){ return 0; } } Image image = Toolkit.getDefaultToolkit().getImage(url); tracker.addImage(image,num_of_downloads); try{ System.out.println( " downloading ..."); tracker.waitForID(num_of_downloads); System.out.println(Thread.currentThread().getName() + " get!!!"); } catch (InterruptedException e){ return 0; } downloaded_image = new ImageUrlTriple(image,url,urlString,name); num_of_downloads++; if (num_of_downloads == app_.maxDownloads()){ stopDownload = true; System.out.println("num_of_downloads "+num_of_downloads); } //System.out.println("num_of_downloads "+num_of_downloads +" "); notify(); return num_of_downloads ; } /** Remove the image from the specified position in the array of downloaded images * @return an ImageUrlTriple containing the image, url and image name */ public synchronized CollageImage getCollageImage() { if (stopDownload) return null; while (downloaded_image == null && !stopDownload){ System.out.println(Thread.currentThread().getName() + " wait to get Collage image..."); try{ wait(); } catch(InterruptedException e){ //e.printStackTrace(); return null; } } collage_image = process(downloaded_image); downloaded_image = null; notify(); System.out.println(Thread.currentThread().getName() + " get Collage image!!!"); return collage_image; } public void stopDownload(){ stopDownload = true; } private CollageImage process(ImageUrlTriple iutriple){ Image image = iutriple.image(); String from_url = iutriple.urlString(); String img_name = iutriple.name(); URL url = iutriple.url(); CollageImage collage_image = new CollageImage(image,url,from_url,img_name); image = collage_image.image_; // images x and y dimensions int image_x_dim = image.getWidth(app_); int image_y_dim = image.getHeight(app_); if ((image_x_dim>0) && (image_y_dim>0)) { collage_image.setDimensions(image_x_dim,image_y_dim); // places and sizes the image MyAffineTransform af = new MyAffineTransform(image_x_dim,image_y_dim); // sets location & size of collage image collage_image.setAffineTransform(af, isJava2_); if (isJava2_ ) { collage_image.image_ = restoreAlpha(collage_image.image_, 0, 0, collage_image.image_x_dim_, collage_image.image_y_dim_); } } collage_image.fresh = false; return collage_image; } /** Resets the alpha channel of an image so that it appears solid * * @param img the image to restore * @param x the x co-ordinate of the image * @param y the y co-ordinate of the image * @param w the width of the image * @param h the height of the image */ public Image restoreAlpha(Image img, int x, int y, int w, int h) { // declare an array to hold the pixels int[] pixels = new int[w * h]; // get the pixels of the image into the pixels array PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w); try { System.err.println(" grab pixels...."); pg.grabPixels(); System.err.println(" got pixels"); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); return img; } // check for any failures if ((pg.getStatus() & ImageObserver.ABORT) != 0) { System.err.println(" image fetch aborted or errored"); return img; } // loop through every pixel in the picture and handle it for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { pixels[j * w + i] |= (255 << 24) & 0xff000000; } } // set the pixels of the whole picture to the pixels array pg.setPixels(0, 0, w, h, pg.getColorModel(), pixels, 0, w); // create the new image and return it return( app_.createImage(new MemoryImageSource(w, h, pixels, 0, w)) ); } }