本文主要是介绍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; } } |
|
<!-- 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"; //--> <iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-3051157228350391&dt=1120830291656&format=728x90_as&output=html&channel=0045736275&ad_type=text_image&cc=17&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_his=15&u_java=true" frameborder="0" width="728" scrolling="no" height="90" allowtransparency="65535"></iframe>
这篇关于MIDP图形设计4的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!