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

《Java编程技巧1001条》第607条: 利用MediaTraker预装图像

2017-12-27 13:49 471 查看


《Java编程技巧1001条》第13部分 多媒体程序设计
第607条 利用MediaTraker预装图像

607 Using MediaTracker to Preload Images 
607 利用 MediaTracker(媒体轨道,媒体轨迹,媒体航线) 来预装图象
As you learned in the gif_anime.java animation you created in Tip xxx, image flickering is a problem during screen update operations. Even if your applet overrides the update method to remove the screen erase, flickering may still occur as the image loads. To solve this problem, your program can wait until the applet loads all of its images before it starts the animation. To help you preload images in this way, Javas AWT library provides the MediaTracker class. In the future, your programs may use the MediaTracker class to track your applets loading of various media objects, such as images and audio clips. Currently, the MediaTracker only tracks the loading of images. 你在TIP 606所创建的动画 gif_anime.java 中已看到, 图象的闪动是在屏幕的刷新操作过程中引起的. 即使你在小应用程序中利用update方法去掉了清屏操作,由于图象的装入而仍然会出现闪动. 为了解决这一个问题, 你的程序可以在动画开始之前把所有的图象预先都装入(内存). 为了帮助你用这一种办法预装图象, Java的AWT库提供了MediaTracker类. 今后, 你的程序可以利用MediaTracker类为你的小应用程序预装各种多媒体对象,包括图象和音频剪切. 而现在只用 MediaTracker 来预装图象.    To use Javas MediaTracker to track images, you must first create a MediaTracker object. Then, you must call the addImage method to add an image to the tracker. The formats of the addImage methods are as follows:为了利用Java的 MediaTracker类来预装图象, 你必须首先创建一个 MediaTracker 对象. 然后再调用ddImage方法在轨道(traker)中装入图象. addImage方法的格式如下:
void addImage(Image image, int id);
void addImage(Image image, int id, int w, int h);

The image parameter specifies the loading of the image that you want to track. The id parameter specifies an identifier which your program can use later to retrieve information about the images. The w and h parameters of the second addImage method specify the width and height of the area within which you want the image rendered. When you use the addImage method to add images to the media tracker, you are not limited to assigning one id number to one image. In this way, you can add multiple images to the tracker with the same id number to form a tracking group. Two MediaTracker class methods you will use to synchronize your graphics operations are waitForAll and waitForId. The formats of these two methods are as follows:
参数image指定了你想装入的图象.参数id是一个标识符, 你的程序以后可利用它来提取图象有关的信息. 在第二个addImage方法中出现的参数w和h是代表你要画的图象区域的宽度和高度. 当你使用addImage方法把图象加入到多媒体轨迹中去时, 你不用限制一幅图象用一个id号. 采用这种方法后, 你就可以利用同一个id在轨迹中加入多
幅图象以形成轨迹群. MediaTracker类中用来同步你的图象操作的方法是waitForAll 和 waitForId. 这两个方法的格式如下:

void waitForAll();
void waitForID(int id);
fix xxx
The waitForID method will not return control to the caller until the images in the tracker with the specified id are loaded. The waitForAll method will not return control to the caller until all images in the tracker are loaded. The following applet, tracked_anime.java, provides an improved version of the gif_anime.java applet that you created in Tip xxx. The animation will not start until all  images are loaded.
在指定了id的图象没有装入轨迹之前,waitForID方法不返回控制到调用者.而waitForAll方法在轨迹中所有的图象没有装入之前不把控制返回给调用者. 以下的小应用程序 tracked_anime.java 提供了TIP 606小应用程序中创建的gif_anime.java 的一个改进版. 动画要在所有的图象装入后才开始执行。
import java.applet.*;
import java.awt.*;

public class tracked_anime extends Applet implements Runnable {

   int img_index = 0;
   Thread anime = null;
   String img_names[] = {"java1.gif", "java2.gif",  "java3.gif",
                         "java4.gif", "java5.gif",  "java6.gif"};

   Image java_img[] = new Image[6];

   public void init()
     {
       MediaTracker mt = new MediaTracker(this);

       for (int i = 0; i < 6; i++)
          {
            java_img[i] = getImage (getCodeBase(), img_names[i]);
            mt.addImage (java_img[i], i);
          }

       try {
           mt.waitForAll();
         } 
       catch (InterruptedException e) {};
     }

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

   public void stop()
     {
       if (anime == null)
         {
            anime.stop();
         }
     }

   public void paint(Graphics g)
     {
       g.drawImage (java_img[img_index], 0, 0, this);
     }   

   public void run()
     {
       while (anime != null)
         {
           img_index++;

           if (img_index > 5) 
             img_index = 0;

           paint (getGraphics());

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

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