📄 client.java
字号:
import javax.microedition.midlet.*;import javax.microedition.io.*;import javax.microedition.lcdui.*;import java.io.*;public class Client implements Runnable,CommandListener{ /*定义舞台*/ Display display; /*定义父类,以供传递控制*/ GameMIDlet parent; /*端口连接控制*/ SocketConnection sc; /*定义输入流,以接收服务器端传来的数据*/ InputStream is; /*定义输出流,以向服务器端发送数据*/ OutputStream os; /*定义游戏控制类,以便实现游戏的初始化和控制*/ MyGameWithTiledLayerCanvas t; /*标记传输是否结束*/ boolean stop; /*定义画板*/ Graphics h; /*定义个Alert以显示版本等信息*/ Alert al; Command exitCommand=new Command("Exit", Command.EXIT, 1); Command exitCommand1=new Command("结束", Command.EXIT, 1); public Client(GameMIDlet m) { parent=m; /*得到父类舞台*/ display = Display.getDisplay(parent); /*对Alert进行设置*/ al = new Alert("Client Server"); al.setType(AlertType.INFO); al.setTimeout(Alert.FOREVER); al.setString("Version:1.0 " +"Auther:You GanQuan " +"Date:2004/05/29 " +"Address:SWUST "); al.addCommand(exitCommand); al.setCommandListener(this); display.setCurrent(al); } public void start() { /*线程*/ Thread t = new Thread(this); t.start(); } public void run() { try{ /*控制与服务器端建立连接*/ sc = (SocketConnection) Connector.open("socket://localhost:8000");/*打开输入流*/ is = sc.openInputStream(); /*打开输出流*/ os = sc.openOutputStream(); /*以传递值开始游戏*/ t=new MyGameWithTiledLayerCanvas(2,os); t.addCommand(exitCommand1); t.setCommandListener(this); display.setCurrent(t); t.start(); /*不停地对数据进行接收并解析之,直到游戏结束*/ while(true) { /*将字节流转化为数据流*/ DataInputStream dis = new DataInputStream(is) ; String sb=""; int c=0; /*对数据的一次读入*/ while (((c = is.read()) != '\n') && (c != -1)) { sb=sb+(char)c; } if (c == -1) { System.out.println("client 49"); break; } /*数据解析还原*/ String sb1="",sb2="",sb3="",sb4=""; int size=sb.indexOf(":"); int size1=sb.indexOf(";"); int size2=sb.indexOf(","); for(int i=0;i<size;i++) sb1=sb1+sb.charAt(i); for(int i=size+1;i<size1;i++) sb2=sb2+sb.charAt(i); for(int i=size1+1;i<size2;i++) sb3=sb3+sb.charAt(i); for(int i=size2+1;i<sb.length();i++) sb4=sb4+sb.charAt(i); Integer x1 = Integer.valueOf(sb1); int x2 = x1.intValue(); x1=Integer.valueOf(sb2); int y2=x1.intValue(); x1 = Integer.valueOf(sb3); int z2 = x1.intValue(); x1 = Integer.valueOf(sb4); int w2 = x1.intValue(); /*设置游戏控制部分的某些值以实现画面的互动*/ t.ex=x2; t.ey=y2; t.state1=w2; /*如果服务器端的某个位置安了泡泡,服务器端也要安*/ if(z2!=-1) {Bomb bomb=new Bomb(z2,t); bomb.start(); } } /*当游戏结束,对相应资源的回收*/ t.exit(); stop(); parent.notifyDestroyed(); } catch (IOException ioe) { if (!stop) { ioe.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } } /*对一些按纽事件的处理*/ public void commandAction(Command c, Displayable s) { String cmd =c.getLabel(); if(cmd.equals("Exit")){ parent.notifyDestroyed(); parent.destroyApp(true); } else if(cmd.equals("结束")){ t.exit(); stop(); parent.notifyDestroyed(); } } /*资源的回收*/ public void stop() { try { stop = true; if (is != null) { is.close(); } if (os != null) { os.close(); } if (sc != null) { sc.close(); } } catch (IOException ioe) {} }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -