📄 gameui.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package org.challenge.chengshi.game;import java.util.Hashtable;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.CommandListener;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Graphics;import org.challenge.chengshi.MainMidlet;import org.challenge.chengshi.game.client.ClientUI;import org.challenge.chengshi.game.client.GameClient;import org.challenge.chengshi.game.server.GameServer;import org.challenge.chengshi.record.Record;/** * * @author challenge */public class GameUI extends Canvas implements CommandListener { private Game game; public MainMidlet mid; public boolean isServer; public GameServer server; public GameClient client; private ClientUI clientUI; private Record record=new Record(); private Command exitCommand=new Command("退出",Command.EXIT,1); private static boolean isFirst=true; /** * 构造函数传入MainMidlet isServer(是否是服务端) * @param mid * @param isServer */ public GameUI(MainMidlet mid,boolean isServer){ this.mid=mid; this.isServer=isServer; if(isServer==true){ //是服务器时建立服务端并初始化 this.server=new GameServer(this); this.init(); }else{//客房端时建立客房端,并显示客房端界面即服务列表 this.client=new GameClient(this); this.clientUI=new ClientUI(this); // this.init(); } try{ this.game=new Game(this);// s=game.toString(); // this.mid.setDisplayable(this); }catch(Exception e){ // s="3"; this.mid.show(); } // s="3";// isFirst=false; // this.show(); // this.repaint(); } //初始化 public void init(){ this.addCommand(exitCommand); this.setCommandListener(this); this.setFullScreenMode(true); this.show(); } //显示本游戏的画面 public void show(){ Display.getDisplay(this.mid).setCurrent(this); } //画游戏画面 protected void paint(Graphics g) { game.paintGame(g); } //对事件的响应 public void commandAction(Command cmd, Displayable arg1) { if(cmd==this.exitCommand){ this.mid.destroyApp(true); this.mid.notifyDestroyed(); } // throw new UnsupportedOperationException("Not supported yet."); } //对键盘事件的响应 protected void keyPressed(int keyCode){ int action=this.getGameAction(keyCode); game.keyPressed(action); } //退出 public void destroy(){ if(this.server!=null){// this.server.destroy(); } if(this.client!=null){// this.client.destroy(); } } //从对方接收信息 public void receiveMessage(String message){ this.game.receieveMessage(message); } //发送信息 public void sendMessage(String message){ if(this.isServer){ //判断 this.server.sendMessage(message); }else{ this.client.sendMessage(message); } } //客户端选择要连接到哪个服务端,连接时调用的方法 public void connect(String name){ this.client.connect(name); } //客户端显示服务列表时要调用的方法 public java.util.Enumeration getRemoteName(){ return this.client.getRemoteName(); } //保存记录 public void save(int step){ this.record.save(step); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -