trackererrortest.java

来自「java learn PPT java learn PPT java learn」· Java 代码 · 共 130 行

JAVA
130
字号
     import java.applet.*;
     import java.awt.*;
     import java.awt.image.*;
     import java.awt.geom.*;

     public class TrackerErrorTest extends Applet 
                                   implements Runnable
     {
        // 动画线程
        private Thread animation;

        // Image对象数组以及第一帧图像的索引
        private Image images[];
        private int firstIndex;

        // 要加载的图像数目
        private final int NUM_IMAGES = 6;

        // 一帧图像的宽和高
        private int imageWidth = 90;
        private int imageHeight = 90;

        public void init()
        {
            images = new Image[NUM_IMAGES];
            firstIndex = 0;

            // 为这个Component创建一个新的MediaTracker对象
            MediaTracker mt = new MediaTracker(this);
            java.net.URL baseURL = getDocumentBase();

            // 加载图像帧,并以0优先级添加到MediaTracker中去
            for(int i = 0; i < NUM_IMAGES; i++)
            {
                 images[i] = getImage(baseURL, "bot" + i + ".gif");
                 mt.addImage(images[i], 0);
            }
            
            try
            {    
                // 等待,直到图像完全加载再继续 
                mt.waitForID(0);
            }
            catch(InterruptedException e) { /* 什么也不做*/ }

            // 检查错误         
            if((mt.statusID(0, false) & MediaTracker.ERRORED) != 0) 
            {              
                 BufferedImage bi;     // 我们的"备用"图像
                 java.util.Random random = new java.util.Random();
                 
                 // BufferedImage的边界
                 Rectangle rect = new Rectangle(imageWidth, imageHeight);                  

                 // 通过检查宽度是否小于或者等于0来检测"坏"的图像
                 for(int i = 0; i < NUM_IMAGES; i++)
                 {
                      if(images[i].getWidth(this) <= 0)
                      {     
                           // 创建大小为(imageWidth x imageHeight)的缓冲图像 
                           bi = new BufferedImage(imageWidth, imageHeight,                                                                         BufferedImage.TYPE_INT_RGB);

                           // 用随机的颜色填充图像                 
                           Graphics2D g2d = bi.createGraphics();
                           g2d.setPaint(new Color(random.nextInt()));                     
                           g2d.fill(rect);

                           // 设置新的图像
                           images[i] = bi;
                      }
                  }
             }

            setBackground(Color.BLACK);
        }   // init
        
        public void start()
        {
            // 启动动画线程
            animation = new Thread(this);
            animation.start();
        }

        public void stop() 
        {
            animation = null;
        }

        public void run() 
        {
             Thread t = Thread.currentThread();
	     while (t == animation)
             {
                  try
                  {     Thread.sleep(100);
                  }
                  catch(InterruptedException e) 
                  {     break;
                  }

                  // 动画索引递增,如果需要的话循环
                  if(++firstIndex >= images.length)
                  {     firstIndex = 0;
                  }

                  repaint();
              }              
          }   // run

          public void paint(Graphics g) 
          {
               Graphics2D g2d = (Graphics2D)g;

               AffineTransform at = AffineTransform.getTranslateInstance(20, 20);

               // 绘制图像数组中的每一帧                
               int currFrame;     // 实际绘制的帧
               for(int i = 0; i < images.length; i++)
               {
                    currFrame = (firstIndex+i)%images.length;

                    g2d.setTransform(at);
                    g2d.drawImage(images[currFrame], null, this);

                    g2d.setPaint(Color.WHITE);
                    g2d.drawString("" + currFrame, imageWidth/2, imageHeight + 20);
                    at.translate(100, 0);
               }
          }    // paint
     }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?