1

/**//*2
* To change this template, choose Tools | Templates3
* and open the template in the editor.4
*/5

6
import javax.microedition.lcdui.*;7
import javax.microedition.midlet.*;8

9

10

public class UFOMidlet extends MIDlet implements CommandListener
{11

12
private UFOCanvas canvas;13

public void startApp()
{14

if(canvas==null)
{15
canvas=new UFOCanvas(Display.getDisplay(this));16
Command exit=new Command("exit",Command.EXIT,0);17
canvas.addCommand(exit);18
canvas.setCommandListener(this);19
}20
canvas.start();21
}22

23

public void pauseApp()
{24
}25

26

public void destroyApp(boolean unconditional)
{27
canvas.stop();28
}29

30

public void commandAction(Command c, Displayable d)
{31

if(c.getCommandType()==Command.EXIT)
{32
destroyApp(true);33
notifyDestroyed();34
}35
}36
}37
1

2
import javax.microedition.lcdui.Display;3
import javax.microedition.lcdui.Graphics;4
import javax.microedition.lcdui.game.GameCanvas;5

6

7

public class UFOCanvas extends GameCanvas implements Runnable
{8

UFOCanvas(Display d)
{9
super(true);10
display=d;11
ufoSprite=new UFO();12

13
}14

15

public void run()
{16
//主函数17

18

while(!sleep)
{19
update();20
draw();21

try
{22
Thread.sleep(delay);23

} catch (InterruptedException ex)
{24

25
}26

27
}28
}29

public void update()
{30
ufoSprite.reflsh();31
}32

public void draw()
{33
Graphics g=getGraphics();34
g.setColor(0x000000);35
g.fillRect(0, 0, getWidth(), getHeight());36
ufoSprite.getUFO().paint(g);37
flushGraphics();38
}39

public void stop()
{40
sleep=true;41
}42

public void start()
{43
display.setCurrent(this);44
sleep=false;45
Thread t=new Thread(this);46
t.start();47
}48

49

50
boolean sleep;51
long delay=1000;52
Display display;53
UFO ufoSprite;54

55
}56
