/** Stop talking to the server */ public void stop() { if (socket != null) { // Close all the streams and socket if (out != null) { try { out.close(); } catch (IOException ioe) { } out = null; } if (in != null) { try { in.close(); } catch (IOException ioe) { } in = null; } if (socket != null) { try { socket.close(); } catch (IOException ioe) { } socket = null; } } else { // Already stopped } // Make sure the right buttons are enabled start_button.setEnabled(true); stop_button.setEnabled(false); setStatus(STATUS_STOPPED); }
/** Start talking to the server */ public void start() { String codehost = getCodeBase().getHost(); if (socket == null) { try { // Open the socket to the server socket = new Socket(codehost, port); // Create output stream out = new ObjectOutputStream(socket.getOutputStream()); out.flush(); // Create input stream and start background // thread to read data from the server in = new ObjectInputStream(socket.getInputStream()); new Thread(this).start(); } catch (Exception e) { // Exceptions here are unexpected, but we can't // really do anything (so just write it to stdout // in case someone cares and then ignore it) System.out.println("Exception! " + e.toString()); e.printStackTrace(); setErrorStatus(STATUS_NOCONNECT); socket = null; } } else { // Already started } if (socket != null) { // Make sure the right buttons are enabled start_button.setEnabled(false); stop_button.setEnabled(true); setStatus(STATUS_ACTIVE); } }
public void mouseClicked(MouseEvent e) { /*can = false; boolean f = true; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) if (cell[i][j] == e.getSource()) { int judege = Clicked(cell[i][j]); f = false; break; } if (!f) break; }*/ boolean flage = CheckAll(); if (flage) { can = false; if (kind.equals("" + turn)) { ChessBoard cel = (ChessBoard) (e.getSource()); int judge = Clicked(cel); if (judge == 1) { try { System.out.println("发送前:" + cell[3][5].taken); out66.writeObject("落子" + turn); out66.flush(); out66.writeObject(stateList.get(stateList.size() - 1)); out66.flush(); out66.writeObject(takenList.get(takenList.size() - 1)); out66.flush(); } catch (IOException e1) { e1.printStackTrace(); } } } else { JOptionPane.showMessageDialog(null, "请确定您的身份,您此时不能落子"); } } else CheckAtTheEnd(); }
@Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("下棋")) { audience.setEnabled(false); fighter.setEnabled(false); begin.setEnabled(true); // JOptionPane.showMessageDialog(null, "下棋"); System.out.println("下棋"); try { System.out.println("客户端发送下棋指令"); out66.writeObject("对手"); out66.flush(); out66.writeObject(new char[0][0]); out66.flush(); out66.writeObject(new boolean[0][0]); out66.flush(); } catch (IOException e1) { e1.printStackTrace(); } } if (e.getActionCommand().equals("观看")) { submit.setEnabled(false); regret.setEnabled(false); audience.setEnabled(false); fighter.setEnabled(false); // JOptionPane.showMessageDialog(null, "观看"); System.out.println("观看"); try { out66.writeObject("观众"); out66.flush(); out66.writeObject(stateList.get(stateList.size() - 1)); out66.flush(); out66.writeObject(takenList.get(takenList.size() - 1)); out66.flush(); } catch (IOException e1) { e1.printStackTrace(); } } /*if (e.getActionCommand().equals("人机对弈")) { audience.setEnabled(false); fighter.setEnabled(false); AIPlayer.setEnabled(false); begin.setEnabled(true); JOptionPane.showMessageDialog(null, "人机对弈"); }*/ if (e.getActionCommand().equals("发送")) { // JOptionPane.showMessageDialog(null, "发送"); System.out.println("发送"); String str = myRole + ": " + " " + jt1.getText() + "\n"; try { out99.writeObject(" " + str); out99.flush(); out99.writeObject(new char[8][8]); out99.flush(); out99.writeObject(new boolean[8][8]); out99.flush(); } catch (IOException e1) { e1.printStackTrace(); } jt2.append(str); jt1.setText(""); } if (e.getActionCommand().equals("取消")) { // JOptionPane.showMessageDialog(null, "取消"); System.out.println("取消"); jt1.setText(""); } if (e.getActionCommand().equals("悔棋")) { // JOptionPane.showMessageDialog(null, "悔棋"); System.out.println("悔棋"); try { out66.writeObject("请求悔棋"); out66.flush(); out66.writeObject(new char[8][8]); out66.flush(); out66.writeObject(new boolean[8][8]); out66.flush(); } catch (IOException e1) { e1.printStackTrace(); } // RegretChess(); // ShowChessNumber(); } if (e.getActionCommand().equals("退出")) { int quit = JOptionPane.showConfirmDialog(null, "您确定要强制退出吗?", "请确认您的选择", JOptionPane.YES_NO_OPTION); if (quit == JOptionPane.YES_OPTION) { JOptionPane.showMessageDialog(null, "已强制退出"); System.exit(0); } else return; } if (e.getActionCommand().equals("开始")) { begin.setEnabled(false); System.out.println("客户端发送开始指令"); // JOptionPane.showMessageDialog(null, "开始"); try { out66.writeObject("请求开始"); out66.flush(); out66.writeObject(stateList.get(stateList.size() - 1)); out66.flush(); out66.writeObject(takenList.get(takenList.size() - 1)); out66.flush(); } catch (IOException e1) { e1.printStackTrace(); } Begin(); if (kind == "黑") { for (int i = 0; i < 8; i++) for (int j = 0; j < 8; j++) if (cell[i][j].taken == false) { CheckPlace(cell[i][j]); if (canPut) { cell[i][j].ChangeBackground(); canPut = false; } } } } if (e.getActionCommand().equals("存盘")) { // JOptionPane.showMessageDialog(null, "存盘"); System.out.println("存盘"); try { System.out.println(); out.writeObject(stateList); out.flush(); out.close(); } catch (IOException e1) { e1.printStackTrace(); } } }