📄 animationtest.java
字号:
package example.animate;
import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
//该MIDlet用于显示一个动画。
public class AnimationTest
extends MIDlet
implements CommandListener {
//定义动画的帧数
private static final int BIRD_FRAMES = 7;
//窗体上同时显示的动画数
private static final int NUM_BIRDS = 5;
private Display display;
private Timer timer = new Timer();
private AnimatedImage[] birds;
private Random random = new Random();
public static final Command exitCommand = new Command("退出", Command.EXIT, 1);
//响应退出按钮
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
exitMIDlet();
}
}
//销毁MIDLET
protected void destroyApp(boolean unconditional) throws
MIDletStateChangeException {
exitMIDlet();
};
public void exitMIDlet() {
//取消任务
timer.cancel();
notifyDestroyed();
};
//生成一个非负的随机数
private int genRandom(int upper) {
return (Math.abs(random.nextInt()) % upper);
};
public Display getDisplay() {
return display;
};
protected void initMIDlet() {
try {
AnimatedCanvas c = new AnimatedCanvas(getDisplay());
Image[] images = loadFrames("/images/bird", BIRD_FRAMES);
int w = c.getWidth();
int h = c.getHeight();
birds = new AnimatedImage[NUM_BIRDS];
for (int i = 0; i < NUM_BIRDS; ++i) {
AnimatedImage b = new AnimatedImage(c, images);
birds[i] = b;
b.move(genRandom(w), genRandom(h));
c.add(b);
timer.schedule(b, genRandom(1000), genRandom(400));
}
c.addCommand(exitCommand);
c.setCommandListener(this);
getDisplay().setCurrent(c);
}
catch (IOException e) {
System.out.println("装入图像失败");
exitMIDlet();
}
}
//装入动画图像,图像以PNG格式存放
private Image[] loadFrames(String name, int frames) throws IOException {
Image[] images = new Image[frames];
for (int i = 0; i < frames; ++i) {
images[i] = Image.createImage(name + i + ".png");
}
return images;
};
protected void pauseApp() {
};
protected void startApp() throws MIDletStateChangeException {
if (display == null) {
display = Display.getDisplay(this);
initMIDlet();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -