source: trunk/gsdl/src/java/org/nzdl/gsdl/GsdlCollageApplet/DownloadImages.java@ 11647

Last change on this file since 11647 was 11647, checked in by shaoqun, 18 years ago

add a new variable (url) to CollageImage class, so we known where the images was downloaded

  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1package org.nzdl.gsdl.GsdlCollageApplet;
2
3import java.awt.*;
4import java.io.*;
5import java.net.*;
6import java.util.*;
7import java.awt.image.*;
8import java.awt.geom.*;
9
10/**
11 * @author Katrina Edgar
12 * @author David Bainbridge
13 *
14 * Stores images that are downloaded but have not yet been displayed
15 * in the applet. Provides methods to add to and remove from this list
16 * and check it's size. It uses an inner class to store the data for these
17 * images, comprising of three parts: the image itself, it's source url
18 * and the name of the image */
19public class DownloadImages {
20
21 /** Data structure to store an image that has been downloaded and is
22 * waiting to be used. */
23 public class ImageUrlTriple {
24
25 Image image_;
26 String urlString_;
27 String name_;
28 URL url_;
29
30 /** Constructs a downloaded image structure from the given parameters
31 * @param image the graphical image
32 * @param url the source url for this image
33 * @param name the name of the image file */
34 public ImageUrlTriple (Image image, URL url, String urlString, String name)
35 {
36 image_ = image;
37 url_ = url;
38 urlString_ = urlString;
39 name_ = name;
40 }
41
42 Image image() {
43 return image_;
44 }
45
46 String urlString() {
47 return urlString_;
48 }
49
50 String name() {
51 return name_;
52 }
53
54 URL url(){
55 return url_;
56 }
57
58 }
59
60
61 ImageUrlTriple downloaded_image;
62 CollageImage collage_image;
63
64 /** Refers to applet */
65 GsdlCollageApplet app_ = null;
66 boolean isJava2_;
67 int num_of_downloads;
68 boolean stopDownload;
69
70
71
72 /** Initialises an empty vector to store downloaded images in and sets
73 * the verbosity for this class */
74 public DownloadImages(GsdlCollageApplet app, int verbosity, boolean isJava2)
75 {
76
77 app_=app;
78 isJava2_= isJava2;
79 }
80
81
82 /** Push a new image into the vector of downloaded images
83 * @param image the graphical image to add
84 * @param url the source url of the image
85 * @param name the name of the image to add */
86 public synchronized int downloadImage(MediaTracker tracker, URL url, String urlString, String name)
87 {
88
89 while (downloaded_image != null ){
90 System.out.println(Thread.currentThread().getName() + " wait to download...");
91 try{
92 wait();
93 }
94 catch(InterruptedException e){
95 return 0;
96 }
97
98 }
99
100 Image image = Toolkit.getDefaultToolkit().getImage(url);
101
102 tracker.addImage(image,num_of_downloads);
103
104 try{
105 System.out.println( " downloading ...");
106 tracker.waitForID(num_of_downloads);
107 System.out.println(Thread.currentThread().getName() + " get!!!");
108 }
109 catch (InterruptedException e){
110 return 0;
111 }
112
113 downloaded_image = new ImageUrlTriple(image,url,urlString,name);
114 num_of_downloads++;
115
116 if (num_of_downloads == app_.maxDownloads()){
117 stopDownload = true;
118 System.out.println("num_of_downloads "+num_of_downloads);
119
120 }
121 //System.out.println("num_of_downloads "+num_of_downloads +" ");
122 notify();
123
124 return num_of_downloads ;
125 }
126
127 /** Remove the image from the specified position in the array of downloaded images
128 * @return an ImageUrlTriple containing the image, url and image name */
129 public synchronized CollageImage getCollageImage()
130 {
131 if (stopDownload) return null;
132
133 while (downloaded_image == null && !stopDownload){
134 System.out.println(Thread.currentThread().getName() + " wait to get Collage image...");
135 try{
136 wait();
137 }
138 catch(InterruptedException e){
139 //e.printStackTrace();
140 return null;
141 }
142 }
143
144 collage_image = process(downloaded_image);
145 downloaded_image = null;
146
147 notify();
148 System.out.println(Thread.currentThread().getName() + " get Collage image!!!");
149 return collage_image;
150
151 }
152
153 public void stopDownload(){
154 stopDownload = true;
155 }
156
157 private CollageImage process(ImageUrlTriple iutriple){
158 Image image = iutriple.image();
159 String from_url = iutriple.urlString();
160 String img_name = iutriple.name();
161 URL url = iutriple.url();
162
163 CollageImage collage_image = new CollageImage(image,url,from_url,img_name);
164
165 image = collage_image.image_;
166 // images x and y dimensions
167 int image_x_dim = image.getWidth(app_);
168 int image_y_dim = image.getHeight(app_);
169
170 if ((image_x_dim>0) && (image_y_dim>0))
171 {
172 collage_image.setDimensions(image_x_dim,image_y_dim);
173
174 // places and sizes the image
175 MyAffineTransform af = new MyAffineTransform(image_x_dim,image_y_dim);
176
177 // sets location & size of collage image
178 collage_image.setAffineTransform(af, isJava2_);
179
180 if (isJava2_ ) {
181 collage_image.image_
182 = restoreAlpha(collage_image.image_, 0, 0,
183 collage_image.image_x_dim_, collage_image.image_y_dim_);
184
185 }
186
187
188 }
189
190 collage_image.fresh = false;
191 return collage_image;
192 }
193
194 /** Resets the alpha channel of an image so that it appears solid
195 *
196 * @param img the image to restore
197 * @param x the x co-ordinate of the image
198 * @param y the y co-ordinate of the image
199 * @param w the width of the image
200 * @param h the height of the image */
201 public Image restoreAlpha(Image img, int x, int y, int w, int h) {
202
203 // declare an array to hold the pixels
204 int[] pixels = new int[w * h];
205
206 // get the pixels of the image into the pixels array
207 PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
208
209 try {
210 System.err.println(" grab pixels....");
211 pg.grabPixels();
212 System.err.println(" got pixels");
213 } catch (InterruptedException e) {
214 System.err.println("interrupted waiting for pixels!");
215 return img;
216 }
217
218
219 // check for any failures
220 if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
221 System.err.println(" image fetch aborted or errored");
222 return img;
223 }
224
225 // loop through every pixel in the picture and handle it
226 for (int j = 0; j < h; j++) {
227 for (int i = 0; i < w; i++) {
228 pixels[j * w + i] |= (255 << 24) & 0xff000000;
229 }
230 }
231
232 // set the pixels of the whole picture to the pixels array
233 pg.setPixels(0, 0, w, h, pg.getColorModel(), pixels, 0, w);
234 // create the new image and return it
235 return( app_.createImage(new MemoryImageSource(w, h, pixels, 0, w)) );
236
237
238
239
240 }
241
242
243}
Note: See TracBrowser for help on using the repository browser.