MIDP图形设计4

  3、TicTacToeMIDlet.java

  TicTacToeMIDlet非常简单:它处理MIDlet的生命周期事件。它根据需要创建屏幕对象并且处理来自屏幕的回调。ChoosePieceScreenDone回调被用来创建GameScreen。quit方法则被GameScreen用来结束游戏。

package example.tictactoe;
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
public class TicTacToeMIDlet extends MIDlet {
private ChoosePieceScreen choosePieceScreen;
private GameScreen gameScreen;
public TicTacToeMIDlet()
{
}
public void startApp() {
Displayable current = Display.getDisplay(this).getCurrent();
if (current == null) {
// first time we've been called
// Get the logo image
Image logo = null;
try
{
logo = Image.createImage("/tictactoe.png");
}
catch (IOException e) {
// just use null image
}
Alert splashScreen = new Alert(null, "Tic-Tac-Toe/nForum Nokia", logo, AlertType.INFO);


splashScreen.setTimeout(4000);
// 4 seconds
choosePieceScreen = new ChoosePieceScreen(this);
Display.getDisplay(this).setCurrent(splashScreen, choosePieceScreen);
}
else
{
Display.getDisplay(this).setCurrent(current);
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void quit()
{
destroyApp(false);
notifyDestroyed();
}
public void choosePieceScreenDone(boolean isPlayerCircle)
{
gameScreen = new GameScreen(this, isPlayerCircle);
Display.getDisplay(this).setCurrent(gameScreen);
}
}

  4、ChoosePieceScreen.java

  ChoosePieceScreen是一个基于高级应用编程接口窗体的屏幕,允许游戏者选择圆或叉作为棋子。当游戏者按下OK键时,它使用MIDlet的回调方法choosePieceScreenDone来处理游戏者的选择。

package example.tictactoe;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
public class ChoosePieceScreen extends List implements CommandListener
{
private static final String CIRCLE_TEXT = "Circle";
private static final String CROSS_TEXT = "Cross";
private final TicTacToeMIDlet midlet;
private final Command quitCommand;
public ChoosePieceScreen(TicTacToeMIDlet midlet) {
super("Choose your piece", List.IMPLICIT);
this.midlet = midlet;
append(CIRCLE_TEXT, loadImage("/circle.png"));
append(CROSS_TEXT, loadImage("/cross.png"));
quitCommand = new Command("Quit", Command.EXIT, 2);
addCommand(quitCommand);
setCommandListener(this);
}
public void commandAction(Command c, Displayable d) {
boolean isPlayerCircle = getString(getSelectedIndex()).equals(CIRCLE_TEXT);
if (c == List.SELECT_COMMAND) {
midlet.choosePieceScreenDone(isPlayerCircle);
}
else
// quit Command
{
midlet.quit();
}
}
private Image loadImage(String imageFile)
{
Image image = null;
try

{
image = Image.createImage(imageFile);
}
catch (Exception e)
{
// Use a 'null' image in the choice list (i.e. text only choices).
}
return image;
}
}
<script type="text/javascript"> <!-- google_ad_client = "pub-3051157228350391"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_type = "text_image"; google_ad_channel ="0045736275"; //--> </script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script><iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-3051157228350391&amp;dt=1120830291656&amp;format=728x90_as&amp;output=html&amp;channel=0045736275&amp;ad_type=text_image&amp;cc=17&amp;u_h=768&amp;u_w=1024&amp;u_ah=740&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=15&amp;u_java=true" frameborder="0" width="728" scrolling="no" height="90" allowtransparency="65535"></iframe>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值