⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 animation.java

📁 Java学习源代码检索系统免费版
💻 JAVA
字号:
//==============================================================
// Animation.java - Demonstrates image animation
//
// Java学习源代码检索系统 Ver 1.0 20031015 免费正式版
// 版权所有: 中国IT认证实验室(www.ChinaITLab.com)
// 程序制作: ChinaITLab网校教研中心
// 主页地址: www.ChinaITLab.com    中国IT认证实验室
// 论坛地址: bbs.chinaitlab.com  
// 电子邮件: Java@ChinaITLab.com
//==============================================================

import java.applet.*;
import java.awt.*;

public class Animation extends Applet 
 implements Runnable {

 // Thread for loading and displaying images
 Thread animThread = null;

 private final int NUM_IMAGES = 10; // Number of image files
 private Image images[];            // Array of images
 private int currImage;             // Index of current image
 private int imgWidth  = 0;         // Width of all images
 private int imgHeight = 0;         // Height of all images
 private boolean allLoaded = false; // true = all loaded
 private MediaTracker tracker;      // Tracks image loading
 private int width, height;         // Applet width and height

 // Initialize applet
 public void init() {
  width = 320;
  height = 240;
  resize(width, height);
  // Create MediaTracker object. The string is
  // for creating the image filenames.
  tracker = new MediaTracker(this);
  String strImage;
  // Load all images. Method getImage() returns immediately
  // and all images are NOT actually loaded into memory
  // by this loop.
  images = new Image[NUM_IMAGES];  // Create image array
  for (int i = 1; i <= NUM_IMAGES; i++) {
   strImage = "images/img00" + ((i < 10) ? "0" : "") 
    + i + ".gif";
   images[i-1] = getImage(getDocumentBase(), 
    strImage);
   tracker.addImage(images[i-1], 0);
  }
 }

 // Paint window contents
 public void paint(Graphics g)
 {
  // Draw current image
  if (allLoaded) {
   g.drawImage(images[currImage],
    (width - imgWidth) / 2,
    (height - imgHeight) / 2, null);
  }
 }

 // Create and start animation thread
 public void start() {
  if (animThread == null) {
   animThread = new Thread(this);
   animThread.start();
  }
 }

 // Run image load and display thread
 public void run() {
  // Load images if not already done
  if (!allLoaded) {
   showStatus("Loading images...");
   // Wait for images to be loaded
   // Other processes continue to run normally
   try  {
    tracker.waitForAll();
   }
   catch (InterruptedException e) {
    stop();  // Stop thread if interrupted
    return;  // Abort loading process
   }
   // If all images are not loaded by this point, 
   // something is wrong and we display an error 
   // message.
   if (tracker.isErrorAny()) {
    showStatus("Error loading images!");
    stop();
    return;
   }
   
   // All images are loaded. Set the loaded flag
   // and prepare image size variables
   allLoaded = true;
   imgWidth  = images[0].getWidth(this);
   imgHeight = images[0].getHeight(this);
  }
  
  // Loop endlessly so animation repeats
  // User ends loop by leaving the page or exiting
  // the browser.
  showStatus("Displaying animation");
  while (true) {
   try {
    repaint();
    currImage++;
    if (currImage == NUM_IMAGES)
     currImage = 0;
    Thread.sleep(50);  // Controls animation speed
   }
   catch (InterruptedException e) {
    stop();
   }
  }  // end of while statement
 }  // end of run() method
}

⌨️ 快捷键说明

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