1. 程式人生 > >自己做的J2ME閱讀器原始碼!

自己做的J2ME閱讀器原始碼!

    挺長時間沒有更新Blog,因為內容不多,所以總結起來一起更新。

    這個東西做了十多天,別人可能也就1天左右,為什麼我要那麼長?可能是新手的原因吧,而且這段時間課程也比較緊。廢話不說。程式碼上來:

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;

public class newCanvas extends Canvas implements CommandListener{

    private String s; //把讀出來的位元組碼形成UTF-8編碼方式
   
    private final static int scrHight = 208;   //固定的螢幕高度。

    private Font f; //當前系統預設字型資訊

    private char[] c; //通過s得到的char陣列

    private Vector view; //最後形成,存放文字的地方

    private String[] strView; //顯示用陣列

    private int start;    //顯示的開始行
 
    private int end;      //終止行

    private int scrLines;  //螢幕可以列印多少行
   
    public static MIDlet mid;    //主視窗
    //成員變數初始化
    public newCanvas() {
        setFullScreenMode(true);
        s = null;
        f = Font.getDefaultFont();
        c = null;
        view = new Vector();
        try {
            s = getText(); //測試通過,已經得到檔案內容。
        } catch (IOException e) {

            System.out.println("讀檔案失敗!"); //這裡新增錯誤處理
        }
        c = s.toCharArray();
        cut(view, c);
        strView = new String[view.size()];
        scrLines = scrHight / f.getHeight();   //scrHight
        start = 0;   
        end = scrLines;
        //按行存放到陣列中,方便以後顯示。
        Enumeration e = view.elements();          
        for (int i = 0; i < strView.length; i++) {
            strView[i] = (String) e.nextElement();
        }
        //垃圾回收
        s = null;
        System.gc();
        //新增退出命令
        this.addCommand(new Command("退出", Command.EXIT, 1));
        setCommandListener(this);
    }

    public void paint(Graphics g) {
        clearScreen(g);
        int x = 0;
        int y = -f.getHeight();
        for (int i = start; i <= end - 1; i++) {
            y += f.getHeight();
            g.drawString(strView[i], x, y, f.SIZE_SMALL);
        }
    }

    private void clearScreen(Graphics g) {
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, getWidth(), scrHight);   //scrHight
        g.setColor(0, 0, 0);
    }

    public String getText() throws IOException {
        InputStream is = getClass().getResourceAsStream("/1.txt");
        is.skip(3);    //跳過UTF-8的標記符號
        if (null != is) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int ch = 0;
            while ((ch = is.read()) != -1) {
                baos.write(ch);
            }
            byte[] text = baos.toByteArray();
            baos.close();
            return new String(text, "UTF-8");

        } else {
            return null;
        }
    }

    public void cut(Vector v, char[] c) {

        StringBuffer sb = new StringBuffer(); //臨時存放一行內容的容器
        int tmp = 0; //記錄截斷位
        int num = 0; // 行內數字計數器
        while (tmp < c.length - 1) {
            for (int i = tmp; i < c.length; i++) {
                if (c[i] == '/n') {
                    num = 0;
                    tmp++;    //遇到空格跳過去
                    break;
                }
                num += f.charWidth(c[i]); //記錄開始
                if (num >= getWidth()) {
                    num = 0;
                    tmp++;    //重複字元跳過去。
                    break;
                }
                sb.append(c[i]); // 新增字元
                tmp = i;
            }
            v.addElement(sb.toString());
            sb.delete(0, sb.length());

        }
        sb = null; //清理垃圾 

    }

    protected void keyPressed(int keycode) {
        switch (getGameAction(keycode)) {
            case Canvas.UP:

                if (start == 0) {
                    break;
                }
                start = start - 1;
                end = end - 1;
                repaint();
                break;
            case Canvas.DOWN:

                if (end == strView.length) {
                    break;
                }
                start = start + 1;
                end = end + 1;
                repaint();
                break;
            case Canvas.LEFT:

                if ((start - (scrLines - 1)) < 0) {
                    start = 0;
                    end = scrLines;
                    repaint();
                    break;
                }
                start = start - (scrLines - 1);
                end = end - (scrLines - 1);
                repaint();
                break;
            case Canvas.RIGHT:           //對右鍵的處理,是向下翻一頁。
                if ((end + (scrLines - 1)) >= strView.length) {
                    if (end == strView.length) {
                        break;
                    }
                    start = end;
                    end = strView.length;
                    repaint();
                    break;
                }
                start = start + (scrLines - 1);
                end = end + (scrLines - 1);
                repaint();
                break;
        }
    }

    public void commandAction(Command c, Displayable d) {
        if (c.getLabel().equals("退出")) {
            mid.notifyDestroyed();
        }
       
    }
}
 

要讀的檔案放到res目錄裡,必須是UTF-8格式的。

這個程式只是能跑起來,速度不是很快,需要把演算法在優化一下,這幾天就弄,然後再加點功能。