---
title: "MIDP图形设计4"
author: "fanjava"
date: "2005-07-08T21:46:00Z"
tags: []
slug: "midp-4"
draft: false
lastmod: "2005-07-08T21:46:00Z"
id: 601704
url: "https://fanjava.iteye.com/blog/601704"
---

**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;  
}  
}

&lt;!-- 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"; //--&gt; &lt;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"&gt;&lt;/iframe&gt;