animationimagemidlet.java
来自「example2 众多JAVA实例源码...学习java基础的好帮手」· Java 代码 · 共 81 行
JAVA
81 行
package opusmicro.demos.animate;
import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
// MIDlet that displays some simple animations.
// Displays a series of birds on the screen and
// animates them at different (random) rates.
public class AnimationImageMIDlet 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( "Exit", Command.EXIT, 1 );
public AnimationImageMIDlet(){
}
public void commandAction( Command c, Displayable d ){
if( c == exitCommand ){
exitMIDlet();
}
}
protected void destroyApp( boolean unconditional )throws MIDletStateChangeException {
exitMIDlet();
}
public void exitMIDlet(){
timer.cancel(); // turn it off...
notifyDestroyed();
}
// Generate a non-negative random number...
private int genRandom( int upper ){
return( Math.abs( random.nextInt() ) % upper );
}
public Display getDisplay(){ return display; }
// Initialize things by creating the canvas and then
// creating a series of birds that are moved to
// random locations on the canvas and attached to
// a timer for scheduling.
protected void initMIDlet(){
try {
AnimatedCanvas c = new AnimatedCanvas( getDisplay() );
Image[] images = loadFrames( "/image", 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( "Could not load images" );
exitMIDlet();
}
}
// Load the bird animation, which is stored as a
// series of PNG files in the MIDlet suite.
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 + =
减小字号Ctrl + -
显示快捷键?