Changeset 11564


Ignore:
Timestamp:
2006-04-03T09:59:58+12:00 (18 years ago)
Author:
shaoqun
Message:

changed the code handling threads

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/src/java/org/nzdl/gsdl/GsdlCollageApplet/DownloadImages.java

    r6816 r11564  
    55import java.net.*;
    66import java.util.*;
     7import java.awt.image.*;
     8import java.awt.geom.*;
    79
    810/**
     
    5052    }
    5153
    52     /** The amount of output to produce to the screen */
    53     int verbosity_       = 0;
    54     /** Holds the images that have been downloaded and are waiting to be used */
    55     Vector image_array_  = null;
     54
     55   
     56    ImageUrlTriple downloaded_image;
     57    CollageImage collage_image;
     58
     59    /** Refers to applet */
     60    GsdlCollageApplet app_ = null;
     61    boolean isJava2_;
     62    int num_of_downloads;
     63    boolean stopDownload;
     64 
     65 
    5666
    5767    /** Initialises an empty vector to store downloaded images in and sets
    5868     *  the verbosity for this class */
    59     public DownloadImages(int verbosity)
     69    public DownloadImages(GsdlCollageApplet app, int verbosity, boolean isJava2)
    6070    {
    61     verbosity_ = verbosity;
    62     image_array_ = new Vector();
    63     }
    64 
    65     /** Get the number of images currently downloaded and waiting for use
    66      *  @return the number of images currently downloaded */
    67     public synchronized int size()
    68     {
    69     return image_array_.size();
    70     }
    71 
    72    //   /** Get the image from the specified position in the array of downloaded images
    73 //       *  @return an ImageUrlTriple containing the image, url and image name */
    74 //      public synchronized ImageUrlTriple get(int pos)
    75 //      {
    76 //      return (ImageUrlTriple)image_array_.get(pos);
    77 //      }
     71   
     72        app_=app;
     73        isJava2_= isJava2;
     74    }
     75
    7876
    7977    /** Push a new image into the vector of downloaded images
     
    8179     *  @param url the source url of the image
    8280     *  @param name the name of the image to add */
    83     public synchronized void push(Image image, String url, String name)
     81    public  synchronized int downloadImage(MediaTracker tracker, URL url, String urlString, String name)
    8482    {
    85     ImageUrlTriple iutriple = new ImageUrlTriple(image,url,name);
    86 
    87     image_array_.addElement(iutriple);
     83
     84    while (downloaded_image != null ){
     85        System.out.println(Thread.currentThread().getName() + " wait to download...");
     86        try{
     87        wait();
     88        }
     89        catch(InterruptedException e){
     90        return 0;
     91        }
     92
     93    }
     94        Image image = Toolkit.getDefaultToolkit().getImage(url);
     95
     96         tracker.addImage(image,num_of_downloads);
     97
     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++;
     109
     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    notify();
     117       
     118    return num_of_downloads ;
    88119    }
    89120
    90121    /** Remove the image from the specified position in the array of downloaded images
    91122     *  @return an ImageUrlTriple containing the image, url and image name */
    92     public synchronized ImageUrlTriple retrieve(int pos)
     123    public synchronized CollageImage getCollageImage()
    93124    {
    94     if (image_array_.size() == 0)
    95         return null;
    96 
    97     if (pos >= (image_array_.size() - 1))
    98         pos = 0;
    99 
    100     ImageUrlTriple u = (ImageUrlTriple) image_array_.elementAt(pos);
    101     image_array_.removeElementAt(pos);
    102     return u;
    103     }
    104 
    105     /** gets the oldest downloaded image, checks that the downloaded images are
    106      *  not empty.
    107      *  @return an ImageUrlTriple containing the image, url and image name */
    108     public synchronized ImageUrlTriple shift()
    109     {   
    110     if (image_array_.size()==0) {
    111         return null;
    112     }
    113 
    114     ImageUrlTriple u = (ImageUrlTriple) image_array_.elementAt(0);
    115     image_array_.removeElementAt(0);
    116     return u;
    117     }
     125    if (stopDownload)  return null;   
     126
     127    while (downloaded_image == null && !stopDownload){
     128        System.out.println(Thread.currentThread().getName() + " wait to get Collage image...");
     129        try{
     130        wait();
     131        }
     132        catch(InterruptedException e){
     133        return null;
     134        }
     135    }
     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;
     143
     144    }
     145
     146    public void stopDownload(){
     147        stopDownload = true;
     148    }
     149
     150    private CollageImage process(ImageUrlTriple iutriple){
     151            Image image     = iutriple.image();
     152        String from_url = iutriple.url();
     153        String img_name = iutriple.name();
     154 
     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
    118233}
Note: See TracChangeset for help on using the changeset viewer.