▼MIDP2.0メモ▼
カスタムアイテムを作る


カスタムアイテムを作るプログラム。



プログラム
CustomItemEx.java
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

//カスタムアイテムを作る(本体)
public class CustomItemEx extends MIDlet {
    static MIDlet current;

    //コンストラクタ
    public CustomItemEx() {
        current=this;
        Display.getDisplay(this).setCurrent(new CustomItemForm());
    }

    //アプリの開始
    public void startApp() {
    }

    //アプリの一時停止
    public void pauseApp() {
    }

    //アプリの終了
    public void destroyApp(boolean flag) {
    }
}


CustomItemForm.java
import javax.microedition.lcdui.*;

//カスタムアイテムを作る(フォーム)
class CustomItemForm extends Form {

    //コンストラクタ
    public CustomItemForm(){
        super("CustomItemEx");

        //文字列アイテム
        append("テキスト0");

        //カスタムアイテム
        append(new SimpleCustomItem("CustomItem"));

        //文字列アイテム
        append("テキスト1");
    }
}


SimpleCustomItem.java
import javax.microedition.lcdui.*;

//カスタムアイテムを作る(カスタムアイテム)
class SimpleCustomItem extends CustomItem {
    int     count;//カウント
    boolean focus;//フォーカス

    //コンストラクタ
    public SimpleCustomItem(String label) {
        super(label);
    }

    //最小幅の取得
    public int getMinContentWidth() {
        return 140;
    }

    //最小高さの取得
    public int getMinContentHeight() {
        return 140;
    }

    //幅の取得
    public int getPrefContentWidth(int width) {
        return 140;
    }

    //高さの取得
    public int getPrefContentHeight(int height) {
        return 140;
    }

    //描画
    public void paint(Graphics g,int w,int h) {
        if (focus) {
            g.setColor(0,0,255);
        } else {
            g.setColor(0,0,0);
        }
        g.drawRect(0,0,w-1,h-1);
        g.drawString(""+count,w/2,h/2,g.BASELINE|g.HCENTER);
    }

    //キープレスイベント
    protected void keyPressed(int keyCode) {
        if (getGameAction(keyCode)==Canvas.FIRE) {
            count++;
            repaint();
        }
    }

    //フォーカスに入る
    protected boolean traverse(int dir,
        int viewportWidth,int viewportHeight,int[] visRect_inout) {
        focus=true;
        return false;
    }

    //フォーカスから外れる
    protected void traverseOut() {
        focus=false;
    }
}



−戻る−