Server/Cientによるネット五目並べ



機能:
1,サーバーとクライアントを提供します。
2,サーバはクライアントの登録状況を監視し、複数のクライアントにログインを許可する。
3,ユーザーはクライアントを通じてサーバーにログインできます。その後、サーバーの現在オンラインの他のユーザーを見て、お互いに交流します。
4,サーバーに登録したら、新しいゲームを作成したり、作成したゲームを追加したりできます。
5、対局
コードの下に具体的なファイルがあります。
コード:
/*
 * 
 */
package djr.chess.gui;

import java.awt.BorderLayout;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.TextArea;

import javax.swing.JPanel;
import javax.swing.JTextArea;

//      
public class UserChatPad extends JPanel
{
	public JTextArea chatTextArea = new JTextArea("    ", 18, 20);
	public UserChatPad()
	{
		setLayout(new BorderLayout());
		chatTextArea.setAutoscrolls(true);
		chatTextArea.setLineWrap(true);
		add(chatTextArea, BorderLayout.CENTER);
	}
}


 /*
 *       ,          ,       

 */
package djr.chess.gui;

import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class UserControlPad extends JPanel
{
	public JLabel ipLabel = new JLabel("IP", JLabel.LEFT);
	public JTextField ipInputted = new JTextField("localhost", 10);
	public JButton connectButton = new JButton("      ");
	public JButton createButton = new JButton("    ");
	public JButton joinButton = new JButton("    ");
	public JButton cancelButton = new JButton("    ");
	public JButton exitButton = new JButton("    ");
	
	public UserControlPad()
	{
		setLayout(new FlowLayout(FlowLayout.LEFT));
		setBackground(Color.LIGHT_GRAY);
		add(ipLabel);
		add(ipInputted);
		add(connectButton);
		add(createButton);
		add(joinButton);
		add(cancelButton);
		add(exitButton);
	}
}


 /*
 *
 */

package djr.chess.gui;

import java.awt.Choice;
import java.awt.FlowLayout;
import java.awt.Panel;
import java.awt.TextField;

import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;

//     
public class UserInputPad extends JPanel
{
	public JTextField contentInputted = new JTextField("", 26);
	public JComboBox userChoice = new JComboBox();

	public UserInputPad()
	{
		setLayout(new FlowLayout(FlowLayout.LEFT));
		for (int i = 0; i < 50; i++)
		{
			userChoice.addItem(i + "." + "   ");
		}
		userChoice.setSize(60, 24);
		add(userChoice);
		add(contentInputted);
	}
}

//      

package djr.chess.gui;

import java.awt.BorderLayout;
import java.awt.List;
import java.awt.Panel;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListModel;

//      
public class UserListPad extends Panel
{
	public List userList = new List(10);
	
	public UserListPad()
	{
		setLayout(new BorderLayout());
		for (int i = 0; i < 10; i++)
		{
			userList.add(i + "." + "   ");
		}
		add(userList, BorderLayout.CENTER);
	}
}

