Changeset 11562 for trunk/gsdl


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

changed code handling threads

File:
1 edited

Legend:

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

    r7334 r11562  
    6161
    6262    /** Holds images currently on display in the applet */
     63    protected  Vector inuse_ready_    = null;
     64
     65    /** Holds images currently on display in the applet */
    6366    protected Vector inuse_    = null;
    64 
     67   
    6568    /** Holds previously displayed images for re-use */
    6669    protected Vector notinuse_ = null;
     
    7073    protected Image    finished_buffer_  = null;
    7174    protected Graphics finished_graphic_ = null;
    72 
     75 
     76    Thread next_frame_ ;
     77
     78    String dots[] = new String[]{" ",".","..","...","....",".....","......"};
     79    int dots_nums=0;
     80    boolean start_paint = false;
    7381    /** Used to determine where to place new images on the applet to optimize
    7482     *  whitespace coverage <br>
     
    95103     *  @param is_java2 whether advanced image processing techniques should be used
    96104     *  @param bgcolor the background colour of the applet screen */
    97     public DisplayImages(GsdlCollageApplet app,
    98              DownloadImages download_images, boolean is_java2, Color bgcolor)
     105    public DisplayImages(GsdlCollageApplet app, DownloadImages download_images,
     106             boolean is_java2, Color bgcolor)
    99107    {
    100108    super("DisplayImages");
     
    102110    // parameters saved locally
    103111    app_             = app;
    104     download_images_ = download_images;
    105112    is_java2_        = is_java2;
    106113    bgcolor_         = bgcolor;
    107114
    108115    inuse_    = new Vector();
     116        inuse_ready_   = new Vector(); 
    109117    notinuse_ = new Vector();
    110 
     118    download_images_ = download_images;
    111119    // sets width and height
    112120    if (is_java2_)
     
    136144        used_space[n][m] = 0;
    137145
     146        next_frame_ =  new Thread(new Runnable(){
     147                public void run() {
     148            try {
     149
     150            Thread curr_thread = Thread.currentThread();
     151       
     152            while (curr_thread == next_frame_) {
     153                next_frame();
     154               
     155                Thread.sleep(app_. refreshDelay_);
     156                curr_thread = Thread.currentThread();
     157            }
     158
     159       
     160            } catch (Exception e) {
     161            e.printStackTrace();
     162            }
     163        }
     164
     165
     166        });
     167
     168       next_frame_.start();
     169
     170
    138171    }
    139172
     
    142175     *  @param y the y co-ordinate of the mouse
    143176     *  @return the image that the user has clicked on or null if the user has clicked whitespace */
    144  
    145177   public synchronized CollageImage clickedOnImage(int x, int y)
    146178    {
     
    177209     *  images will be removed once there are greater than 25 images onscreen, to ensure
    178210     *  the applet cannot be overloaded. */
    179     public synchronized void display_collage()
     211    public void display_collage()
    180212    {
    181     // get the number of images currently in the collage
     213
     214        // get the number of images currently in the collage
    182215    final int no_images = inuse_.size();
     216   
    183217
    184218    // if there is nothing new to add
     
    187221   
    188222    // otherwise need to re-generate images
    189     if (app_.verbosity() > 3)
     223    if (app_.verbosity() >= 2)
    190224    {
    191         System.err.print("Regenerating images:");
     225        System.err.println("Regenerating images: " +no_images);
    192226    }
    193227   
    194228    // get each image currently onscreen inturn
    195     for (int i=0; i<no_images; i++)
     229    for (int i=0; i<inuse_.size(); i++)
    196230    {
    197231        CollageImage collage_image = (CollageImage)inuse_.elementAt(i);
    198 
     232       
    199233        Image image = collage_image.image_;
    200234        // images x and y dimensions
     
    202236        int image_y_dim = image.getHeight(app_);
    203237
    204         int status = app_.checkImage(image,app_);
     238        //int status = app_.checkImage(image,app_);
    205239
    206240        if ((image_x_dim>0) && (image_y_dim>0))
     
    214248            // places and sizes the image
    215249            MyAffineTransform af = new MyAffineTransform(image_x_dim,image_y_dim);
    216            
     250       
    217251            // sets location & size of collage image
    218252            collage_image.setAffineTransform(af, is_java2_);
    219253           
    220             if (is_java2_) {
     254             if (is_java2_) {
    221255                collage_image.image_
    222                 = restoreAlpha(collage_image.image_, 0, 0,
     256                 = restoreAlpha(collage_image.image_, 0, 0,
    223257                           collage_image.image_x_dim_, collage_image.image_y_dim_);
     258               
    224259            }
    225260           
     
    230265        if (is_java2_)
    231266        {
     267             
     268 
    232269            // conducts fading and colouring of image
    233270            Image img = handlepixels(collage_image.image_, 0, 0, collage_image.image_x_dim_,
    234                          collage_image.image_y_dim_, i, collage_image.fresh );
     271                        collage_image.image_y_dim_, i, collage_image.fresh );
    235272            collage_image.image_ = img;
    236            
    237         }
     273       
     274 
     275        }
     276
    238277        if (collage_image.fresh) {
    239278            collage_image.fresh = false;
     
    255294    }
    256295
    257     /** loops through removing oldest images and reduces number of images if excessive overlap is occuring */
    258     synchronized void remove_old_images() {
    259 
     296
     297 /** loops through removing oldest images and reduces number of images if excessive overlap is occuring */
     298    void remove_old_images() {
     299   
    260300    // loops through removing oldest images that have almost faded completely
    261301    // and restricting the size of a simple collage to 25 images
     
    272312
    273313            CollageImage removed_image = (CollageImage)inuse_.elementAt(i);
    274             inuse_.removeElementAt(i);
     314            inuse_.remove(i);
    275315
    276316            // restores alpha channel to represent a solid image
     
    301341
    302342            CollageImage removed_image = (CollageImage)inuse_.elementAt(i);
    303             inuse_.removeElementAt(i);
     343            inuse_.remove(i);
    304344
    305345            // indicates space for this image is no longer used
     
    321361        }
    322362    }
    323 
    324     }
    325 
     363     
     364     
     365    }
     366
     367 
    326368    /** Alters the images currently on display in the applet <br>
    327369     *  Changes are prioritised in the following way:
     
    334376     *  Then the collage is asked to display and repaint with the new image set. */
    335377
    336     synchronized void next_frame()
     378     public void next_frame()
    337379    {
    338380    double random = Math.random();
    339    
    340     // if there is a new image in the downloaded vector
    341     if (download_images_.size()>0)
    342     {
    343         int position = (int) ((download_images_.size() - 1) * random);
    344 
    345         // get the image and its associated url (and remove from download area)
    346         DownloadImages.ImageUrlTriple iutriple = download_images_.retrieve(position);
    347         Image image     = iutriple.image();
    348         String from_url = iutriple.url();
    349         String img_name = iutriple.name();
    350 
    351         // prepare the image for collaging
    352         // app_.prepareImage(image,app_);
    353         CollageImage collage_image = new CollageImage(image,from_url,img_name);
    354        
    355         // add it to the vector of images currently in use
    356         inuse_.addElement(collage_image);       
    357        
    358         if (app_.verbosity()>=1)
     381             
     382
     383    if (inuse_ready_.size() > 0){
     384        int pos = (int) ((inuse_ready_.size() - 1) * random);
     385        inuse_.addElement(inuse_ready_.remove(pos));
     386    }
     387        else if (notinuse_.size()>0)
    359388        {
    360         System.err.println("Adding in a newly downloaded image");
    361         if (app_.verbosity()>=2)
     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)
    362397            {
    363             System.err.println("  name="+img_name+", url="+from_url);
     398            System.err.println("Re-using an image not on screen");
    364399            }
    365 
    366         }
    367     }
    368     // use an image that has already been displayed but is no longer in the collage
    369     else if (notinuse_.size()>0)
    370     {
    371         int position = (int) ((notinuse_.size() - 1) * random);
    372         Image image;
    373 
    374         CollageImage collage_image = (CollageImage)notinuse_.elementAt(position);
    375         notinuse_.removeElementAt(position);
    376         inuse_.addElement(collage_image);
    377 
    378         if (app_.verbosity()>3)
    379         {
    380         System.err.println("Re-using an image not on screen");
    381         }
    382     }
     400        }
    383401    // otherwise repaint the first image as the last image
    384402    else if (inuse_.size() > 0){
     
    393411        removed_image.fresh = true;
    394412       
    395         if (is_java2_) {
     413         if (is_java2_) {
    396414        removed_image.image_ = restoreAlpha(removed_image.image_, 0, 0,
    397415                            removed_image.image_x_dim_, removed_image.image_y_dim_);
     
    399417       
    400418        notinuse_.addElement(removed_image);
    401         inuse_.removeElementAt(0);
    402        
     419        inuse_.remove(0);
    403420        if (app_.verbosity()>=4){
    404421        System.err.println("Removing oldest image");
     
    409426        System.err.println("No images in download area");
    410427    }
    411    
    412     display_collage();
     428          display_collage();
     429   
    413430    }
    414431   
     
    493510     *  @param fresh whether the image is being processed for the first time */
    494511
    495     public synchronized Image handlepixels(Image img, int x, int y, int w, int h, int p, boolean fresh) {
     512    public Image handlepixels(Image img, int x, int y, int w, int h, int p, boolean fresh) {
    496513   
    497514    // declare an array to hold the pixels
     
    606623     *  @param h the height of the image */
    607624    public Image restoreAlpha(Image img, int x, int y, int w, int h) {
    608    
     625     
    609626    // declare an array to hold the pixels
    610627    int[] pixels = new int[w * h];
     
    612629    // get the pixels of the image into the pixels array
    613630    PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
     631
    614632    try {
     633     
    615634        pg.grabPixels();
    616    
     635         
    617636    } catch (InterruptedException e) {
    618637        System.err.println("interrupted waiting for pixels!");
    619638        return img;
    620639    }
     640
     641     
    621642    // check for any failures
    622643    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
     
    649670     *  @param h the height of the image */
    650671    public boolean checkFaded (Image img, int x, int y, int w, int h) {
    651 
    652672     if (w == 0 || h == 0)
    653673         return false;
     
    682702
    683703    /** Paints the applet on the screen */
    684     public synchronized void paint(Graphics g)
     704    public void paint(Graphics g)
    685705    {
     706
    686707    screen_graphic_.setColor(bgcolor_);
    687708    screen_graphic_.fillRect(0,0,app_x_dim_,app_y_dim_);
    688    
    689     int n = inuse_.size();
    690    
     709
    691710    // get each image currently onscreen inturn
    692     for (int i=0; i < n; i++)
     711   
     712    for (int i=0; i < inuse_.size(); i++)
    693713        {
    694         CollageImage collage_image = (CollageImage)inuse_.elementAt(i);
    695         // don't paint new images yet
     714         
     715            CollageImage collage_image =null;
     716           try{
     717           collage_image = (CollageImage)inuse_.elementAt(i);
     718           start_paint = true;
     719        }
     720            catch(Exception e){
     721          System.out.println("exception in paint");                 
     722              break;
     723         }
     724       // don't paint new images yet
    696725        if (collage_image.fresh) {
    697             if (app_.verbosity()>3) {
    698             System.err.println("doing nothing... image " + i + " of "+n+ " images in use is fresh.");
    699             }
     726          if (app_.verbosity()>=1) {
     727            System.err.println("doing nothing... images in use is fresh.");
     728           }
    700729        }
    701730
    702731        // advanced image processing may be used
    703         //      else if (is_java2_) {
    704         else if (false) {
    705             // the alphacomposite controls the blending of these two images.
     732        else if (is_java2_) {
     733         
     734            //the alphacomposite controls the blending of these two images.
    706735            Graphics2D sg2d = (Graphics2D)screen_graphic_;
    707             //sg2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
     736            sg2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
    708737            sg2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    709738                          RenderingHints.VALUE_ANTIALIAS_ON);
    710739            collage_image.expand();
    711740            sg2d.drawImage(collage_image.image_, collage_image.af_, app_);
    712             }
     741           }
    713742        // simple image collage
    714743        else
    715744            {
    716745            // add the image to the collage
     746           
    717747            screen_graphic_.drawImage(collage_image.image_,
    718748                          collage_image.xl_, collage_image.yt_,
     
    720750                          0,0,collage_image.image_x_dim_-1,
    721751                          collage_image.image_y_dim_-1,  app_);
    722        
    723             }
    724        
    725        
     752            }
     753   
    726754        }
    727755   
     
    729757    finished_graphic_.drawImage(screen_buffer_,0,0,app_);
    730758   
     759
    731760    if (finished_buffer_ != null)
    732761        {
    733762        g.drawImage(finished_buffer_, 0, 0, app_);
    734         }
     763        if (!start_paint){
     764            g.setColor(Color.white);
     765            g.setFont(new Font("font",Font.PLAIN+Font.BOLD,30));
     766            g.drawString("Downloading" + dots[(dots_nums++)%dots.length],app_x_dim_/3-20,app_y_dim_/2-2);
     767        }
     768        }
     769   
     770
    735771    }
    736772
    737773    /** Runs display thread by repeatedly calling the next frame,
    738774     *  waiting for a specified delay period between calls */
    739     public void run()
    740     {
     775    public void run() {
    741776    try {
    742 
    743         System.err.println("Running display thread");
    744 
     777       
    745778        Thread curr_thread = Thread.currentThread();
    746779       
    747         //long tm = System.currentTimeMillis();
    748    
    749780        while (curr_thread == this) {
    750         next_frame();
    751         //tm += app_.refreshDelay();
    752         //Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
    753         Thread.sleep(app_.refreshDelay());
    754         //Thread.sleep(2);
    755         curr_thread = Thread.currentThread();
    756         }
    757 
    758         System.err.println("Display thread finished");
     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       
    759792       
    760793    } catch (Exception e) {
    761         e.printStackTrace();
    762     }
    763     }
     794        //e.printStackTrace();
     795           System.out.println("Display images thread is interrupted");
     796    }
     797
     798    System.out.println("Display images thread stoped");
     799         
     800    }
     801
    764802}
Note: See TracChangeset for help on using the changeset viewer.