一个经典的 JAVA ME 手机程序入门级源码

一个经典的 JAVA ME 手机程序入门级源码


一个由 Carol Hamer 写的比较有代表性的源码,作者全力推荐,尤其是对于没有 J2ME 开发经验的朋友。自己动手敲出以下贴出的 Hello.java 和 HelloCanvas.java 源码,并运行,用心体会一下。相信你理解了此源码之后,即可步入 J2ME 开发。


注释都在源码里,运行环境自己配,自己动手看运行效果,理解之后,然后动手修改一下源码,J2ME 开发,你可以的!


Hello.java 源码:

package net.frog_parrot.hello; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; /** * This is the main class of the hello world demo * * @author Carol Hamer * */ public class Hello extends MIDlet implements CommandListener { /** * The canvas is the region of the screen that has bean alloted to the game */ HelloCanvas myCanvas; /** * The Command objects apears as buttons. */ private Command exitCommand = new Command("Exit",Command.EXIT,99); /** * The Command objects apears as buttons. */ private Command newCommand = new Command("Toggle Msg",Command.SCREEN,1); /** * initialize the canvas and the commands */ public Hello() { myCanvas = new HelloCanvas(Display.getDisplay(this)); myCanvas.addCommand(exitCommand); myCanvas.addCommand(newCommand); myCanvas.setCommandListener(this); } //-------------------------------- // implementation of MIDlet /** * Start the application */ protected void startApp() throws MIDletStateChangeException { myCanvas.start(); } /** * This method is called to notify the MIDlet to enter a paused state. * The MIDlet should use this opportunity to release shared resources. */ protected void pauseApp() { } /** * If the MIDlet was using resources,it should release them in this method */ protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } //----------------------- // implementation of CommandListener. /** * Respond to a command issued on the Canvas * (either reset or exit) */ public void commandAction(Command c, Displayable s) { if(c == newCommand) { myCanvas.newHello(); }else if(c == exitCommand) { try { destroyApp(false); notifyDestroyed(); } catch (MIDletStateChangeException e) { } } } }


HelloCanvas.java 源码:

package net.frog_parrot.hello; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Font; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.game.GameCanvas; /** * This class represents the region of the screen that has bean alloted to the game * * @author Carol Hamer * */ public class HelloCanvas extends GameCanvas { //------------------------ //fields /** * A handle to the screen of the device */ Display myDisplay; /** * Whether or not the screen should currently display the "Hello world" message. */ boolean mySayHello = true; //------------------------------- //initialization and game state changes /** * Constructor merely sets the display */ public HelloCanvas(Display d) { super(false); myDisplay = d; } /** * This is called as soon as the application begins */ void start() { myDisplay.setCurrent(this); repaint(); } /** * toggle the hello message */ void newHello() { mySayHello = !mySayHello; //paint the display repaint(); } //------------------------------------ //graphics methods /** * clear the screen and display the hello world message if appropriate. */ public void paint(Graphics g) { // x and y are the coordinates of the top corner of the game's dispaly area int x = g.getClipX(); int y = g.getClipY(); // w and h are are the width and height of the display area: int w = g.getClipWidth(); int h = g.getClipHeight(); // clear the screen (paint it white): g.setColor(0xffffff); g.fillRect(x, y, w, h); //display the hello world message if appropriate: if(mySayHello) { Font font = g.getFont(); int fontHeight = font.getHeight(); int fontWidth = font.stringWidth("Hello World!"); // set the text color to red: g.setColor(0x00ff0000); g.setFont(font); // write the string in the center of the screen: g.drawString("Hello World!", (w-fontWidth)/2, (h-fontHeight)/2, g.TOP|g.LEFT); } } }


注:本文源码来自于 Carol Hamer 写的 《一步一步学习 midp2.0 编程》,这本书的中译本已经共享至我的 csdn 资源,pdf 格式的,有兴趣的朋友可以去下载下来看看。如果你还徘徊在 J2ME 游戏开发的大门之外,并且找不着比较好的入门资料和源代码,那么这本书就是为不得其门而入的你写的!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值