您的位置:首页 > 编程语言 > Java开发

《Java编程技巧1001条》 第605条: 检测图像装入过程

2017-12-27 11:37 633 查看


《Java编程技巧1001条》第13部分 多媒体程序设计
第605条 检测图像装入过程

605 Overriding imageUpdate Method to Detect Progress of Imaging Loading
605 利用imageUpdate方法来检测图象安装是否成功
In the previous tip, you learned that Java calls the imageUpdate method as it loads a graphics image. By overriding the imageUpdate method, you can monitor the progress of the image-loading operation. The following applet, img_progress.java, displays progress messages to the status window to indicate that image loading is in progress:
在以上TIP中, 你已了解Java在安装图象时调用了imageUpdate方法. 通过imageUpdate的调用, 你可以监示图象安装操作的进行. 以下的小应用程序img_progress.java在状态行窗口中显示了过程信息,指示图象正在安装之中:
import java.applet.*;
import java.awt.*;

public class img_progress extends Applet implements Runnable {

   int load_index = 0;
   Thread progress = null;
   Image bg_img;
   String ld_str[]  = {"loading *   ", "loading  *  ",
                       "loading   * ", "loading    *"};

   public void init()
     {
       bg_img = getImage (getCodeBase(), "bgimg.jpg");
     }

   public void start()
     {
       if (progress == null)
         {
           progress = new Thread(this);
           progress.start();
         }
     }

   public void paint(Graphics g)
     {
       g.drawImage (bg_img, 0, 0, this);
     }   

   public void run()
     {
       while (progress != null)
         {
           load_index++;

           if (load_index > 3)
             load_index = 0;

           showStatus (ld_str[load_index]);

           try {
               Thread.sleep (300);
             } 
           catch (InterruptedException e) {};
         }
     }

   public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h)
     {
       if (img == bg_img)
         {
           if ((flags & ALLBITS) != 0)
             {
               if ( progress != null)
                 {
                    showStatus ("loaded");
                    progress.stop();
                 }
             }
         }
       return super.imageUpdate(img, flags, x, y, w, h);
     }
 }
*** kaj describe the processing

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: