trackertest.java

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

JAVA
107
字号
     import java.applet.*;
     import java.awt.*;
     import java.awt.geom.*;

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

        // Image对象数组,还有第一个图像的索引
        private Image images[];
        private int firstIndex;

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

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

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

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

            // 加载图像帧,并以优先级0添加到MediaTracker 中
            // priority of 0            
            for(int i = 0; i < NUM_IMAGES; i++)
            {
                 images[i] = getImage(baseURL, "fire" + i + ".gif");
                 mt.addImage(images[i], 0);
            }
            
            try
            {    
                // 等待,直到图像完全加载再继续 
                mt.waitForID(0);
            }
            catch(InterruptedException e) { /* 什么也不做 */ }
         
            // 现在我们可以肯定图像已经加载,现在获取它们的宽和高
            // access their width and height
            imageWidth = images[0].getWidth(this);
            imageHeight = images[0].getHeight(this);

            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;     // the actual frame to draw
               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 + -
显示快捷键?