当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
计算机二级考试指导:我的第一个JavaMidlet
发布时间:2010/3/30 18:00:48 来源:城市学习网 编辑:admin

  我的第一个JavaMidlet
  最近开始学习为手机开发软件了。
  上网搜索了一下相关资料,然后就开始写程序了。
  值得一提的是,SUN公司目前提供的开发工具,已经相当成熟了。目前已经发展到Java Platform Micro Edition Software Development Kit 3.0了。用这套工具,可以写代码,编译,以及调试。最令人满意的是,它提供了Mac的版本,这样我就不用为了开发,而安装Windows了。
  写的代码,主要是为了了解开发的流程,以及基本的技巧。主要测试了canvas,font,drawstring等功能,学会了如何在屏幕上输出文字,和图形。
  用于屏幕输出的,主要有两种类型的控件(或者说是类),分别为screen和canvas;
  前者属于比较高级的控件,其跨平台的兼容性比较好。例如当你使用textbox(screen的子类)的时候,你不用关心坐标,字体和颜色等问题。这些都由平台负责管理。当然,你对它的
控制力度也变小了,你的是关注它的功能是否满足你的需求。
  如果用后者,则你需要关心的事情就多了。因为canvas相当于一块画布,上面画什么,在什么地方画,怎么画都需要你考虑。控制的力度是比较大,但是写程序也变得相当复杂。
  package hello;
  import javax.microedition.midlet.*;
  import javax.microedition.lcdui.*;
  public class HelloMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand; // The exit command
  private Command view;
  private Display display; // The display for this MIDlet
  public HelloMIDlet() {
  display = Display.getDisplay(this);
  exitCommand = new Command("Exit", Command.EXIT, 0);
  view = new Command("View", Command.ITEM, 1);
  }
  public void startApp() {
  TextBox t = new TextBox("Hello", "Hello, World!", 256, 0);
  t.addCommand(exitCommand);
  t.addCommand(view);
  t.setCommandListener(this);
  MyCanvas m=new MyCanvas();
  if (System.getProperty(
  "microedition.io.file.FileConnection.version") != null)
  t.setTicker(new Ticker(System.getProperty(
  "microedition.io.file.FileConnection.version")));
  else
  t.setTicker(new Ticker("no"));
  display.setCurrent(t);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
  if (c exitCommand) {
  destroyApp(false);
  notifyDestroyed();
  }
  }
  }
  package hello;
  import javax.microedition.lcdui.*;
  public class MyCanvas extends Canvas implements Runnable {
  int count;
  Font font;
  public MyCanvas() {
  Thread th=new Thread(this);
  th.start();
  font=Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_ITALIC, Font.SIZE_LARGE);
  }
  //Drawing on canvas
  public void paint(Graphics g) {
  g.setFont(font);
  g.setColor(0,122,122);
  g.fillRect(100,30,40,40);
  g.setColor(255,0,0);
  g.fillArc(100, 100, 40, 40, 0, 360);
  if(count0)
  {
  g.drawString("counter="+count,0,80,Graphics.TOPGraphics.LEFT);
  }
  else
  {
  g.setColor(255,255,255);
  g.drawString("counter="+(count - 1) ,0,80,Graphics.TOPGraphics.LEFT);
  g.setColor(0,122,122);
  g.drawString("counter="+count,0,80,Graphics.TOPGraphics.LEFT);
  }
  }
  //Handling keyEvents
  protected void keyPressed(int keyCode) {
  repaint();
  }
  public void run() {
  while(true) {
  count++;
  try {
  Thread.sleep(1000);
  }catch(Exception e){}
  repaint();
  count%=1000;
  }
  }
  }
  以上两个文件经过编译,打包之后,就形成2个文件,分别为HelloWorld.jad和HelloWorld.jar.
  通过蓝牙上传到我的dopod s900手机上之后,HelloWorld.jad文件,系统就会进行安装并运行。
  经过检测,我发现我的windows mobile手机支持file connection api,看来有得玩啦。

广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved