Changeset 11715


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

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

Location:
branches/gsdl-2_70-distribution-branch/gsdl/src/java/org/nzdl/gsdl/GsdlCollageApplet
Files:
2 edited

Legend:

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

    r11572 r11715  
    1212 *
    1313 *  Retrieves images from the download thread, determines an appropriate position
    14  *  for these images onscreen ensuring maximum whitespace coverage and minimal overlap
     14 *  for thes  images onscreen ensuring maximum whitespace coverage and minimal overlap
    1515 *  when possible. Draws images using advanced image processing techniques when applicable,
    1616 *  such as fading the edges of an image and discolouring backgrounds. Also re-uses images
     
    1919public class DisplayImages extends Thread {
    2020
    21     /** Decrements the alpha value between adjacent rows on the edge of an image<br>
    22      *  Used to fade the edges faster than the rest of the image <br>
    23      *  Only used when java2 parameter is true and advanced image processing
    24      *  techniques may be applied */
    25     static final int FADEVALUE = 3;
    26     /** Defines the number of shades of white used to colour the backgrounds of white images <br>
    27      *  These colours are used because when several white images appear in the collage
    28      *  simultaneously it is difficult to distinguish their edges<br>
    29      *  Only used when java2 parameter is true and advanced image processing
    30      *  techniques may be applied */
    31     static final int NO_OF_COLORS = 7;
    3221
    3322    /** Will fire a removal operation when 4 images in the collage overlap a single area */
     
    6049    protected boolean is_java2_     = false;
    6150
    62     /** Holds images currently on display in the applet */
    63     protected  Vector inuse_ready_    = null;
    64 
     51 
    6552    /** Holds images currently on display in the applet */
    6653    protected Vector inuse_    = null;
    6754   
     55     /** Holds images ready to be used */
     56    protected Vector inuse_ready_    = null;
     57
    6858    /** Holds previously displayed images for re-use */
    6959    protected Vector notinuse_ = null;
     
    8777    public static int [] [] used_space = null;
    8878   
    89     /** Defines the shades of white used to colour the backgrounds of white images <br>
    90      *  These colours are used because when several white images appear in the collage
    91      *  simultaneously it is difficult to distinguish their edges<br>
    92      *  Only used when java2 parameter is true and advanced image processing
    93      *  techniques may be applied */
    94     int colorMask [] = {0xF8FFFF, 0xFFF8FF, 0xFFFFF8, 0xF8F8FF, 0xFFF8F8, 0xF8FFF8, 0xFAFAFA};
    95 
     79 
    9680    /**
    9781     *  Starts the display images thread. Initialises variables using parameters and
     
    11498
    11599    inuse_    = new Vector();
    116         inuse_ready_   = new Vector(); 
    117     notinuse_ = new Vector();
     100        notinuse_ = new Vector();
     101        inuse_ready_ = new Vector();
    118102    download_images_ = download_images;
    119103    // sets width and height
     
    146130        next_frame_ =  new Thread(new Runnable(){
    147131                public void run() {
    148             try {
    149 
    150             Thread curr_thread = Thread.currentThread();
    151        
    152             while (curr_thread == next_frame_) {
     132         
     133            Thread curr_thread = Thread.currentThread();
     134           
     135            while (curr_thread == next_frame_) {
     136            try {       
     137                Runtime rt = Runtime.getRuntime();
     138                //System.out.println("total: "+rt.totalMemory());
     139                //System.out.println("free:  "+rt.freeMemory());
     140                rt.gc();             
     141                        //System.out.println("**************next frame...");
    153142                next_frame();
    154                
    155143                Thread.sleep(app_. refreshDelay_);
     144               
    156145                curr_thread = Thread.currentThread();
     146            } catch (Exception e) {
     147                            e.printStackTrace();
     148                break;
    157149            }
    158 
    159        
    160             } catch (Exception e) {
    161             e.printStackTrace();
    162             }
     150            }
     151                   
     152           
    163153        }
    164154
     
    166156        });
    167157
    168        next_frame_.start();
     158     next_frame_.start();
    169159
    170160
     
    175165     *  @param y the y co-ordinate of the mouse
    176166     *  @return the image that the user has clicked on or null if the user has clicked whitespace */
    177    public synchronized CollageImage clickedOnImage(int x, int y)
     167   public CollageImage clickedOnImage(int x, int y)
    178168    {
    179169    if (app_.verbosity() > 1)
     
    182172    }
    183173
    184     int no_images = inuse_.size() - 1;
     174   
    185175    // checks from last image down, as last images place will be at the top of the collage
    186     for (int i= no_images; i >= 0; i--)
     176    for (int i= inuse_.size() - 1; i >= 0; i--)
    187177    {
    188178        CollageImage collage_image = (CollageImage)inuse_.elementAt(i);
     
    211201    public  void display_collage()
    212202    {
    213 
    214         // get the number of images currently in the collage
    215     final int no_images = inuse_.size();
    216    
    217 
     203   
    218204    // if there is nothing new to add
    219     if (no_images==0) return;
    220    
     205    if (inuse_.size()==0) return;
    221206   
    222207    // otherwise need to re-generate images
    223208    if (app_.verbosity() >= 2)
    224     {
    225         System.err.println("Regenerating images: " +no_images);
    226     }
     209        {
     210        //system.err.println("Regenerating images");
     211        }
    227212   
    228213    // get each image currently onscreen inturn
    229     for (int i=0; i<inuse_.size(); i++)
    230     {
    231         CollageImage collage_image = (CollageImage)inuse_.elementAt(i);
    232        
    233         Image image = collage_image.image_;
    234         // images x and y dimensions
    235         int image_x_dim = image.getWidth(app_);
    236         int image_y_dim = image.getHeight(app_);
    237 
    238         //int status = app_.checkImage(image,app_);
    239 
    240         if ((image_x_dim>0) && (image_y_dim>0))
     214    for (int i=0; i<inuse_.size();)
    241215        {
     216        CollageImage collage_image = (CollageImage)inuse_.elementAt(i);
     217 
     218                if (!collage_image.isValid()){
     219            continue;
     220        }       
     221
     222                int[] pixels = null;
    242223        // the first time the image is painted, determine its position and size
    243224        if (collage_image.fresh == true)
    244225            {
    245             // first time => randomly place doc
    246             collage_image.setDimensions(image_x_dim,image_y_dim);
    247            
    248             // places and sizes the image
    249             MyAffineTransform af = new MyAffineTransform(image_x_dim,image_y_dim);
    250        
    251             // sets location & size of collage image
    252             collage_image.setAffineTransform(af, is_java2_);
    253            
    254              if (is_java2_) {
    255                 collage_image.image_
    256                  = restoreAlpha(collage_image.image_, 0, 0,
    257                            collage_image.image_x_dim_, collage_image.image_y_dim_);
    258                
    259             }
    260            
    261            
     226            collage_image.process();
    262227            }
    263228       
    264229        // advanced image processing may be used
    265230        if (is_java2_)
    266         {
    267              
    268  
    269             // conducts fading and colouring of image
    270             Image img = handlepixels(collage_image.image_, 0, 0, collage_image.image_x_dim_,
    271                         collage_image.image_y_dim_, i, collage_image.fresh );
    272             collage_image.image_ = img;
     231            {
     232            // conducts fading and colouring of image
     233            pixels = collage_image.handlepixels(inuse_.size()-i);
     234            }
    273235       
    274  
    275         }
    276 
    277236        if (collage_image.fresh) {
    278237            collage_image.fresh = false;
    279238        }
    280            
    281         }
    282         else {
    283         if (app_.verbosity()>=3) {
    284             System.err.println("  Image ["+i+"] not ready yet!");
    285         }
    286         }
    287         //finished_graphic_.drawImage(screen_buffer_,0,0,app_);
    288    
    289     }   
    290    
    291     // draws the finished image onscreen
    292     finished_graphic_.drawImage(screen_buffer_,0,0,app_);
    293     remove_old_images();
    294     }
    295 
    296 
    297  /** loops through removing oldest images and reduces number of images if excessive overlap is occuring */
    298     void remove_old_images() {
    299    
    300     // loops through removing oldest images that have almost faded completely
    301     // and restricting the size of a simple collage to 25 images
    302     int i=0;
    303     while (i<inuse_.size())
    304     {
    305         CollageImage collage_image = (CollageImage)inuse_.elementAt(i);
    306        
    307         // if we remove when images have faded too much
    308         if (is_java2_) {
    309         if (checkFaded(collage_image.image_, 0, 0, collage_image.image_x_dim_, collage_image.image_y_dim_) || overlap)
    310             {
     239       
     240       
     241        // if we remove when images have faded too much
     242        if (is_java2_) {
     243            if (collage_image.checkFaded(pixels)|| overlap)
     244            {
     245                overlap = false;
     246               
     247                CollageImage removed_image = (CollageImage)inuse_.elementAt(i);
     248                //System.out.println(removed_image.name_ + "is removed");
     249                inuse_.remove(i);
     250                // indicates space for this image is no longer used
     251                for (int n = removed_image.xl_; n < (removed_image.xl_ + removed_image.image_x_dim_); n++)
     252                for (int m = removed_image.yt_; m < (removed_image.yt_ + removed_image.image_y_dim_); m++)
     253                    if(used_space[n][m] != 0) {
     254                    used_space[n][m]--;
     255                    if (used_space[n][m] > NO_IMAGES_OF_OVERLAP)
     256                        overlap = true;
     257                    }
     258               
     259                // sets the image as fresh and adds it to the vector of images previously seen
     260                removed_image.fresh = true;
     261                notinuse_.addElement(removed_image);
     262            }
     263            else{
     264            i++;
     265            }
     266        }
     267        else
     268            if (inuse_.size() > 25 || overlap) {
    311269            overlap = false;
    312 
     270           
    313271            CollageImage removed_image = (CollageImage)inuse_.elementAt(i);
    314272            inuse_.remove(i);
    315 
    316             // restores alpha channel to represent a solid image
    317             Image img = restoreAlpha(removed_image.image_, 0, 0, removed_image.image_x_dim_, removed_image.image_y_dim_);
    318273           
    319274            // indicates space for this image is no longer used
     
    322277                if(used_space[n][m] != 0) {
    323278                    used_space[n][m]--;
    324                     if (used_space[n][m] > NO_IMAGES_OF_OVERLAP)
    325                     overlap = true;
     279                if (used_space[n][m] > NO_IMAGES_OF_OVERLAP)
     280                    overlap = true;
    326281                }
    327 
     282           
    328283            // sets the image as fresh and adds it to the vector of images previously seen
    329             removed_image.image_ = img;
    330284            removed_image.fresh = true;
    331285            notinuse_.addElement(removed_image);
    332286            }
    333         else {
    334             i++;
    335         }
    336         }
    337         // if we remove to prevent too many images being displayed simultaneously
    338         else {
    339         if (inuse_.size() > 25 || overlap) {
    340             overlap = false;
    341 
    342             CollageImage removed_image = (CollageImage)inuse_.elementAt(i);
    343             inuse_.remove(i);
    344 
    345             // indicates space for this image is no longer used
    346             for (int n = removed_image.xl_; n < (removed_image.xl_ + removed_image.image_x_dim_); n++)
    347             for (int m = removed_image.yt_; m < (removed_image.yt_ + removed_image.image_y_dim_); m++)
    348                 if(used_space[n][m] != 0) {
    349                 used_space[n][m]--;
    350                 if (used_space[n][m] > NO_IMAGES_OF_OVERLAP)
    351                     overlap = true;
    352                 }
    353            
    354             // sets the image as fresh and adds it to the vector of images previously seen
    355             removed_image.fresh = true;
    356             notinuse_.addElement(removed_image);
    357         }
    358         else {
    359             i++;
    360         }
    361         }
    362     }
    363      
    364      
    365     }
    366 
     287            else{
     288            i++;
     289            }   
     290       
     291        }
     292    }
     293   
    367294 
    368295    /** Alters the images currently on display in the applet <br>
     
    379306    {
    380307    double random = Math.random();
    381              
    382 
    383     if (inuse_ready_.size() > 0){
     308   
     309     
     310     if (inuse_ready_.size() > 0){
    384311        int pos = (int) ((inuse_ready_.size() - 1) * random);
    385         inuse_.addElement(inuse_ready_.remove(pos));
    386     }
    387         else if (notinuse_.size()>0)
    388         {
    389         int position = (int) ((notinuse_.size() - 1) * random);
    390         Image image;
    391        
    392         CollageImage collage_image = (CollageImage)notinuse_.elementAt(position);
    393         notinuse_.remove(position);
    394         inuse_.addElement(collage_image);
    395        
    396         if (app_.verbosity()>3)
     312        CollageImage collage_image = (CollageImage) inuse_ready_.remove(pos);
     313        inuse_.addElement(collage_image);
     314    }
     315    else if (notinuse_.size()>0)
     316        {
     317            int position = (int) ((notinuse_.size() - 1) * random);
     318           
     319            CollageImage collage_image = (CollageImage)notinuse_.elementAt(position);
     320            notinuse_.remove(position);
     321            inuse_.addElement(collage_image);
     322           
     323            if (app_.verbosity()>3)
    397324            {
    398325            System.err.println("Re-using an image not on screen");
    399326            }
    400         }
    401     // otherwise repaint the first image as the last image
    402 //  else if (inuse_.size() > 0){
    403 //      CollageImage removed_image = (CollageImage)inuse_.elementAt(0);
    404        
    405 //      // empty the picture from the grid
    406 //      for (int n = removed_image.xl_; n < (removed_image.xl_ + removed_image.image_x_dim_); n++)
    407 //      for (int m = removed_image.yt_; m < (removed_image.yt_ + removed_image.image_y_dim_); m++)
    408 //          used_space[n][m]--;
    409        
    410 //      // reset the alpha and add it
    411 //      removed_image.fresh = true;
    412        
    413 //       if (is_java2_) {
    414 //      removed_image.image_ = restoreAlpha(removed_image.image_, 0, 0,
    415 //                          removed_image.image_x_dim_, removed_image.image_y_dim_);
    416 //      }
    417        
    418 //      notinuse_.addElement(removed_image);
    419 //      inuse_.remove(0);
    420 //      if (app_.verbosity()>=4){
    421 //      System.err.println("Removing oldest image");
    422 //      }
    423 //  }
    424     else{
    425         if (app_.verbosity()>=4)
    426         System.err.println("No images in download area");
    427     }
    428           display_collage();
    429    
    430     }
    431    
    432     /** Adjusts the colour and alpha value of an individual pixel <br>
    433      *  If the image is being drawn for the first time, the RGB values are
    434      *  extracted. If the pixel is close to white (RGB > 250) then an offwhite
    435      *  colour is applied to this pixel.
    436      *  This is done because when several white images appear in the collage
    437      *  simultaneously it is difficult to distinguish their edges. <br>
    438      *  This function also fades the alpha value of the pixel as the image ages.
    439      *  The alpha value is more heavily decremented as pixels get closer to the
    440      *  edge of the image
    441      *
    442      *  @param pixel the pixel to manipulate
    443      *  @param p the position of the image in collage (representative of age)
    444      *  @param fresh indicates whether or not the image is being drawn for the first time
    445      *  @param edge indicates whether or not this pixel is near the edge of the image
    446      *  @param fade the amount by which to fade this pixel
    447      *  @return the adjusted pixel as an int */
    448     public int handlesinglepixel(int pixel, int p, boolean fresh, boolean edge, int fade) {
    449    
    450     int newpixel = 0;
    451 
    452         //changes the colour of the picture, only when first drawn
    453     //and only if the pixel is close to white
    454     if (fresh) {
    455        
    456         int red   = (pixel >> 16) & 0xff;
    457         int green = (pixel >>  8) & 0xff;
    458         int blue  = (pixel      ) & 0xff;
    459        
    460         if (red >= 250 && green >= 250 && blue >= 250) {
    461        
    462         int c = colorMask[((int) (p%NO_OF_COLORS))];
    463 
    464         red = (c >> 16) & 0xff;
    465         green = (c >> 8) & 0xff;
    466         blue  = (c) & 0xff;
    467         }
    468 
    469         newpixel |= (red << 16) & 0x00ff0000;
    470         newpixel |= (green << 8) & 0x0000ff00;
    471         newpixel |= blue & 0x000000ff;
    472     }
    473 
    474     else {
    475         newpixel |= pixel & 0x00ffffff;
    476     }
    477 
    478     int alpha = (pixel >> 24) & 0xff;
    479 
    480     if (edge) {
    481         // fade the edges more...
    482         alpha = fade;
    483     }
    484     else if (alpha > 10 && !fresh) {
    485         alpha -= 10;
    486     }
    487 
    488     newpixel |= (alpha << 24) & 0xff000000;
    489    
    490     return (newpixel);
    491     }
    492    
    493     /** Fades and colours the image on a pixel-by-pixel basis <br>
    494      *  First it grabs the pixels of the entire image and stores them in a 2D array.
    495      *  Then a bound is calculated to indicate the point from which edge
    496      *  fading should occur. The position of the bound will change in proportion
    497      *  to the age of the image so that older images have a larger faded edge.
    498      *  The area within the bounds, that forms the center of the image, is processed
    499      *  first and faded by a standard alpha value. Then each edge of the image
    500      *  is processed separately, creating a gradient fade from the true edge to the
    501      *  position of the bound. <br>
    502      *  The faded pixel array is then turned into a new image a returned.
    503      *
    504      *  @param img the image that requires processing
    505      *  @param x the x co-ordinate of the image
    506      *  @param y the y co-ordinate of the image
    507      *  @param w the width of the image
    508      *  @param h the height of the image
    509      *  @param p the position of the image in the applet (indicates age)
    510      *  @param fresh whether the image is being processed for the first time */
    511 
    512     public  Image handlepixels(Image img, int x, int y, int w, int h, int p, boolean fresh) {
    513    
    514     // declare an array to hold the pixels
    515     int[] pixels = new int[w * h];
    516 
    517     // get the pixels of the image into the pixels array
    518     PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
    519     try {
    520         pg.grabPixels();
    521    
    522     } catch (InterruptedException e) {
    523         System.err.println("interrupted waiting for pixels!");
    524         return img;
    525     }
    526     // check for any failures
    527     if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
    528         System.err.println("image fetch aborted or errored");
    529         return img;
    530     }
    531 
    532     // calculates the bound from which fading should begin
    533     double bound = (inuse_.size() - p) * 0.01;
    534     if (w > h)
    535         bound *= w;
    536     else
    537         bound *= h;
    538     int upperboundheight = h - (int) bound;
    539     int upperboundwidth = w - (int) bound;
    540     int lowerbound = (int) bound;
    541 
    542     // loop through every pixel in the picture and handle it
    543     for (int j = lowerbound; j < upperboundheight; j++) {
    544         for (int i = lowerbound; i < upperboundwidth; i++) {
    545         // width and height: x+i y+j
    546         pixels[j * w + i] = handlesinglepixel(pixels[j * w + i], p, fresh, false, 255);
    547         }
    548     }
    549 
    550     int fade = 0;
    551     int fader = 0;
    552     int corealpha = (pixels[(h/2) * w + (w/2)] >> 24) & 0xff;
    553    
    554     // top edge
    555     for (int n = lowerbound; n >= 0; n--) {
    556         fader = corealpha - fade;
    557         if (fader < 0)
    558         fader = 0;
    559        
    560         for (int m = 0; m < w; m++)
    561         pixels[n * w + m] = handlesinglepixel(pixels[n * w + m], p, fresh, true, fader);
    562        
    563         fade+= FADEVALUE;
    564     }
    565 
    566     // bottom edge
    567     fade = 0;
    568     for (int n = upperboundheight; n < h; n++) {
    569         fader = corealpha - fade;
    570         if (fader < 0)
    571         fader = 0;
    572         for (int m = 0; m < w; m++)
    573         pixels[n * w + m] = handlesinglepixel(pixels[n * w + m], p, fresh, true, fader);
    574        
    575         fade += FADEVALUE;
    576     }
    577 
    578     // left edge
    579     fade = 0;
    580     for (int n = lowerbound; n >= 0; n--) {
    581         fader = corealpha - fade;
    582         if (fader < 0)
    583         fader = 0;
    584         for (int m = 0; m < h; m++) {
    585         if ( m < lowerbound && n > m);
    586         else if ( m > upperboundheight && n > (h - m));
    587         else
    588             pixels[m * w + n] = handlesinglepixel(pixels[m * w + n], p, fresh, true, fader);
    589         }
    590         fade += FADEVALUE;   
    591     }
    592    
    593     // right edge
    594     fade = 0;
    595     for (int n = upperboundwidth; n < w; n++) {
    596         fader = corealpha - fade;
    597         if (fader < 0)
    598         fader = 0;
    599         for (int m = 0; m < h; m++) {
    600         if ( m < lowerbound && (w - n) > m);
    601         else if ( m > upperboundheight && (w - n) > (h - m));
    602         else
    603             pixels[m * w + n] = handlesinglepixel(pixels[m * w + n], p, fresh, true, fader);
    604         }
    605        
    606         fade += FADEVALUE;
    607     }
    608    
    609     // set the pixels of the whole picture to the pixels array
    610     pg.setPixels(0, 0, w, h, pg.getColorModel(), pixels, 0, w);
    611 
    612     // create the new image and return it
    613     return( app_.createImage(new MemoryImageSource(w, h, pixels, 0, w)) );
    614    
    615     }
    616 
    617     /** Resets the alpha channel of an image so that it appears solid
    618      *
    619      *  @param img the image to restore
    620      *  @param x the x co-ordinate of the image
    621      *  @param y the y co-ordinate of the image
    622      *  @param w the width of the image
    623      *  @param h the height of the image */
    624     public Image restoreAlpha(Image img, int x, int y, int w, int h) {
    625      
    626     // declare an array to hold the pixels
    627     int[] pixels = new int[w * h];
    628 
    629     // get the pixels of the image into the pixels array
    630     PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
    631 
    632     try {
    633      
    634         pg.grabPixels();
    635          
    636     } catch (InterruptedException e) {
    637         System.err.println("interrupted waiting for pixels!");
    638         return img;
    639     }
    640 
    641      
    642     // check for any failures
    643     if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
    644         System.err.println("image fetch aborted or errored");
    645         return img;
    646     }
    647 
    648     // loop through every pixel in the picture and handle it
    649     for (int j = 0; j < h; j++) {
    650         for (int i = 0; i < w; i++) {
    651         pixels[j * w + i] |= (255 << 24) & 0xff000000;
    652         }
    653     }
    654 
    655     // set the pixels of the whole picture to the pixels array
    656     pg.setPixels(0, 0, w, h, pg.getColorModel(), pixels, 0, w);
    657 
    658     // create the new image and return it
    659     return( app_.createImage(new MemoryImageSource(w, h, pixels, 0, w)) );
    660    
    661     }
    662 
    663     /** Checks whether an image has faded to the point where it must be removed
    664      *  from the collage.
    665      *
    666      *  @param img the image to restore
    667      *  @param x the x co-ordinate of the image
    668      *  @param y the y co-ordinate of the image
    669      *  @param w the width of the image
    670      *  @param h the height of the image */
    671     public boolean checkFaded (Image img, int x, int y, int w, int h) {
    672      if (w == 0 || h == 0)
    673          return false;
    674    
    675     // declare an array to hold the pixels
    676     int[] pixels = new int[w * h];
    677 
    678     // get the pixels of the image into the pixels array
    679     PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
    680     try {
    681         pg.grabPixels();
    682    
    683     } catch (InterruptedException e) {
    684         System.err.println("interrupted waiting for pixels!");
    685         return false;
    686     }
    687     // check for any failures
    688     if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
    689         System.err.println("image fetch aborted or errored");
    690         return false;
    691     }
    692 
    693     // get the alpha value of the middle pixel of the image
    694     int corealpha = (pixels[(h/2) * w + (w/2)] >> 24) & 0xff;
    695    
    696     if (corealpha < 2)
    697         return true;
    698 
    699     return false;
    700    
    701     }
     327        }
     328        else{
     329        if (app_.verbosity()>=4)
     330            System.err.println("No images in download area");
     331        }
     332
     333    display_collage();
     334   
     335    }
     336 
     337 
    702338
    703339    /** Paints the applet on the screen */
     
    725361        if (collage_image.fresh) {
    726362          if (app_.verbosity()>=1) {
    727             System.err.println("doing nothing...  images in use is fresh.");
     363            System.err.println("doing nothing...  "+ collage_image.name_ +" is fresh.");
    728364           }
    729365        }
     
    742378        // simple image collage
    743379        else
    744             {
    745             // add the image to the collage
     380            {           // add the image to the collage
    746381           
    747382            screen_graphic_.drawImage(collage_image.image_,
     
    779414       
    780415        while (curr_thread == this) {
    781         CollageImage collage_image  = download_images_.getCollageImage();
    782             if (collage_image !=null){
    783                 inuse_ready_.addElement(collage_image);
    784             }
    785             else{
    786                 break;
    787             }
    788            curr_thread = Thread.currentThread();
    789         }
    790 
    791        
    792        
     416        if (inuse_ready_.size() >= 1){
     417            Thread.sleep(1000);         
     418            continue;
     419        }   
     420        CollageImage collage_image = download_images_.getCollageImage();
     421   
     422        if (collage_image !=null){
     423            inuse_ready_.addElement(collage_image);
     424        }
     425        curr_thread = Thread.currentThread();
     426        }
     427           
    793428    } catch (Exception e) {
    794         //e.printStackTrace();
     429       e.printStackTrace();
    795430           System.out.println("Display images thread is interrupted");
    796431    }
  • branches/gsdl-2_70-distribution-branch/gsdl/src/java/org/nzdl/gsdl/GsdlCollageApplet/DownloadUrls.java

    r11563 r11715  
    6565    int verbosity_ = 0;
    6666
    67     protected boolean busy_ = false;
    68    
     67     /** Records all images which have already been examined */
     68    Hashtable visited_images_  = null;
     69       
    6970    MediaTracker tracker;
    7071
     
    99100    System.err.println("starting_url_ "  + starting_url +"\n"+
    100101                       "href_musthave_ " +  href_musthave +"\n"+
    101                "image_mustnothave_" + image_mustnothave+"\n"+
     102               "image_mustnothave_ " + image_mustnothave+"\n"+
    102103               "image_ignore_ "+  image_ignore+"\n"+
    103104               "image_type_ "+ image_type+"\n"+
     
    110111    }
    111112
    112       public boolean getStatus(){
    113     return busy_;
    114 
    115     }
    116 
     113 
    117114    /** Determines whether or not a url has already been examined
    118115     *
     
    128125     }
    129126
    130     // if the url has been visited before, return true
     127        // if the url has been visited before, return true
    131128    if (visited_url_.containsKey(url_string))
    132129    {
     
    142139    return false;
    143140    }
     141
     142  /** Determines whether or not an images or its screenview has been visited)
     143     *  has already been examined
     144     *
     145     *  @param url_string the url to check
     146     *  @param img_name the image to check
     147     *  @return true if the url has been visited, false if not */
     148    public boolean image_visited(String url_string, String img_name)
     149    {
     150    String hash_dir = url_string.substring(0,url_string.lastIndexOf("/")); 
     151   
     152    if ( visited_images_.containsKey(hash_dir)){
     153        Hashtable hashed_images = (Hashtable)visited_images_.get(hash_dir);   
     154       
     155            if (img_name.startsWith("screenview")){
     156        return true;
     157        }
     158
     159        if (hashed_images.containsKey(img_name)){
     160        return true;
     161        }
     162         
     163            Enumeration enu = hashed_images.keys();
     164            for(;enu.hasMoreElements();){
     165        String name = (String)enu.nextElement();
     166        if(name.startsWith("screenview")){
     167            return true;
     168        }
     169        }
     170   
     171            hashed_images.put(img_name,"visited");           
     172    }
     173    else{
     174        Hashtable hashed_images = new Hashtable();
     175        hashed_images.put(img_name,"visited");
     176        visited_images_.put(hash_dir,hashed_images);       
     177    }
     178         
     179    return false;
     180    }
     181
     182   
     183
     184
    144185
    145186    /** Restricts the type of images that can be included in the collage
     
    325366        // get the image from the url
    326367        if (verbosity_>=2) {
    327         //System.err.println("  ****Downloading image URL: " + url.toString());
    328         }
     368        System.err.println("  Downloading image URL: " + url.toString());
     369        }
     370           
     371           if (image_visited(url.toString(),img_name)) return;
    329372
    330373       int size = download_images_.downloadImage(tracker,url, from_url, img_name);
     
    354397    public void rec_add_images(String new_url, int depth)
    355398    {
    356 
     399   
    357400    if (already_visited(new_url)) return;
    358401
     
    377420        // get all the <code><img src=</code> links into a vector
    378421        Vector src_links = curl.getSrcLinks();
    379        
     422
     423               
    380424        if (verbosity_ >= 2) {
    381425        System.err.println("  Got src links... there are " + src_links.size() + " of them.");
     
    386430        URL url = (URL)src_links.get(i);
    387431        String url_string  = url.toString();
    388                
     432           
    389433        //System.err.println(" source links " + i + " ["  + url_string +"]");
    390434
     
    440484        for (int i = 0; i < href_links.size(); i++)
    441485        {
     486           
    442487            URL url = (URL)href_links.get(i);
    443488            String url_string  = url.toString();
    444 
    445             //System.err.println(" href links " + i + "["  + url_string +"]");
    446                    
     489            //System.err.println(" href links " + i + "["  + url_string +"]");     
    447490
    448491            if (image_file_extension(url_string))
    449492            {
    450                                      
     493                                 
    451494                if (filter_image(url_string))
    452495
    453496                {
     497                                       
    454498                    img_name = url_string.substring(url_string.lastIndexOf("/") + 1, url_string.length());
    455499
     
    471515                if (filter_href(url_string,new_url,depth))
    472516                {
     517       
    473518                    rec_add_images(url_string,depth+1);
    474519                   
     
    493538    public void externalLinks() {
    494539    external_links_ = null;
    495 
    496540    try {
    497541
    498         if (starting_url_.indexOf(document_root_) >= 0 ){
     542        if (starting_url_ == null || (document_root_ != null && starting_url_.indexOf(document_root_) >= 0) ){
    499543        return;
    500544        }
     
    510554        // split the line of the space, first part is the image, second part the link
    511555        while (l != null) {
    512 
     556   
    513557        String tmp1 = new String();
    514558        String tmp2 = new String();
     
    523567           
    524568            if (tmp2 != null) {
     569           
    525570            external_links_.put(tmp1, tmp2);
    526571            //System.err.println(tmp1 + " " + tmp2);
     
    543588    System.err.println("Starting download thread.");
    544589    visited_url_ = new Hashtable();
    545    
     590    visited_images_ = new Hashtable();
     591
    546592        rec_add_images(starting_url_,1);
    547593    download_images_.stopDownload();
Note: See TracChangeset for help on using the changeset viewer.