⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gobangmidlet.java

📁 J2ME程序设计实例教程的源码
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.bluetooth.*;
import javax.microedition.io.*;
import java.io.*;

/**
 * 该类是应用程序的主类,实现了MIDlet的生命周期方法。
 * 同时,该类还充当了控制器的功能。
 */
public class GobangMIDlet extends MIDlet implements CommandListener {
    private Display display;
    private ModeForm modeForm;      //启动模式选择界面
    private Chessboard chessboard;  //棋盘
    private BTServer btServer;      //蓝牙服务器
    private BTClient btClient;      //蓝牙客户端
    private BTConnection btconn;    //蓝牙连接
    
    private Command cmdStartup = new Command("启动", Command.SCREEN, 1);
    private Command cmdExit = new Command("退出", Command.EXIT, 1);
    
    public GobangMIDlet() {
        display = Display.getDisplay(this);
        modeForm = new ModeForm("五子连珠", this);
    }
    
    public void startApp() {
        modeForm.addCommand(cmdStartup);
        modeForm.addCommand(cmdExit);
        display.setCurrent(modeForm);
    }
    
    public void pauseApp() {
        release();
    }
    
    public void destroyApp(boolean forced) {
        release();
    }
    
    public void commandAction(Command cmd, Displayable d) {
        if(cmd == cmdExit) {
            destroyApp(true);
            notifyDestroyed();
        }
        else if(cmd == cmdStartup) {
            switch(modeForm.getMode()) {
                case ModeForm.SERVER: {
                    chessboard = new Chessboard(this, "五子连珠——服务器模式", 
                                                    ChessPoint.WHITE_CHESSMAN);
                    chessboard.addCommand(cmdExit);
                    display.setCurrent(chessboard);
                    chessboard.setInformation("等待客户端连接......");
                    
                    //启动服务等待客户端连接
                    btServer= new BTServer(this);
                    btServer.startService();
                    break;
                }
                case ModeForm.CLIENT: {
                    chessboard = new Chessboard(this, "五子连珠——客户端模式", 
                                                    ChessPoint.BLACK_CHESSMAN);
                    chessboard.addCommand(cmdExit);
                    display.setCurrent(chessboard);
                    chessboard.setInformation("搜索服务器,建立连接......");
                    
                    try {//创建客户端,搜索服务
                        btClient = new BTClient(this);
                        btClient.startDiscover();
                    }
                    catch(BluetoothStateException bse) {
                        //System.out.println("client---error: " + bse);
                    }
                    break;
                }
            } 
        }
    }
    
    public void release() {
        if(btServer != null) {
            btServer.close();
        }
        if(btClient != null) {
            btClient.stopDiscover();
        }
        if(btconn != null) {
            btconn.close();
        }
        if(chessboard != null) {
            chessboard.endPlay();
            chessboard.setInformation("对方已经断开!");
        }
    }
    
    //开始对弈
    public void startPlay(BTConnection btconn) {
        this.btconn = btconn;
        chessboard.startPlay();
    }
    
    //通知对方走子
    public void notifyOther(int x, int y) {
        btconn.send(x, y);
    }
    
    //对方走的子
    public void otherPlay(int x, int y) {
        chessboard.otherPlayChessman(x, y);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -