Chinaunix首页 | 论坛 | 博客
  • 博客访问: 413023
  • 博文数量: 116
  • 博客积分: 7087
  • 博客等级: 少将
  • 技术积分: 1175
  • 用 户 组: 普通用户
  • 注册时间: 2005-02-19 23:32
文章分类

全部博文(116)

文章存档

2012年(1)

2011年(2)

2010年(10)

2009年(21)

2008年(18)

2007年(12)

2006年(21)

2005年(31)

我的朋友

分类: Java

2010-01-23 22:33:23

最近开始学习为手机开发软件了。
今天上网搜索了一下相关资料,然后就开始写程序了。
值得一提的是,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(count==0)
        {
            g.drawString("counter="+count,0,80,Graphics.TOP|Graphics.LEFT);
            
        }
        else
        {
            g.setColor(255,255,255);
            g.drawString("counter="+(count - 1) ,0,80,Graphics.TOP|Graphics.LEFT);
            g.setColor(0,122,122);
            g.drawString("counter="+count,0,80,Graphics.TOP|Graphics.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,看来有得玩啦。
阅读(873) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~