📄 midletgamehall.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package view;import control.CardClient;import control.ChatClient;import control.FIRClient;import control.GameService;import java.io.IOException;import java.util.Date;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import model.INFO;import org.netbeans.microedition.lcdui.SimpleTableModel;import org.netbeans.microedition.lcdui.TableItem;import org.netbeans.microedition.util.SimpleCancellableTask;/** * @author XJX */public class MidletGameHall extends MIDlet implements CommandListener, ItemCommandListener,Runnable{private int[]gamesInfo; private boolean midletPaused = false; private int[] roomPopulation; private String userAccount; private int gameIndex; private int roomIndex; private Thread refreshGameThread=null; private Thread refreshRoomThread=null; private boolean isRefreshGame=false; private boolean isRefreshRoom=false; public int midletCmd; public static final int LeaveRoom = 1; public static final int Login = 2; public static final int SetReady = 3; public static final int Register = 4; public static final int EnterGame=5; public static final int EnterRoom=6; public static final int RefreshChatTable=7; public static final int ExitToRoomSelecting=8; public static final int ExitToGameSelecting=9; public static final int Logout=10; public int getGameIndex() { return gameIndex; } public int getRoomIndex() { return roomIndex; } private ChatClient chatProxy; private Command sendCommand = new Command("Send", Command.OK, 1); //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0| private Form formLogin; private ImageItem imageItem; private TextField textFieldUsername; private ImageItem imageItem1; private TextField textFieldPassword; private ImageItem imageItem2; private Form formRegister; private TextField textFieldEmail; private TextField textFieldQQ; private TextField textFieldNewUsername; private TextField textFieldNewPassword; private TextField textFieldNickname; private Alert alertRegisterFail; private Alert alertRegisterOK; private Alert alertWrongPassword; private List listGameSelect; private Alert alertRoomFull; private List listRoomSelect; private Form formChat; private TableItem tableItemPlayers; private StringItem stringItemChat; private TextField textFieldInput; private Command okCommand; private Command exitCommand; private Command itemCommandLogin; private Command itemCommandRegister; private Command cancelCommand; private Command okCommand1; private Command okCommandSend1; private Command okCommandReady; private Command okCommandSend; private Command exitCommandLeaveChat; private Command exitCommandLeaveGameSelect; private Command exitCommandLeaveRoomSelect; private Image image1; private Image image2; private Image image3; private Image image4; private SimpleTableModel tableModel; private SimpleCancellableTask task; //</editor-fold>//GEN-END:|fields|0|private GameService gs=null; /** * The MidletGameHall constructor. */ public MidletGameHall() { if(gs==null){ gs=new GameService(); } } public void beginGame(int gameIndex){ try {//GameService gs;int port; switch (gameIndex) { case 0: //FIR FIRClient firclient = null; //GameService gameEntity = new GameService(); //port = gameEntity.getJarPort(0, roomIndex); port = gs.getJarPort(0, roomIndex); gs.removeJarPort(3, gameIndex * 5 + roomIndex); //gs=null; chatProxy=null; System.gc(); firclient = new FIRClient(INFO.PlatformIP, port, INFO.userAccount, this); firclient.show(); break; case 1: //Card CardClient cardclient=null; //gs = new GameService(); port = gs.getJarPort(1, roomIndex); gs.removeJarPort(3, gameIndex * 5 + roomIndex); //gs=null; chatProxy=null; System.gc(); cardclient=new CardClient(this,roomIndex,port); cardclient.show(); break; } } catch (IOException ex) { ex.printStackTrace(); }} public void refreshChatTable() { midletCmd=MidletGameHall.RefreshChatTable; Thread t=new Thread(this); t.start(); } private void autoRefreshGameInfo(final String username) { isRefreshGame=true; refreshGameThread = new Thread() { public synchronized void run() { while (isRefreshGame==true) { try { Thread.sleep(300); } catch (InterruptedException ex) { //ex.printStackTrace(); } if (isRefreshGame==false) return; try { gamesInfo = gs.enterPlatformAndGetGamesInfo(username); } catch (IOException ex) { ex.printStackTrace(); } int selectIndex=getListGameSelect().getSelectedIndex(); if (isRefreshGame==false) return; listGameSelect=null; switchDisplayable(null, getListGameSelect()); getListGameSelect().setSelectedIndex(selectIndex, true); } } }; refreshGameThread.start(); } private void autoRefreshRoomInfo() { isRefreshRoom=true; refreshRoomThread=new Thread(){ public synchronized void run(){ while (isRefreshRoom==true) { try { Thread.sleep(300); } catch (InterruptedException ex) { //ex.printStackTrace(); } try { if (isRefreshRoom == false) { return; } roomPopulation = gs.enterGameAndGetRoomsInfo(userAccount, gameIndex); } catch (IOException ex) { ex.printStackTrace(); } int selectIndex = getListRoomSelect().getSelectedIndex(); if (isRefreshRoom == false) { return; } listRoomSelect = null; String roomName = "房间"; for (int i = 0; i < roomPopulation.length; i++) { int j = i + 1; getListRoomSelect().append(roomName + j + " (" + roomPopulation[i] + "人)", null); } if (isRefreshRoom == false) { return; } switchDisplayable(null, getListRoomSelect()); getListRoomSelect().setSelectedIndex(selectIndex, true); } } }; refreshRoomThread.start(); } //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0| //</editor-fold>//GEN-END:|methods|0| //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize /** * Initilizes the application. * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method. */ private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize // write pre-initialize user code here//GEN-LINE:|0-initialize|1|0-postInitialize // write post-initialize user code here }//GEN-BEGIN:|0-initialize|2| //</editor-fold>//GEN-END:|0-initialize|2| //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction /** * Performs an action assigned to the Mobile Device - MIDlet Started point. */ public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction // write pre-action user code here switchDisplayable(null, getFormLogin());//GEN-LINE:|3-startMIDlet|1|3-postAction Display.getDisplay(this).setCurrentItem(textFieldUsername); // write post-action user code here }//GEN-BEGIN:|3-startMIDlet|2| //</editor-fold>//GEN-END:|3-startMIDlet|2| //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction /** * Performs an action assigned to the Mobile Device - MIDlet Resumed point. */ public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction // write pre-action user code here//GEN-LINE:|4-resumeMIDlet|1|4-postAction // write post-action user code here }//GEN-BEGIN:|4-resumeMIDlet|2| //</editor-fold>//GEN-END:|4-resumeMIDlet|2| //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch /** * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable. * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately * @param nextDisplayable the Displayable to be set */ public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch // write pre-switch user code here Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch if (alert == null) { display.setCurrent(nextDisplayable); } else { display.setCurrent(alert, nextDisplayable); }//GEN-END:|5-switchDisplayable|1|5-postSwitch // write post-switch user code here
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -