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

📄 bird_image.java

📁 本光盘是《J2ME无线移动游戏开发》一书的配套光盘
💻 JAVA
字号:
package ch08;

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

public class Bird_Image
    extends TimerTask {

  //声明一个Canvas对象
  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 Bird_Image(Image[] images) {
    this(null, images, null);
  }

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

  // Construct an animation. The canvas can be null, but
  // if not null then a repaint will be triggered on it
  // each time the image changes due to a timer event.
  // If a clip list is specified, the image is drawn
  // multiple times, each time with a different clip
  // rectangle, to simulate transparent parts.
  /*
   11.构造器
   */
  public Bird_Image(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();
    }
  }

  // Move to the next frame, wrapping if necessary.
  /*
   12.
   */
  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();
    }
  }

  // Draw the current image in the animation.  If
  // no clip list, just a simple copy, otherwise
  // set the clipping rectangle accordingly and
  // draw the image multiple times.
  /*
   13.图形绘制
   */
  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);
    }
  }

  // Moves the animation's top left corner.
  /*
   14.改变坐标
   */
  public void move(int x, int y) {
    this.x = x;
    this.y = y;
  }

  // Invoked by the timer. Advances to the next frame
  // and causes a repaint if a canvas is specified.

  /*
   15.实现run()方法
   */
  public void run() {
    if (w == 0 || h == 0) {
      return;
    }
    advance(true);
  }
}

⌨️ 快捷键说明

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