J2ME 拼图游戏 快速开发 全过程 (四)——游戏选项设置界面

代码:

package cn.edu.xtu.tilepuzzle.ui;


import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;


import cn.edu.xtu.tilepuzzle.GameDB;
import cn.edu.xtu.tilepuzzle.model.BoardModel;




public class GameSetUI extends Form implements CommandListener {
public boolean reversed;
    public boolean funny;
    public boolean hard;
    public boolean addString;
    public boolean rowColumnIs_4x3=false;
    Command ok;
    Command cancel;
    public Display dpy;
    public Displayable prev;
    ChoiceGroup baseSet;
    ChoiceGroup hardSet;
    ChoiceGroup rowColumnSet;
    
    BoardModel boardModel;


    public GameSetUI(BoardModel boardModel,Display dpy_, Displayable prev_) {
        super("游戏设置");
        this.boardModel=boardModel;
        dpy = dpy_;
        prev = prev_;       
        
        initSetData();


        append("基本设置:");
        baseSet = new ChoiceGroup(null, Choice.MULTIPLE);
        baseSet.append("反向", null);
        baseSet.append("趣味洗牌", null);
        baseSet.append("标记方格", null);
        append(baseSet);


        // use a label here
        append("难易程度:");
        hardSet = new ChoiceGroup(null, Choice.EXCLUSIVE);
        hardSet.append("简单", null);
        hardSet.append("困难", null);
        append(hardSet);
        
        append("行、列数 设置:");
        rowColumnSet = new ChoiceGroup(null, Choice.EXCLUSIVE);
        rowColumnSet.append("4 行,3 列", null);
        rowColumnSet.append("5 行,4 列", null);
        append(rowColumnSet);


        loadUI();
        
        cancel = new Command("取消", Command.CANCEL, 0);
        ok = new Command("确定", Command.OK, 1);
        addCommand(ok);
        addCommand(cancel);
        setCommandListener(this);
    }
    public void initSetData(){
    /*
    * gameSetData[0]:orgImageString 图片路径默认   DataBase.orgImageString
    * gameSetData[1]:反向boolean true 反向        false reversed
    * gameSetData[2]:趣味洗牌boolean true 是 falsefunny
    * gameSetData[3]:标记方格 boolean true 标记 trueaddString
    * gameSetData[4]:困难/简单boolean true 困难 truehard
    * gameSetData[5]:行大小 int rows 4 rows
    * gameSetData[6]:列大小 int columns 5columns
    * gameSetData[7]:行/列String 3x4rowColumn
    * */
    // set up default values
    if(boardModel.gameSetData[GameDB.IndexInGameSetDatat_reversed].equals("false")){
    reversed = false;
    }else {
    reversed = true;
}
    if(boardModel.gameSetData[GameDB.IndexInGameSetDatat_funny].equals("false")){
    funny = false;
    }else {
    funny = true;
}
    if(boardModel.gameSetData[GameDB.IndexInGameSetDatat_addString].equals("false")){
    addString = false;
    }else {
    addString = true;
}
    if(boardModel.gameSetData[GameDB.IndexInGameSetDatat_hard].equals("false")){
hard = false;
    }else {
    hard = true;
}
    if(boardModel.gameSetData[GameDB.IndexInGameSetDatat_columns].equals("4")){
    rowColumnIs_4x3 = true;
    }else {
    rowColumnIs_4x3 = false;
}
    }


    public void commandAction(Command c, Displayable d) {
        if (c == ok) {
            readUI();            
            boardModel.updateGameData(GameSetUI.this);
            System.out.println("点击了确定按键");
        } else if (c == cancel) {
            loadUI();
            System.out.println("点击了返回按键");
        }        
        dpy.setCurrent(prev);
        System.out.println("返回到跳转前页面");
    }


    void loadUI() {
baseSet.setSelectedIndex(0, reversed);
baseSet.setSelectedIndex(1, funny);
baseSet.setSelectedIndex(2, addString);
hardSet.setSelectedIndex((hard ? 1 : 0), true);
rowColumnSet.setSelectedIndex(rowColumnIs_4x3 ? 0 : 1, true);
}


    void readUI() {
        reversed = baseSet.isSelected(0);
        funny = baseSet.isSelected(1);
        addString=baseSet.isSelected(2);
        hard = hardSet.isSelected(1);        
        rowColumnIs_4x3=rowColumnSet.isSelected(0);
    }
}

运行时显示如图:


你可能感兴趣的:(游戏,String,command,null,Class,j2me)