?? animatedimage.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 + -