Changeset 11562 for trunk/gsdl
- Timestamp:
- 2006-04-03T09:57:56+12:00 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl/src/java/org/nzdl/gsdl/GsdlCollageApplet/DisplayImages.java
r7334 r11562 61 61 62 62 /** Holds images currently on display in the applet */ 63 protected Vector inuse_ready_ = null; 64 65 /** Holds images currently on display in the applet */ 63 66 protected Vector inuse_ = null; 64 67 65 68 /** Holds previously displayed images for re-use */ 66 69 protected Vector notinuse_ = null; … … 70 73 protected Image finished_buffer_ = null; 71 74 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; 73 81 /** Used to determine where to place new images on the applet to optimize 74 82 * whitespace coverage <br> … … 95 103 * @param is_java2 whether advanced image processing techniques should be used 96 104 * @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) 99 107 { 100 108 super("DisplayImages"); … … 102 110 // parameters saved locally 103 111 app_ = app; 104 download_images_ = download_images;105 112 is_java2_ = is_java2; 106 113 bgcolor_ = bgcolor; 107 114 108 115 inuse_ = new Vector(); 116 inuse_ready_ = new Vector(); 109 117 notinuse_ = new Vector(); 110 118 download_images_ = download_images; 111 119 // sets width and height 112 120 if (is_java2_) … … 136 144 used_space[n][m] = 0; 137 145 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 138 171 } 139 172 … … 142 175 * @param y the y co-ordinate of the mouse 143 176 * @return the image that the user has clicked on or null if the user has clicked whitespace */ 144 145 177 public synchronized CollageImage clickedOnImage(int x, int y) 146 178 { … … 177 209 * images will be removed once there are greater than 25 images onscreen, to ensure 178 210 * the applet cannot be overloaded. */ 179 public synchronizedvoid display_collage()211 public void display_collage() 180 212 { 181 // get the number of images currently in the collage 213 214 // get the number of images currently in the collage 182 215 final int no_images = inuse_.size(); 216 183 217 184 218 // if there is nothing new to add … … 187 221 188 222 // otherwise need to re-generate images 189 if (app_.verbosity() > 3)223 if (app_.verbosity() >= 2) 190 224 { 191 System.err.print ("Regenerating images:");225 System.err.println("Regenerating images: " +no_images); 192 226 } 193 227 194 228 // get each image currently onscreen inturn 195 for (int i=0; i< no_images; i++)229 for (int i=0; i<inuse_.size(); i++) 196 230 { 197 231 CollageImage collage_image = (CollageImage)inuse_.elementAt(i); 198 232 199 233 Image image = collage_image.image_; 200 234 // images x and y dimensions … … 202 236 int image_y_dim = image.getHeight(app_); 203 237 204 int status = app_.checkImage(image,app_);238 //int status = app_.checkImage(image,app_); 205 239 206 240 if ((image_x_dim>0) && (image_y_dim>0)) … … 214 248 // places and sizes the image 215 249 MyAffineTransform af = new MyAffineTransform(image_x_dim,image_y_dim); 216 250 217 251 // sets location & size of collage image 218 252 collage_image.setAffineTransform(af, is_java2_); 219 253 220 if (is_java2_) {254 if (is_java2_) { 221 255 collage_image.image_ 222 = restoreAlpha(collage_image.image_, 0, 0,256 = restoreAlpha(collage_image.image_, 0, 0, 223 257 collage_image.image_x_dim_, collage_image.image_y_dim_); 258 224 259 } 225 260 … … 230 265 if (is_java2_) 231 266 { 267 268 232 269 // conducts fading and colouring of image 233 270 Image img = handlepixels(collage_image.image_, 0, 0, collage_image.image_x_dim_, 234 271 collage_image.image_y_dim_, i, collage_image.fresh ); 235 272 collage_image.image_ = img; 236 237 } 273 274 275 } 276 238 277 if (collage_image.fresh) { 239 278 collage_image.fresh = false; … … 255 294 } 256 295 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 260 300 // loops through removing oldest images that have almost faded completely 261 301 // and restricting the size of a simple collage to 25 images … … 272 312 273 313 CollageImage removed_image = (CollageImage)inuse_.elementAt(i); 274 inuse_.remove ElementAt(i);314 inuse_.remove(i); 275 315 276 316 // restores alpha channel to represent a solid image … … 301 341 302 342 CollageImage removed_image = (CollageImage)inuse_.elementAt(i); 303 inuse_.remove ElementAt(i);343 inuse_.remove(i); 304 344 305 345 // indicates space for this image is no longer used … … 321 361 } 322 362 } 323 324 } 325 363 364 365 } 366 367 326 368 /** Alters the images currently on display in the applet <br> 327 369 * Changes are prioritised in the following way: … … 334 376 * Then the collage is asked to display and repaint with the new image set. */ 335 377 336 synchronizedvoid next_frame()378 public void next_frame() 337 379 { 338 380 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) 359 388 { 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) 362 397 { 363 System.err.println(" name="+img_name+", url="+from_url);398 System.err.println("Re-using an image not on screen"); 364 399 } 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 } 383 401 // otherwise repaint the first image as the last image 384 402 else if (inuse_.size() > 0){ … … 393 411 removed_image.fresh = true; 394 412 395 if (is_java2_) {413 if (is_java2_) { 396 414 removed_image.image_ = restoreAlpha(removed_image.image_, 0, 0, 397 415 removed_image.image_x_dim_, removed_image.image_y_dim_); … … 399 417 400 418 notinuse_.addElement(removed_image); 401 inuse_.removeElementAt(0); 402 419 inuse_.remove(0); 403 420 if (app_.verbosity()>=4){ 404 421 System.err.println("Removing oldest image"); … … 409 426 System.err.println("No images in download area"); 410 427 } 411 412 display_collage();428 display_collage(); 429 413 430 } 414 431 … … 493 510 * @param fresh whether the image is being processed for the first time */ 494 511 495 public synchronizedImage 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) { 496 513 497 514 // declare an array to hold the pixels … … 606 623 * @param h the height of the image */ 607 624 public Image restoreAlpha(Image img, int x, int y, int w, int h) { 608 625 609 626 // declare an array to hold the pixels 610 627 int[] pixels = new int[w * h]; … … 612 629 // get the pixels of the image into the pixels array 613 630 PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w); 631 614 632 try { 633 615 634 pg.grabPixels(); 616 635 617 636 } catch (InterruptedException e) { 618 637 System.err.println("interrupted waiting for pixels!"); 619 638 return img; 620 639 } 640 641 621 642 // check for any failures 622 643 if ((pg.getStatus() & ImageObserver.ABORT) != 0) { … … 649 670 * @param h the height of the image */ 650 671 public boolean checkFaded (Image img, int x, int y, int w, int h) { 651 652 672 if (w == 0 || h == 0) 653 673 return false; … … 682 702 683 703 /** Paints the applet on the screen */ 684 public synchronizedvoid paint(Graphics g)704 public void paint(Graphics g) 685 705 { 706 686 707 screen_graphic_.setColor(bgcolor_); 687 708 screen_graphic_.fillRect(0,0,app_x_dim_,app_y_dim_); 688 689 int n = inuse_.size(); 690 709 691 710 // get each image currently onscreen inturn 692 for (int i=0; i < n; i++) 711 712 for (int i=0; i < inuse_.size(); i++) 693 713 { 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 696 725 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 } 700 729 } 701 730 702 731 // advanced image processing may be used 703 //else if (is_java2_) {704 else if (false) {705 // 732 else if (is_java2_) { 733 734 //the alphacomposite controls the blending of these two images. 706 735 Graphics2D sg2d = (Graphics2D)screen_graphic_; 707 //sg2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));736 sg2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); 708 737 sg2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 709 738 RenderingHints.VALUE_ANTIALIAS_ON); 710 739 collage_image.expand(); 711 740 sg2d.drawImage(collage_image.image_, collage_image.af_, app_); 712 741 } 713 742 // simple image collage 714 743 else 715 744 { 716 745 // add the image to the collage 746 717 747 screen_graphic_.drawImage(collage_image.image_, 718 748 collage_image.xl_, collage_image.yt_, … … 720 750 0,0,collage_image.image_x_dim_-1, 721 751 collage_image.image_y_dim_-1, app_); 722 723 } 724 725 752 } 753 726 754 } 727 755 … … 729 757 finished_graphic_.drawImage(screen_buffer_,0,0,app_); 730 758 759 731 760 if (finished_buffer_ != null) 732 761 { 733 762 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 735 771 } 736 772 737 773 /** Runs display thread by repeatedly calling the next frame, 738 774 * waiting for a specified delay period between calls */ 739 public void run() 740 { 775 public void run() { 741 776 try { 742 743 System.err.println("Running display thread"); 744 777 745 778 Thread curr_thread = Thread.currentThread(); 746 779 747 //long tm = System.currentTimeMillis();748 749 780 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 759 792 760 793 } 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 764 802 }
Note:
See TracChangeset
for help on using the changeset viewer.