Changeset 11714
- Timestamp:
- 2006-04-26T16:55:22+12:00 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/gsdl-2_70-distribution-branch/gsdl/src/java/org/nzdl/gsdl/GsdlCollageApplet/DownloadImages.java
r11564 r11714 24 24 25 25 Image image_; 26 String url _;26 String urlString_; 27 27 String name_; 28 URL url_; 28 29 29 30 /** Constructs a downloaded image structure from the given parameters … … 31 32 * @param url the source url for this image 32 33 * @param name the name of the image file */ 33 public ImageUrlTriple (Image image, String url, String name)34 public ImageUrlTriple (Image image, URL url, String urlString, String name) 34 35 { 35 36 image_ = image; 36 37 url_ = url; 38 urlString_ = urlString; 37 39 name_ = name; 38 40 } … … 42 44 } 43 45 44 String url () {45 return url _;46 String urlString() { 47 return urlString_; 46 48 } 47 49 … … 50 52 } 51 53 54 URL url(){ 55 return url_; 56 } 57 52 58 } 53 54 59 55 60 … … 60 65 GsdlCollageApplet app_ = null; 61 66 boolean isJava2_; 62 int num_of_downloads ;67 int num_of_downloads = 1; 63 68 boolean stopDownload; 64 65 69 70 MediaTracker tracker_; 66 71 67 72 /** Initialises an empty vector to store downloaded images in and sets … … 74 79 } 75 80 76 77 81 /** Push a new image into the vector of downloaded images 78 82 * @param image the graphical image to add … … 81 85 public synchronized int downloadImage(MediaTracker tracker, URL url, String urlString, String name) 82 86 { 87 88 tracker_= tracker; 83 89 84 90 while (downloaded_image != null ){ … … 92 98 93 99 } 94 Image image = Toolkit.getDefaultToolkit().getImage(url); 100 101 Image image = Toolkit.getDefaultToolkit().getImage(url); 102 tracker_.addImage(image,num_of_downloads); 95 103 96 tracker.addImage(image,num_of_downloads); 104 try{ 105 tracker_.waitForID(num_of_downloads); 106 } 107 catch (InterruptedException e){ 108 notify(); 109 return num_of_downloads; 110 } 111 97 112 98 try{ 99 System.out.println( " downloading ..."); 100 tracker.waitForID(num_of_downloads); 101 System.out.println(Thread.currentThread().getName() + " get!!!"); 102 } 103 catch (InterruptedException e){ 104 return 0; 105 } 106 107 downloaded_image = new ImageUrlTriple(image,urlString,name); 108 num_of_downloads++; 113 downloaded_image = new ImageUrlTriple(image,url,urlString,name); 109 114 110 if (num_of_downloads == app_.maxDownloads()){111 stopDownload = true;112 System.out.println("num_of_downloads "+num_of_downloads);113 114 }115 //System.out.println("num_of_downloads "+num_of_downloads +" ");116 115 notify(); 117 116 118 117 return num_of_downloads ; 119 118 } 120 119 120 121 121 /** Remove the image from the specified position in the array of downloaded images 122 122 * @return an ImageUrlTriple containing the image, url and image name */ … … 125 125 if (stopDownload) return null; 126 126 127 while (downloaded_image == null && !stopDownload){127 while (downloaded_image == null){ 128 128 System.out.println(Thread.currentThread().getName() + " wait to get Collage image..."); 129 129 try{ … … 131 131 } 132 132 catch(InterruptedException e){ 133 //e.printStackTrace(); 134 downloaded_image = null; 135 notify(); 133 136 return null; 134 137 } 135 138 } 136 137 collage_image = process(downloaded_image); 138 downloaded_image = null; 139 140 notify(); 141 System.out.println(Thread.currentThread().getName() + " get Collage image!!!"); 142 return collage_image; 139 140 if (tracker_.statusID(num_of_downloads, false) == MediaTracker.COMPLETE){ 141 collage_image = new CollageImage(app_,isJava2_,downloaded_image); 142 downloaded_image = null; 143 num_of_downloads++; 144 notify(); 145 return collage_image; 146 } 147 else{ 148 if ((tracker_.statusAll(false) & MediaTracker.ERRORED) != 0){ 149 //System.out.println(downloaded_image.name_); 150 downloaded_image = null; 151 num_of_downloads++; 152 notify(); 153 return null; 154 } 155 } 156 157 return null; 143 158 144 159 } … … 148 163 } 149 164 150 private CollageImage process(ImageUrlTriple iutriple){151 Image image = iutriple.image();152 String from_url = iutriple.url();153 String img_name = iutriple.name();154 165 155 CollageImage collage_image = new CollageImage(image,from_url,img_name);156 157 image = collage_image.image_;158 // images x and y dimensions159 int image_x_dim = image.getWidth(app_);160 int image_y_dim = image.getHeight(app_);161 162 if ((image_x_dim>0) && (image_y_dim>0))163 {164 collage_image.setDimensions(image_x_dim,image_y_dim);165 166 // places and sizes the image167 MyAffineTransform af = new MyAffineTransform(image_x_dim,image_y_dim);168 169 // sets location & size of collage image170 collage_image.setAffineTransform(af, isJava2_);171 172 if (isJava2_ ) {173 collage_image.image_174 = restoreAlpha(collage_image.image_, 0, 0,175 collage_image.image_x_dim_, collage_image.image_y_dim_);176 177 }178 179 180 }181 182 collage_image.fresh = false;183 return collage_image;184 }185 186 /** Resets the alpha channel of an image so that it appears solid187 *188 * @param img the image to restore189 * @param x the x co-ordinate of the image190 * @param y the y co-ordinate of the image191 * @param w the width of the image192 * @param h the height of the image */193 public Image restoreAlpha(Image img, int x, int y, int w, int h) {194 195 // declare an array to hold the pixels196 int[] pixels = new int[w * h];197 198 // get the pixels of the image into the pixels array199 PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);200 201 try {202 System.err.println(" grab pixels....");203 pg.grabPixels();204 System.err.println(" got pixels!!!");205 } catch (InterruptedException e) {206 System.err.println("interrupted waiting for pixels!");207 return img;208 }209 210 211 // check for any failures212 if ((pg.getStatus() & ImageObserver.ABORT) != 0) {213 System.err.println(" image fetch aborted or errored");214 return img;215 }216 217 // loop through every pixel in the picture and handle it218 for (int j = 0; j < h; j++) {219 for (int i = 0; i < w; i++) {220 pixels[j * w + i] |= (255 << 24) & 0xff000000;221 }222 }223 224 // set the pixels of the whole picture to the pixels array225 pg.setPixels(0, 0, w, h, pg.getColorModel(), pixels, 0, w);226 227 // create the new image and return it228 return( app_.createImage(new MemoryImageSource(w, h, pixels, 0, w)) );229 230 }231 232 233 166 }
Note:
See TracChangeset
for help on using the changeset viewer.