Changeset 11714


Ignore:
Timestamp:
2006-04-26T16:55:22+12:00 (18 years ago)
Author:
kjdon
Message:

committed Shaoquns version of the applet for the branch - version 1.4

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/gsdl-2_70-distribution-branch/gsdl/src/java/org/nzdl/gsdl/GsdlCollageApplet/DownloadImages.java

    r11564 r11714  
    2424
    2525    Image image_;
    26     String url_;
     26    String urlString_;
    2727    String name_;
     28        URL url_;
    2829
    2930    /** Constructs a downloaded image structure from the given parameters
     
    3132     *  @param url the source url for this image
    3233     *  @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)
    3435    {
    3536        image_ = image;
    3637        url_ = url;
     38            urlString_ = urlString;
    3739        name_ = name;
    3840    }
     
    4244    }
    4345
    44     String url() {
    45         return url_;
     46    String urlString() {
     47        return urlString_;
    4648    }
    4749
     
    5052    }
    5153
     54    URL url(){
     55        return url_;
     56    }
     57
    5258    }
    53 
    5459
    5560   
     
    6065    GsdlCollageApplet app_ = null;
    6166    boolean isJava2_;
    62     int num_of_downloads;
     67    int num_of_downloads = 1;
    6368    boolean stopDownload;
    64  
    65  
     69
     70    MediaTracker tracker_;
    6671
    6772    /** Initialises an empty vector to store downloaded images in and sets
     
    7479    }
    7580
    76 
    7781    /** Push a new image into the vector of downloaded images
    7882     *  @param image the graphical image to add
     
    8185    public  synchronized int downloadImage(MediaTracker tracker, URL url, String urlString, String name)
    8286    {
     87   
     88    tracker_= tracker;
    8389
    8490    while (downloaded_image != null ){
     
    9298
    9399    }
    94         Image image = Toolkit.getDefaultToolkit().getImage(url);
     100     
     101    Image image = Toolkit.getDefaultToolkit().getImage(url);
     102    tracker_.addImage(image,num_of_downloads);
    95103
    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   
    97112
    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);
    109114
    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 +" ");
    116115    notify();
    117116       
    118117    return num_of_downloads ;
    119118    }
    120 
     119 
     120 
    121121    /** Remove the image from the specified position in the array of downloaded images
    122122     *  @return an ImageUrlTriple containing the image, url and image name */
     
    125125    if (stopDownload)  return null;   
    126126
    127     while (downloaded_image == null && !stopDownload){
     127    while (downloaded_image == null){
    128128        System.out.println(Thread.currentThread().getName() + " wait to get Collage image...");
    129129        try{
     
    131131        }
    132132        catch(InterruptedException e){
     133        //e.printStackTrace();
     134        downloaded_image = null;
     135        notify();
    133136        return null;
    134137        }
    135138    }
    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;
    143158
    144159    }
     
    148163    }
    149164
    150     private CollageImage process(ImageUrlTriple iutriple){
    151             Image image     = iutriple.image();
    152         String from_url = iutriple.url();
    153         String img_name = iutriple.name();
    154165 
    155         CollageImage collage_image = new CollageImage(image,from_url,img_name);
    156 
    157         image = collage_image.image_;
    158         // images x and y dimensions
    159         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 image
    167         MyAffineTransform af = new MyAffineTransform(image_x_dim,image_y_dim);
    168        
    169         // sets location & size of collage image
    170         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 solid
    187      *
    188      *  @param img the image to restore
    189      *  @param x the x co-ordinate of the image
    190      *  @param y the y co-ordinate of the image
    191      *  @param w the width of the image
    192      *  @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 pixels
    196     int[] pixels = new int[w * h];
    197 
    198     // get the pixels of the image into the pixels array
    199     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 failures
    212     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 it
    218     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 array
    225     pg.setPixels(0, 0, w, h, pg.getColorModel(), pixels, 0, w);
    226 
    227     // create the new image and return it
    228     return( app_.createImage(new MemoryImageSource(w, h, pixels, 0, w)) );
    229    
    230     }
    231 
    232 
    233166}
Note: See TracChangeset for help on using the changeset viewer.