/*******    ************\

//FIRPad

package djr.chess.pad;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

import javax.swing.JTextField;

public class FIRPad extends Panel implements MouseListener, ActionListener
{
	//        
	public boolean isMouseEnabled = false;
	//     
	public boolean isWinned = false; 
	//       
	public boolean isGaming = false;
	//    x    
	public int chessX_POS = -1;
	//    y    
	public int chessY_POS = -1;
	//      
	public int chessColor = 1;
	//   x      
	public int chessBlack_XPOS[] = new int[200];
	//   y      
	public int chessBlack_YPOS[] = new int[200];
	//   x      
	public int chessWhite_XPOS[] = new int[200];
	//   y      
	public int chessWhite_YPOS[] = new int[200];
	//     
	public int chessBlackCount = 0;
	//     
	public int chessWhiteCount = 0;
	//       
	public int chessBlackVicTimes = 0;
	//       
	public int chessWhiteVicTimes = 0;
	//    
	public Socket chessSocket;
	public DataInputStream inputData;
	public DataOutputStream outputData;
	public String chessSelfName = null;
	public String chessPeerName = null;
	public String host = null;
	public int port = 4331;
	public TextField statusText = new TextField("      !");
	public FIRThread firThread = new FIRThread(this);

	public FIRPad()
	{
		setSize(440, 440);
		setLayout(null);
		setBackground(Color.LIGHT_GRAY);
		addMouseListener(this);
		add(statusText);
		statusText.setBounds(new Rectangle(40, 5, 360, 24));
		statusText.setEditable(false);
	}

	//      
	public boolean connectServer(String ServerIP, int ServerPort) throws Exception
	{
		try
		{
			//       
			chessSocket = new Socket(ServerIP, ServerPort);
			//      
			inputData = new DataInputStream(chessSocket.getInputStream());
			//      
			outputData = new DataOutputStream(chessSocket.getOutputStream());
			firThread.start();
			return true;
		}
		catch (IOException ex)
		{
			statusText.setText("    ! 
"); } return false; } // public void setVicStatus(int vicChessColor) { // this.removeAll(); // for (int i = 0; i <= chessBlackCount; i++) { chessBlack_XPOS[i] = 0; chessBlack_YPOS[i] = 0; } // for (int i = 0; i <= chessWhiteCount; i++) { chessWhite_XPOS[i] = 0; chessWhite_YPOS[i] = 0; } // chessBlackCount = 0; // chessWhiteCount = 0; add(statusText); statusText.setBounds(40, 5, 360, 24); if (vicChessColor == 1) { // chessBlackVicTimes++; statusText.setText(" , : " + chessBlackVicTimes + ":" + chessWhiteVicTimes + ", , ..."); } else if (vicChessColor == -1) { // chessWhiteVicTimes++; statusText.setText(" , : " + chessBlackVicTimes + ":" + chessWhiteVicTimes + ", , ..."); } } // public void setLocation(int xPos, int yPos, int chessColor) { if (chessColor == 1) { // chessBlack_XPOS[chessBlackCount] = xPos * 20; chessBlack_YPOS[chessBlackCount] = yPos * 20; chessBlackCount++; } else if (chessColor == -1) { // chessWhite_XPOS[chessWhiteCount] = xPos * 20; chessWhite_YPOS[chessWhiteCount] = yPos * 20; chessWhiteCount++; } } // public boolean checkVicStatus(int xPos, int yPos, int chessColor) { int chessLinkedCount = 1; // int chessLinkedCompare = 1; // int chessToCompareIndex = 0; // int closeGrid = 1; // if (chessColor == 1) { // chessLinkedCount = 1; // , 1 // for , for (closeGrid = 1; closeGrid <= 4; closeGrid++) { // 4 for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) { // if (((xPos + closeGrid) * 20 == chessBlack_XPOS[chessToCompareIndex]) && ((yPos * 20) == chessBlack_YPOS[chessToCompareIndex])) { // 4 chessLinkedCount = chessLinkedCount + 1; // 1 if (chessLinkedCount == 5) { // , return true; } } } if (chessLinkedCount == (chessLinkedCompare + 1)) { chessLinkedCompare++; } else {// , , break; } } for (closeGrid = 1; closeGrid <= 4; closeGrid++) { for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) { if (((xPos - closeGrid) * 20 == chessBlack_XPOS[chessToCompareIndex]) && (yPos * 20 == chessBlack_YPOS[chessToCompareIndex])) { // 4 chessLinkedCount++; if (chessLinkedCount == 5) { return true; } } } if (chessLinkedCount == (chessLinkedCompare + 1)) { chessLinkedCompare++; } else { break; } } // for chessLinkedCount = 1; chessLinkedCompare = 1; for (closeGrid = 1; closeGrid <= 4; closeGrid++) { for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) { if ((xPos * 20 == chessBlack_XPOS[chessToCompareIndex]) && ((yPos + closeGrid) * 20 == chessBlack_YPOS[chessToCompareIndex])) { // 4 chessLinkedCount++; if (chessLinkedCount == 5) { return true; } } } if (chessLinkedCount == (chessLinkedCompare + 1)) { chessLinkedCompare++; } else { break; } } for (closeGrid = 1; closeGrid <= 4; closeGrid++) { for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) { if ((xPos * 20 == chessBlack_XPOS[chessToCompareIndex]) && ((yPos - closeGrid) * 20 == chessBlack_YPOS[chessToCompareIndex])) { // 4 chessLinkedCount++; if (chessLinkedCount == 5) { return true; } } } if (chessLinkedCount == (chessLinkedCompare + 1)) { chessLinkedCompare++; } else { break; } } chessLinkedCount = 1; chessLinkedCompare = 1; for (closeGrid = 1; closeGrid <= 4; closeGrid++) { for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) { if (((xPos - closeGrid) * 20 == chessBlack_XPOS[chessToCompareIndex]) && ((yPos + closeGrid) * 20 == chessBlack_YPOS[chessToCompareIndex])) { // 4 chessLinkedCount++; if (chessLinkedCount == 5) { return true; } } } if (chessLinkedCount == (chessLinkedCompare + 1)) { chessLinkedCompare++; } else { break; } } for (closeGrid = 1; closeGrid <= 4; closeGrid++) { for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) { if (((xPos + closeGrid) * 20 == chessBlack_XPOS[chessToCompareIndex]) && ((yPos - closeGrid) * 20 == chessBlack_YPOS[chessToCompareIndex])) { // 4 chessLinkedCount++; if (chessLinkedCount == 5) { return true; } } } if (chessLinkedCount == (chessLinkedCompare + 1)) { chessLinkedCompare++; } else { break; } } chessLinkedCount = 1; chessLinkedCompare = 1; for (closeGrid = 1; closeGrid <= 4; closeGrid++) { for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) { if (((xPos + closeGrid) * 20 == chessBlack_XPOS[chessToCompareIndex]) && ((yPos + closeGrid) * 20 == chessBlack_YPOS[chessToCompareIndex])) { // 4 chessLinkedCount++; if (chessLinkedCount == 5) { return true; } } } if (chessLinkedCount == (chessLinkedCompare + 1)) { chessLinkedCompare++; } else { break; } } for (closeGrid = 1; closeGrid <= 4; closeGrid++) { for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) { if (((xPos - closeGrid) * 20 == chessBlack_XPOS[chessToCompareIndex]) && ((yPos - closeGrid) * 20 == chessBlack_YPOS[chessToCompareIndex])) { // 4 chessLinkedCount++; if (chessLinkedCount == 5) { return true; } } } if (chessLinkedCount == (chessLinkedCompare + 1)) { chessLinkedCompare++; } else { break; } } } else if (chessColor == -1) { // chessLinkedCount = 1; for (closeGrid = 1; closeGrid <= 4; closeGrid++) { for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) { if (((xPos + closeGrid) * 20 == chessWhite_XPOS[chessToCompareIndex]) && (yPos * 20 == chessWhite_YPOS[chessToCompareIndex])) {// 4 chessLinkedCount++; if (chessLinkedCount == 5) { return true; } } } if (chessLinkedCount == (chessLinkedCompare + 1)) { chessLinkedCompare++; } else { break; } } for (closeGrid = 1; closeGrid <= 4; closeGrid++) { for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) { if (((xPos - closeGrid) * 20 == chessWhite_XPOS[chessToCompareIndex]) && (yPos * 20 == chessWhite_YPOS[chessToCompareIndex])) {// 4 chessLinkedCount++; if (chessLinkedCount == 5) { return true; } } } if (chessLinkedCount == (chessLinkedCompare + 1)) { chessLinkedCompare++; } else { break; } } chessLinkedCount = 1; chessLinkedCompare = 1; for (closeGrid = 1; closeGrid <= 4; closeGrid++) { for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) { if ((xPos * 20 == chessWhite_XPOS[chessToCompareIndex]) && ((yPos + closeGrid) * 20 == chessWhite_YPOS[chessToCompareIndex])) {// 4 chessLinkedCount++; if (chessLinkedCount == 5) { return true; } } } if (chessLinkedCount == (chessLinkedCompare + 1)) { chessLinkedCompare++; } else { break; } } for (closeGrid = 1; closeGrid <= 4; closeGrid++) { for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) { if ((xPos * 20 == chessWhite_XPOS[chessToCompareIndex]) && ((yPos - closeGrid) * 20 == chessWhite_YPOS[chessToCompareIndex])) {// 4 chessLinkedCount++; if (chessLinkedCount == 5) { return true; } } } if (chessLinkedCount == (chessLinkedCompare + 1)) { chessLinkedCompare++; } else { break; } } chessLinkedCount = 1; chessLinkedCompare = 1; for (closeGrid = 1; closeGrid <= 4; closeGrid++) { for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) { if (((xPos - closeGrid) * 20 == chessWhite_XPOS[chessToCompareIndex]) && ((yPos + closeGrid) * 20 == chessWhite_YPOS[chessToCompareIndex])) {// 4 chessLinkedCount++; if (chessLinkedCount == 5) { return true; } } } if (chessLinkedCount == (chessLinkedCompare + 1)) { chessLinkedCompare++; } else { break; } } for (closeGrid = 1; closeGrid <= 4; closeGrid++) { for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) { if (((xPos + closeGrid) * 20 == chessWhite_XPOS[chessToCompareIndex]) && ((yPos - closeGrid) * 20 == chessWhite_YPOS[chessToCompareIndex])) {// 4 chessLinkedCount++; if (chessLinkedCount == 5) { return true; } } } if (chessLinkedCount == (chessLinkedCompare + 1)) { chessLinkedCompare++; } else { break; } } chessLinkedCount = 1; chessLinkedCompare = 1; for (closeGrid = 1; closeGrid <= 4; closeGrid++) { for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) { if (((xPos + closeGrid) * 20 == chessWhite_XPOS[chessToCompareIndex]) && ((yPos + closeGrid) * 20 == chessWhite_YPOS[chessToCompareIndex])) {// 4 chessLinkedCount++; if (chessLinkedCount == 5) { return true; } } } if (chessLinkedCount == (chessLinkedCompare + 1)) { chessLinkedCompare++; } else { break; } } for (closeGrid = 1; closeGrid <= 4; closeGrid++) { for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) { if (((xPos - closeGrid) * 20 == chessWhite_XPOS[chessToCompareIndex]) && ((yPos - closeGrid) * 20 == chessWhite_YPOS[chessToCompareIndex])) {// 4 chessLinkedCount++; if (chessLinkedCount == 5) { return (true); } } } if (chessLinkedCount == (chessLinkedCompare + 1)) { chessLinkedCompare++; } else { break; } } } return false; } // public void paint(Graphics g) { for (int i = 40; i <= 380; i = i + 20) { g.drawLine(40, i, 400, i); } g.drawLine(40, 400, 400, 400); for (int j = 40; j <= 380; j = j + 20) { g.drawLine(j, 40, j, 400); } g.drawLine(400, 40, 400, 400); g.fillOval(97, 97, 6, 6); g.fillOval(337, 97, 6, 6); g.fillOval(97, 337, 6, 6); g.fillOval(337, 337, 6, 6); g.fillOval(217, 217, 6, 6); } // public void paintFirPoint(int xPos, int yPos, int chessColor) { FIRPointBlack firPBlack = new FIRPointBlack(this); FIRPointWhite firPWhite = new FIRPointWhite(this); if (chessColor == 1 && isMouseEnabled) { // // setLocation(xPos, yPos, chessColor); // isWinned = checkVicStatus(xPos, yPos, chessColor); if (isWinned == false) { // firThread.sendMessage("/" + chessPeerName + " /chess " + xPos + " " + yPos + " " + chessColor); this.add(firPBlack); // firPBlack.setBounds(xPos * 20 - 7, yPos * 20 - 7, 16, 16); // statusText.setText(" ( " + chessBlackCount + " )" + xPos + " " + yPos + ", ."); isMouseEnabled = false; // } else { // firThread.sendMessage("/" + chessPeerName + " /chess " + xPos + " " + yPos + " " + chessColor); this.add(firPBlack); firPBlack.setBounds(xPos * 20 - 7, yPos * 20 - 7, 16, 16); setVicStatus(1); // , isMouseEnabled = false; } } else if (chessColor == -1 && isMouseEnabled) { // setLocation(xPos, yPos, chessColor); isWinned = checkVicStatus(xPos, yPos, chessColor); if (isWinned == false) { firThread.sendMessage("/" + chessPeerName + " /chess " + xPos + " " + yPos + " " + chessColor); this.add(firPWhite); firPWhite.setBounds(xPos * 20 - 7, yPos * 20 - 7, 16, 16); statusText.setText(" ( " + chessWhiteCount + " )" + xPos + " " + yPos + ", ."); isMouseEnabled = false; } else { firThread.sendMessage("/" + chessPeerName + " /chess " + xPos + " " + yPos + " " + chessColor); this.add(firPWhite); firPWhite.setBounds(xPos * 20 - 7, yPos * 20 - 7, 16, 16); setVicStatus(-1); // , isMouseEnabled = false; } } } // public void paintNetFirPoint(int xPos, int yPos, int chessColor) { FIRPointBlack firPBlack = new FIRPointBlack(this); FIRPointWhite firPWhite = new FIRPointWhite(this); setLocation(xPos, yPos, chessColor); if (chessColor == 1) { isWinned = checkVicStatus(xPos, yPos, chessColor); if (isWinned == false) { this.add(firPBlack); firPBlack.setBounds(xPos * 20 - 7, yPos * 20 - 7, 16, 16); statusText.setText(" ( " + chessBlackCount + " )" + xPos + " " + yPos + ", ."); isMouseEnabled = true; } else { firThread.sendMessage("/" + chessPeerName + " /victory " + chessColor);//djr this.add(firPBlack); firPBlack.setBounds(xPos * 20 - 7, yPos * 20 - 7, 16, 16); setVicStatus(1); isMouseEnabled = true; } } else if (chessColor == -1) { isWinned = checkVicStatus(xPos, yPos, chessColor); if (isWinned == false) { this.add(firPWhite); firPWhite.setBounds(xPos * 20 - 7, yPos * 20 - 7, 16, 16); statusText.setText(" ( " + chessWhiteCount + " )" + xPos + " " + yPos + ", ."); isMouseEnabled = true; } else { firThread.sendMessage("/" + chessPeerName + " /victory " + chessColor); this.add(firPWhite); firPWhite.setBounds(xPos * 20 - 7, yPos * 20 - 7, 16, 16); setVicStatus(-1); isMouseEnabled = true; } } } // public void mousePressed(MouseEvent e) { if (e.getModifiers() == InputEvent.BUTTON1_MASK) { chessX_POS = (int) e.getX(); chessY_POS = (int) e.getY(); int a = (chessX_POS + 10) / 20, b = (chessY_POS + 10) / 20; if (chessX_POS / 20 < 2 || chessY_POS / 20 < 2 || chessX_POS / 20 > 19 || chessY_POS / 20 > 19) { // , } else { paintFirPoint(a, b, chessColor); // } } } public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void actionPerformed(ActionEvent e){} } // package djr.chess.pad; import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; public class FIRPointBlack extends Canvas { FIRPad padBelonged; // public FIRPointBlack(FIRPad padBelonged) { setSize(20, 20); // this.padBelonged = padBelonged; } public void paint(Graphics g) { // g.setColor(Color.black); g.fillOval(0, 0, 14, 14); } } // package djr.chess.pad; import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; public class FIRPointWhite extends Canvas { FIRPad padBelonged; // public FIRPointWhite(FIRPad padBelonged) { setSize(20, 20); this.padBelonged = padBelonged; } public void paint(Graphics g) { // g.setColor(Color.white); g.fillOval(0, 0, 14, 14); } } //FIRThread package djr.chess.pad; import java.io.IOException; import java.util.StringTokenizer; public class FIRThread extends Thread { FIRPad currPad; // public FIRThread(FIRPad currPad) { this.currPad = currPad; } // public void dealWithMsg(String msgReceived) { if (msgReceived.startsWith("/chess ")) { // StringTokenizer userMsgToken = new StringTokenizer(msgReceived, " "); // 、0 :x ;1 :y ;2 : String[] chessInfo = { "-1", "-1", "0" }; int i = 0; // String chessInfoToken; while (userMsgToken.hasMoreTokens()) { chessInfoToken = (String) userMsgToken.nextToken(" "); if (i >= 1 && i <= 3) { chessInfo[i - 1] = chessInfoToken; } i++; } currPad.paintNetFirPoint(Integer.parseInt(chessInfo[0]), Integer .parseInt(chessInfo[1]), Integer.parseInt(chessInfo[2])); } else if (msgReceived.startsWith("/yourname ")) { // currPad.chessSelfName = msgReceived.substring(10); } else if (msgReceived.equals("/error")) { // currPad.statusText.setText(" , !"); } } // public void sendMessage(String sndMessage) { try { currPad.outputData.writeUTF(sndMessage); } catch (Exception ea) { ea.printStackTrace();; } } public void run() { String msgReceived = ""; try { while (true) { // msgReceived = currPad.inputData.readUTF(); dealWithMsg(msgReceived); } } catch (IOException es){} } } /***** ******\ //FIRServer package djr.chess.server; import java.io.*; import java.net.*; import java.awt.*; import java.util.*; import java.awt.event.*; import javax.swing.JButton; // public class FIRServer extends Frame implements ActionListener { JButton clearMsgButton = new JButton(" "); JButton serverStatusButton = new JButton(" "); JButton closeServerButton = new JButton(" "); Panel buttonPanel = new Panel(); ServerMsgPanel serverMsgPanel = new ServerMsgPanel(); ServerSocket serverSocket; Hashtable clientDataHash = new Hashtable(50); // Hashtable clientNameHash = new Hashtable(50); // Hashtable chessPeerHash = new Hashtable(50); // public FIRServer() { super("Java "); setBackground(Color.LIGHT_GRAY); buttonPanel.setLayout(new FlowLayout()); clearMsgButton.setSize(60, 25); buttonPanel.add(clearMsgButton); clearMsgButton.addActionListener(this); serverStatusButton.setSize(75, 25); buttonPanel.add(serverStatusButton); serverStatusButton.addActionListener(this); closeServerButton.setSize(75, 25); buttonPanel.add(closeServerButton); closeServerButton.addActionListener(this); add(serverMsgPanel, BorderLayout.CENTER); add(buttonPanel, BorderLayout.SOUTH); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); pack(); setVisible(true); setSize(400, 300); setResizable(false); validate(); try { createServer(4331, serverMsgPanel); } catch (Exception e) { e.printStackTrace(); } } // public void createServer(int port, ServerMsgPanel serverMsgPanel) throws IOException { Socket clientSocket; // long clientAccessNumber = 1; // this.serverMsgPanel = serverMsgPanel; // try { serverSocket = new ServerSocket(port); serverMsgPanel.msgTextArea.setText(" :" + InetAddress.getLocalHost() + ":" //djr + serverSocket.getLocalPort() + "
"); while (true) { // clientSocket = serverSocket.accept(); serverMsgPanel.msgTextArea.append(" :" + clientSocket + "
"); // DataOutputStream outputData = new DataOutputStream(clientSocket .getOutputStream()); // clientDataHash.put(clientSocket, outputData); // clientNameHash .put(clientSocket, (" " + clientAccessNumber++)); // FIRServerThread thread = new FIRServerThread(clientSocket, clientDataHash, clientNameHash, chessPeerHash, serverMsgPanel); thread.start(); } } catch (IOException ex) { ex.printStackTrace(); } } public void actionPerformed(ActionEvent e) { if (e.getSource() == clearMsgButton) { // serverMsgPanel.msgTextArea.setText(""); } if (e.getSource() == serverStatusButton) { // try { serverMsgPanel.msgTextArea.append(" :" + InetAddress.getLocalHost() + ":" + serverSocket.getLocalPort() + "
"); } catch (Exception ee) { ee.printStackTrace(); } } if (e.getSource() == closeServerButton) { // System.exit(0); } } public static void main(String args[]) { FIRServer firServer = new FIRServer(); } } //FIRServerThread package djr.chess.server; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.util.Enumeration; import java.util.Hashtable; import java.util.StringTokenizer; public class FIRServerThread extends Thread { Socket clientSocket; // Hashtable clientDataHash; // Hash Hashtable clientNameHash; // Hash Hashtable chessPeerHash; // Hash ServerMsgPanel serverMsgPanel; boolean isClientClosed = false; public FIRServerThread(Socket clientSocket, Hashtable clientDataHash, Hashtable clientNameHash, Hashtable chessPeerHash, ServerMsgPanel server) { this.clientSocket = clientSocket; this.clientDataHash = clientDataHash; this.clientNameHash = clientNameHash; this.chessPeerHash = chessPeerHash; this.serverMsgPanel = server; } public void dealWithMsg(String msgReceived) { String clientName; String peerName; if (msgReceived.startsWith("/")) { if (msgReceived.equals("/list")) { // Feedback(getUserList()); } else if (msgReceived.startsWith("/creatgame [inchess]")) { // String gameCreaterName = msgReceived.substring(20); // synchronized (clientNameHash) { // clientNameHash.put(clientSocket, msgReceived.substring(11)); } synchronized (chessPeerHash) { // chessPeerHash.put(gameCreaterName, "wait"); } Feedback("/yourname " + clientNameHash.get(clientSocket)); sendGamePeerMsg(gameCreaterName, "/OK"); sendPublicMsg(getUserList()); } else if (msgReceived.startsWith("/joingame ")) { // StringTokenizer userTokens = new StringTokenizer(msgReceived, " "); String userToken; String gameCreatorName; String gamePaticipantName; String[] playerNames = { "0", "0" }; int nameIndex = 0; while (userTokens.hasMoreTokens()) { userToken = (String) userTokens.nextToken(" "); if (nameIndex >= 1 && nameIndex <= 2) { playerNames[nameIndex - 1] = userToken; // } nameIndex++; } gameCreatorName = playerNames[0]; gamePaticipantName = playerNames[1]; if (chessPeerHash.containsKey(gameCreatorName) && chessPeerHash.get(gameCreatorName).equals("wait")) { // synchronized (clientNameHash) { // clientNameHash.put(clientSocket, ("[inchess]" + gamePaticipantName)); } synchronized (chessPeerHash) { // chessPeerHash.put(gameCreatorName, gamePaticipantName); } sendPublicMsg(getUserList()); // sendGamePeerMsg(gamePaticipantName, ("/peer " + "[inchess]" + gameCreatorName)); // sendGamePeerMsg(gameCreatorName, ("/peer " + "[inchess]" + gamePaticipantName)); } else { // sendGamePeerMsg(gamePaticipantName, "/reject"); try { closeClient(); } catch (Exception ez) { ez.printStackTrace(); } } } else if (msgReceived.startsWith("/[inchess]")) { // int firstLocation = 0, lastLocation; lastLocation = msgReceived.indexOf(" ", 0); peerName = msgReceived.substring((firstLocation + 1), lastLocation); msgReceived = msgReceived.substring((lastLocation + 1)); if (sendGamePeerMsg(peerName, msgReceived)) { Feedback("/error"); } } else if (msgReceived.startsWith("/giveup ")) { // String chessClientName = msgReceived.substring(8); if (chessPeerHash.containsKey(chessClientName) && !((String) chessPeerHash.get(chessClientName)) .equals("wait")) { // , sendGamePeerMsg((String) chessPeerHash.get(chessClientName), "/youwin"); synchronized (chessPeerHash) { // chessPeerHash.remove(chessClientName); } } if (chessPeerHash.containsValue(chessClientName)) { // , sendGamePeerMsg((String) getHashKey(chessPeerHash, chessClientName), "/youwin"); synchronized (chessPeerHash) {// chessPeerHash.remove((String) getHashKey(chessPeerHash, chessClientName)); } } } else { // int lastLocation = msgReceived.indexOf(" ", 0); if (lastLocation == -1) { Feedback(" "); return; } } } else { msgReceived = clientNameHash.get(clientSocket) + ">" + msgReceived; serverMsgPanel.msgTextArea.append(msgReceived + "
"); sendPublicMsg(msgReceived); serverMsgPanel.msgTextArea.setCaretPosition(serverMsgPanel.msgTextArea.getText() .length()); } } // public void sendPublicMsg(String publicMsg) { synchronized (clientDataHash) { for (Enumeration enu = clientDataHash.elements(); enu .hasMoreElements();) { DataOutputStream outputData = (DataOutputStream) enu.nextElement(); try { outputData.writeUTF(publicMsg); } catch (IOException es) { es.printStackTrace(); } } } } // public boolean sendGamePeerMsg(String gamePeerTarget, String gamePeerMsg) { for (Enumeration enu = clientDataHash.keys(); enu.hasMoreElements();) { // Socket userClient = (Socket) enu.nextElement(); if (gamePeerTarget.equals((String) clientNameHash.get(userClient)) && !gamePeerTarget.equals((String) clientNameHash .get(clientSocket))) { // synchronized (clientDataHash) { // DataOutputStream peerOutData = (DataOutputStream) clientDataHash .get(userClient); try { // peerOutData.writeUTF(gamePeerMsg); } catch (IOException es) { es.printStackTrace(); } } return false; } } return true; } // public void Feedback(String feedBackMsg) { synchronized (clientDataHash) { DataOutputStream outputData = (DataOutputStream) clientDataHash .get(clientSocket); try { outputData.writeUTF(feedBackMsg); } catch (Exception eb) { eb.printStackTrace(); } } } // public String getUserList() { String userList = "/userlist"; for (Enumeration enu = clientNameHash.elements(); enu.hasMoreElements();) { userList = userList + " " + (String) enu.nextElement(); } return userList; } // value Hashtable key public Object getHashKey(Hashtable targetHash, Object hashValue) { Object hashKey; for (Enumeration enu = targetHash.keys(); enu.hasMoreElements();) { hashKey = (Object) enu.nextElement(); if (hashValue.equals((Object) targetHash.get(hashKey))) return hashKey; } return null; } // public void sendInitMsg() { sendPublicMsg(getUserList()); Feedback("/yourname " + (String) clientNameHash.get(clientSocket)); Feedback("Java "); Feedback("/list -- "); Feedback("/<username> <talk> -- "); Feedback(" : "); } public void closeClient() { serverMsgPanel.msgTextArea.append(" :" + clientSocket + "
"); synchronized (chessPeerHash) { // if (chessPeerHash.containsKey(clientNameHash.get(clientSocket))) { chessPeerHash.remove((String) clientNameHash.get(clientSocket)); } if (chessPeerHash.containsValue(clientNameHash.get(clientSocket))) { chessPeerHash.put((String) getHashKey(chessPeerHash, (String) clientNameHash.get(clientSocket)), "tobeclosed"); } } synchronized (clientDataHash) { // clientDataHash.remove(clientSocket); } synchronized (clientNameHash) { // clientNameHash.remove(clientSocket); } sendPublicMsg(getUserList()); serverMsgPanel.statusLabel.setText(" :" + clientDataHash.size()); try { clientSocket.close(); } catch (IOException exx) { exx.printStackTrace(); } isClientClosed = true; } public void run() { DataInputStream inputData; synchronized (clientDataHash) { serverMsgPanel.statusLabel.setText(" :" + clientDataHash.size()); } try { // inputData = new DataInputStream(clientSocket.getInputStream()); sendInitMsg(); while (true) { String message = inputData.readUTF(); dealWithMsg(message); } } catch (IOException esx){} finally { if (!isClientClosed) { closeClient(); } } } } //ServerMsgPanel package djr.chess.server; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Label; import java.awt.Panel; import java.awt.TextArea; import javax.swing.JLabel; // public class ServerMsgPanel extends Panel { public TextArea msgTextArea = new TextArea("", 22, 50, TextArea.SCROLLBARS_VERTICAL_ONLY); public JLabel statusLabel = new JLabel(" :", Label.LEFT); public Panel msgPanel = new Panel(); public Panel statusPanel = new Panel(); public ServerMsgPanel() { setSize(350, 300); setBackground(Color.LIGHT_GRAY); setLayout(new BorderLayout()); msgPanel.setLayout(new FlowLayout()); msgPanel.setSize(210, 210); statusPanel.setLayout(new BorderLayout()); statusPanel.setSize(210, 50); msgPanel.add(msgTextArea); statusPanel.add(statusLabel, BorderLayout.WEST); add(msgPanel, BorderLayout.CENTER); add(statusPanel, BorderLayout.NORTH); } } /******** *******\ //FIRClient package djr.chess.client; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import javax.swing.JFrame; import djr.chess.gui.UserChatPad; import djr.chess.gui.UserControlPad; import djr.chess.gui.UserInputPad; import djr.chess.gui.UserListPad; import djr.chess.pad.FIRPad; // public class FIRClient extends JFrame implements ActionListener, KeyListener { // Socket clientSocket; // DataInputStream inputStream; // DataOutputStream outputStream; // String chessClientName = null; // String host = null; // int port = 4331; // boolean isOnChat = false; // boolean isOnChess = false; // boolean isGameConnected = false; // boolean isCreator = false; // boolean isParticipant = false; // UserListPad userListPad = new UserListPad(); // UserChatPad userChatPad = new UserChatPad(); // UserControlPad userControlPad = new UserControlPad(); // UserInputPad userInputPad = new UserInputPad(); // FIRPad firPad = new FIRPad(); // Panel southPanel = new Panel(); Panel northPanel = new Panel(); Panel centerPanel = new Panel(); Panel eastPanel = new Panel(); // , public FIRClient() { super("Java "); setLayout(new BorderLayout()); host = userControlPad.ipInputted.getText(); eastPanel.setLayout(new BorderLayout()); eastPanel.add(userListPad, BorderLayout.NORTH); eastPanel.add(userChatPad, BorderLayout.CENTER); eastPanel.setBackground(Color.LIGHT_GRAY); userInputPad.contentInputted.addKeyListener(this); firPad.host = userControlPad.ipInputted.getText(); centerPanel.add(firPad, BorderLayout.CENTER); centerPanel.add(userInputPad, BorderLayout.SOUTH); centerPanel.setBackground(Color.LIGHT_GRAY); userControlPad.connectButton.addActionListener(this); userControlPad.createButton.addActionListener(this); userControlPad.joinButton.addActionListener(this); userControlPad.cancelButton.addActionListener(this); userControlPad.exitButton.addActionListener(this); userControlPad.createButton.setEnabled(false); userControlPad.joinButton.setEnabled(false); userControlPad.cancelButton.setEnabled(false); southPanel.add(userControlPad, BorderLayout.CENTER); southPanel.setBackground(Color.LIGHT_GRAY); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { if (isOnChat) { // try { // clientSocket.close(); } catch (Exception ed){} } if (isOnChess || isGameConnected) { // try { // firPad.chessSocket.close(); } catch (Exception ee){} } System.exit(0); } }); add(eastPanel, BorderLayout.EAST); add(centerPanel, BorderLayout.CENTER); add(southPanel, BorderLayout.SOUTH); pack(); setSize(670, 560); setVisible(true); setResizable(false); this.validate(); } // IP public boolean connectToServer(String serverIP, int serverPort) throws Exception { try { // clientSocket = new Socket(serverIP, serverPort); // inputStream = new DataInputStream(clientSocket.getInputStream()); // outputStream = new DataOutputStream(clientSocket.getOutputStream()); // FIRClientThread clientthread = new FIRClientThread(this); // , clientthread.start(); isOnChat = true; return true; } catch (IOException ex) { userChatPad.chatTextArea .setText(" !
"); } return false; } // public void actionPerformed(ActionEvent e) { if (e.getSource() == userControlPad.connectButton) { // host = firPad.host = userControlPad.ipInputted.getText(); // try { if (connectToServer(host, port)) { // , userChatPad.chatTextArea.setText(""); userControlPad.connectButton.setEnabled(false); userControlPad.createButton.setEnabled(true); userControlPad.joinButton.setEnabled(true); firPad.statusText.setText(" , !"); } } catch (Exception ei) { userChatPad.chatTextArea .setText(" !
"); } } if (e.getSource() == userControlPad.exitButton) { // if (isOnChat) { // try { // clientSocket.close(); } catch (Exception ed){} } if (isOnChess || isGameConnected) { // try { // firPad.chessSocket.close(); } catch (Exception ee){} } System.exit(0); } if (e.getSource() == userControlPad.joinButton) { // String selectedUser = (String)userListPad.userList.getSelectedItem(); // if (selectedUser == null || selectedUser.startsWith("[inchess]") || selectedUser.equals(chessClientName)) { // , , firPad.statusText.setText(" !"); } else { // try { if (!isGameConnected) { // if (firPad.connectServer(firPad.host, firPad.port)) { // isGameConnected = true; isOnChess = true; isParticipant = true; userControlPad.createButton.setEnabled(false); userControlPad.joinButton.setEnabled(false); userControlPad.cancelButton.setEnabled(true); firPad.firThread.sendMessage("/joingame " + (String)userListPad.userList.getSelectedItem() + " " + chessClientName); } } else { // isOnChess = true; isParticipant = true; userControlPad.createButton.setEnabled(false); userControlPad.joinButton.setEnabled(false); userControlPad.cancelButton.setEnabled(true); firPad.firThread.sendMessage("/joingame " + (String)userListPad.userList.getSelectedItem() + " " + chessClientName); } } catch (Exception ee) { isGameConnected = false; isOnChess = false; isParticipant = false; userControlPad.createButton.setEnabled(true); userControlPad.joinButton.setEnabled(true); userControlPad.cancelButton.setEnabled(false); userChatPad.chatTextArea .setText(" :
" + ee); } } } if (e.getSource() == userControlPad.createButton) { // try { if (!isGameConnected) { // if (firPad.connectServer(firPad.host, firPad.port)) { // isGameConnected = true; isOnChess = true; isCreator = true; userControlPad.createButton.setEnabled(false); userControlPad.joinButton.setEnabled(false); userControlPad.cancelButton.setEnabled(true); firPad.firThread.sendMessage("/creatgame " + "[inchess]" + chessClientName); } } else { // isOnChess = true; isCreator = true; userControlPad.createButton.setEnabled(false); userControlPad.joinButton.setEnabled(false); userControlPad.cancelButton.setEnabled(true); firPad.firThread.sendMessage("/creatgame " + "[inchess]" + chessClientName); } } catch (Exception ec) { isGameConnected = false; isOnChess = false; isCreator = false; userControlPad.createButton.setEnabled(true); userControlPad.joinButton.setEnabled(true); userControlPad.cancelButton.setEnabled(false); ec.printStackTrace(); userChatPad.chatTextArea.setText(" :
" + ec); } } if (e.getSource() == userControlPad.cancelButton) { // if (isOnChess) { // firPad.firThread.sendMessage("/giveup " + chessClientName); firPad.setVicStatus(-1 * firPad.chessColor); userControlPad.createButton.setEnabled(true); userControlPad.joinButton.setEnabled(true); userControlPad.cancelButton.setEnabled(false); firPad.statusText.setText(" !"); } if (!isOnChess) { // userControlPad.createButton.setEnabled(true); userControlPad.joinButton.setEnabled(true); userControlPad.cancelButton.setEnabled(false); firPad.statusText.setText(" !"); } isParticipant = isCreator = false; } } public void keyPressed(KeyEvent e) { TextField inputwords = (TextField) e.getSource(); if (e.getKeyCode() == KeyEvent.VK_ENTER) { // if (userInputPad.userChoice.getSelectedItem().equals(" ")) { // try { // outputStream.writeUTF(inputwords.getText()); inputwords.setText(""); } catch (Exception ea) { userChatPad.chatTextArea .setText(" !
"); userListPad.userList.removeAll(); userInputPad.userChoice.removeAll(); inputwords.setText(""); userControlPad.connectButton.setEnabled(true); } } else { // try { outputStream.writeUTF("/" + userInputPad.userChoice.getSelectedItem() + " " + inputwords.getText()); inputwords.setText(""); } catch (Exception ea) { userChatPad.chatTextArea .setText(" !
"); userListPad.userList.removeAll(); userInputPad.userChoice.removeAll(); inputwords.setText(""); userControlPad.connectButton.setEnabled(true); } } } } public void keyTyped(KeyEvent e) {} public void keyReleased(KeyEvent e) {} public static void main(String args[]) { FIRClient chessClient = new FIRClient(); } } //FIRClientThread package djr.chess.client; import java.io.IOException; import java.util.StringTokenizer; import javax.swing.DefaultListModel; import javax.swing.ListModel; public class FIRClientThread extends Thread { public FIRClient firClient; public FIRClientThread(FIRClient firClient) { this.firClient = firClient; } public void dealWithMsg(String msgReceived) { if (msgReceived.startsWith("/userlist ")) { // StringTokenizer userToken = new StringTokenizer(msgReceived, " "); int userNumber = 0; // firClient.userListPad.userList.removeAll(); // firClient.userInputPad.userChoice.removeAll(); // firClient.userInputPad.userChoice.addItem(" "); while (userToken.hasMoreTokens()) { // String user = (String) userToken.nextToken(" "); // if (userNumber > 0 && !user.startsWith("[inchess]")) { // firClient.userListPad.userList.add(user);// firClient.userInputPad.userChoice.addItem(user); // } userNumber++; } firClient.userInputPad.userChoice.setSelectedIndex(0);// } else if (msgReceived.startsWith("/yourname ")) { // firClient.chessClientName = msgReceived.substring(10); // firClient.setTitle("Java " + " :" + firClient.chessClientName); // Frame } else if (msgReceived.equals("/reject")) { // try { firClient.firPad.statusText.setText(" !"); firClient.userControlPad.cancelButton.setEnabled(false); firClient.userControlPad.joinButton.setEnabled(true); firClient.userControlPad.createButton.setEnabled(true); } catch (Exception ef) { firClient.userChatPad.chatTextArea .setText("Cannot close!"); } firClient.userControlPad.joinButton.setEnabled(true); } else if (msgReceived.startsWith("/peer ")) { // firClient.firPad.chessPeerName = msgReceived.substring(6); if (firClient.isCreator) { // firClient.firPad.chessColor = 1; // firClient.firPad.isMouseEnabled = true; firClient.firPad.statusText.setText(" ..."); } else if (firClient.isParticipant) { // firClient.firPad.chessColor = -1; // firClient.firPad.statusText.setText(" , ."); } } else if (msgReceived.equals("/youwin")) { // firClient.isOnChess = false; firClient.firPad.setVicStatus(firClient.firPad.chessColor); firClient.firPad.statusText.setText(" "); firClient.firPad.isMouseEnabled = false; } else if (msgReceived.equals("/OK")) { // firClient.firPad.statusText.setText(" "); } else if (msgReceived.equals("/error")) { // firClient.userChatPad.chatTextArea.append(" , .
"); } else { firClient.userChatPad.chatTextArea.append(msgReceived + "
"); firClient.userChatPad.chatTextArea.setCaretPosition( firClient.userChatPad.chatTextArea.getText().length()); } } public void run() { String message = ""; try { while (true) { // , wait message = firClient.inputStream.readUTF(); dealWithMsg(message); } } catch (IOException es){} } }
 
 
 
モジュール名
ファイル名
機能の説明
ユーザパネル
UserListPad.java    User ChatPad.java    UserInputPad.java    UserControl Pad.java   
四つのパネルでは、サーバー上のユーザ、チャット、チャット情報やコマンド情報を一覧表示し、作成、ゲームに参加するなどの操作を行います。
ボード
FIRPoil Black.java    FIRPoil White.java    FIRPad.java    FIRTh read.java   
黒の駒類、白の駒類、碁盤のパネル類はユーザーがここで碁を打つことができるようにします。複数のユーザーをサポートするために、碁盤のスレッド類が含まれています。
サーバ
ServerMsgPanel.java    FIRSERver Thread.java    FIRSER.java   
サーバ情報出力パネル、マルチスレッド類、サーバ端類
クライアント
FIRC lient.java    FIRC lient Thread.java   
      クライアントクラス