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

📄 animatedimage.java

📁 JAVA编程百例书中各章节的所有例子的源代码,包括套接字编程
💻 JAVA
字号:
package ch09.section05;

import java.util.*;
import javax.microedition.lcdui.*;

// 定义了一个动画,该动画其实只是一系列相同大小的图片
// 轮流显示,然后模拟出的动画
public class AnimatedImage
    extends TimerTask {
  private Canvas canvas;
  private Image[] images;
  private int[][] clipList;
  private int current;
  private int x;
  private int y;
  private int w;
  private int h;

  //构造一个无画布的动画
  public AnimatedImage(Image[] images) {
    this(null, images, null);
  };

  public AnimatedImage(Canvas canvas, Image[] images) {
    this(canvas, images, null);
  };

  //构造动画
  public AnimatedImage(Canvas canvas, Image[] images, int[][] clipList) {
    this.canvas = canvas;
    this.images = images;
    this.clipList = clipList;
    if (images != null && clipList != null) {
      if (clipList.length < images.length) {
        throw new IllegalArgumentException();
      }
    }
    if (images != null && images.length > 0) {
      w = images[0].getWidth();
      h = images[0].getHeight();
    }
  }

  //不同帧之间的替换
  public void advance(boolean repaint) {
    if (++current >= images.length) {
      current = 0;
    }
    if (repaint && canvas != null && canvas.isShown()) {
      canvas.repaint(x, y, w, h);
      canvas.serviceRepaints();
    }
  }

  //绘制当前帧
  public void draw(Graphics g) {
    if (w == 0 || h == 0) {
      return;
    }
    int which = current;
    if (clipList == null || clipList[which] == null) {
      g.drawImage(images[which], x, y, g.TOP | g.LEFT);
    }
    else {
      int cx = g.getClipX();
      int cy = g.getClipY();
      int cw = g.getClipWidth();
      int ch = g.getClipHeight();
      int[] list = clipList[which];
      for (int i = 0; i + 3 <= list.length; i += 4) {
        g.setClip(x + list[0], y + list[1], list[2], list[3]);
        g.drawImage(images[which], x, y, g.TOP | g.LEFT);
      }
      g.setClip(cx, cy, cw, ch);
    }
  }

  //移动动画到左上角
  public void move(int x, int y) {
    this.x = x;
    this.y = y;
  }

//该方法由Timer控制调用
  public void run() {
    if (w == 0 || h == 0) {
      return;
    }
    advance(true);
  }
}

⌨️ 快捷键说明

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