ホームに戻る
 スクラッチパッドの使用


/*
*   スクラッチパッドの使用
*/

import java.io.*;
import javax.microedition.io.*;
import com.nttdocomo.ui.*;

public class scratchpad extends IApplication
{
    nmCanvas nmc;

    public void start() {
        nmc = new nmCanvas();
        Display.setCurrent(nmc);
    }

    public class nmCanvas extends Canvas
    {
        int i = 0;

        String s1 = "abc";
        String s2 = "xyz";

        int font_width;
        int font_height;

        int screen_width;
        int screen_height;

        int position_x;
        int position_y;

        public nmCanvas()
        {
            // ソフトキーの左側を「変更」にする
            setSoftLabel(Frame.SOFT_KEY_1, "変更");
            // ソフトキーの右側を「終了」にする
            setSoftLabel(Frame.SOFT_KEY_2, "終了");

            // スクリーンの幅と高さを取得
            screen_width = getWidth();
            screen_height = getHeight();
            
            // デフォルトのフォントを取得
            Font font = Font.getDefaultFont();
            
            // 文字列のフォント情報を取得
            font_width = font.stringWidth(s1);
            font_height = font.getHeight();

            // 文字列を中央に表示するための位置を算出
            position_x = (screen_width - font_width) / 2;
            position_y = (screen_height + font_height) / 2;

            // 位置 0 から4バイトぶんのint値を読みこむ
            try{
                DataInputStream in = Connector.openDataInputStream("scratchpad:///0;pos=0");
                try{
                    i = in.readInt();
                }
                catch(Exception e){}
                in.close();
            }
            catch(Exception e){}
        }

        public void paint(Graphics g)
        {
            // 描画を隠す
            g.lock();
            // 画面のクリア
            g.clearRect(0, 0, screen_width, screen_height);
            // 文字列の描画
            if(i == 0){
                g.drawString(s2, position_x, position_y);
            }
            else{
                g.drawString(s1, position_x, position_y);
            }
            // 描画されたものを表示
            g.unlock(true);
        }

        public void processEvent(int nType, int nParm)
        {
            if(nType == Display.KEY_PRESSED_EVENT) {
                switch(nParm) {
                    case Display.KEY_SOFT1:
			if(i == 0){
                            i = 1;
                        }
                        else if(i == 1){
                            i = 0;
                        }
                        break;
                    case Display.KEY_SOFT2:
                        // 位置 0 から4バイトぶんのint値を書き込む
                        try{
                            DataOutputStream out = Connector.openDataOutputStream("scratchpad:///0;pos=0");
                            out.writeInt(i);
                            out.close();
                        }
                        catch(Exception e){}
			// 終了
                        IApplication.getCurrentApp().terminate();
                        break;
                }
            }
            this.repaint();
        }
    }
}


スクラッチパッドとはデータを記録できる領域のことのようです
データはファイル名で管理されず バイト単位での領域の位置で管理します
例では pos=0 としているので 位置 0 から読み書きが行われます
スクラッチパッドのサイズはJAMファイルの SPsize で設定する必要があります
この例では最低4バイト必要

動作はソフトキー1で abc と xyz の表示を切り替え
ソフトキー2で終了します
再起動時に終了時の abc か xyz かの状態を再現します

内部では読みこみ時にint値を読みだし
ソフトキー2での終了でint値を書き出しています

inserted by FC2 system