基于java平台设计--游戏大厅

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


import java.io.*;
import java.net.*;


public class Second
{
	public static JFrame jf2 = new JFrame();
	public static JFrame jf3;
	
	private JTabbedPane jtp2 = new JTabbedPane();             //创建标签面板
	private JTabbedPane jtp3 = new JTabbedPane();
	
	private JPanel jp1 = new JPanel();
	private JPanel jp11= new JPanel();
	private JPanel jp112 = new JPanel();
	private JPanel jp12 = new JPanel();
	private JPanel jp2 = new JPanel();	
	private JPanel jp22 = new JPanel();
	
	private JButton jb1 = new JButton();
	private JButton jb2 = new JButton();
	private JButton lc[] = new JButton[10];
	private JButton rc[] = new JButton[10];
	
	private JLabel jl22 = new JLabel();
	private JLabel jl23 = new JLabel();
	private JLabel lcn[] = new JLabel[10];                 //左边桌子名字
	private JLabel rcn[] = new JLabel[10];                 //右边桌子名字
	private JLabel desk[] = new JLabel[10];
	private JLabel jl111 = new JLabel("<< 象棋游戏  >>");
	private JLabel jl21 = new JLabel(new ImageIcon("./res/img/boy2.gif"));	
	
	private JTextField name = new JTextField();
	private JTextArea jta1 = new JTextArea(50,11);                
	private ImageIcon headicon = new ImageIcon();
	
	private int lnum = 0;
	private int rnum = 0;
	
	private DrawQiPan chessboard = new DrawQiPan();
	private Third th;
	public Socket s = new Socket();
	public static DataInputStream dis;
	public static DataOutputStream dos;


	
	public Second(JTextField name, JFrame jf2, ImageIcon headicon)
	{
		this.name = name;                                               //继承上一届面用户图片
		this.jf2 = jf2;
		this.headicon = headicon;                                      //继承上一届面用户图片
	}
	
	public void connect()                                                    //连接服务器
	{
		try
		{
			s = new Socket("10.0.162.52",9978);
			dos = new DataOutputStream(s.getOutputStream());                 //获得服务器的输出流即能写信息给服务器       
			dis = new DataInputStream(s.getInputStream());                   //获得服务器的输入流即能读取来自服务器的信息
			dos.writeUTF(name.getText());                                    //将用户的名称写入客户端
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public void createThread()                                        //线程处理,主要用于接收信息
	{
		Client player = new Client(this);                             
		player.start();                                               //开始线程
	}
	
	public JSplitPane createPane()                                             //第二个界面编辑
	{
		J_Action a = new J_Action();
		JSplitPane mainSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);    //平衡分割  
		JSplitPane rightSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);    //竖直分割
		
		jp112.setLayout(new FlowLayout());                              //流式布局
		jb1 = new JButton("加入");        
		jb2 = new JButton("退出");
		jb1.addActionListener(a);                                        //加入和退出设置监听器
		jb2.addActionListener(a);
		jp112.add(jb1);
		jp112.add(jb2);
		jp11.setLayout(new BorderLayout());                                      //默认布局
		jp11.add(jl111, "West");
		jp11.add(jp112, "East");
		
		jp12.setLayout(new GridLayout(5,2));                           //整个座位面板网格式布局5行2列
		int count = 1;
		for(int i = 0; i < 10; i++ )                                    
		{
			lcn[i] = new JLabel(name.getText());
			lcn[i].setVisible(false);
			lc[i] = new JButton(new ImageIcon("./res/img/noone.gif"));
			lc[i].setBackground(new Color(153,153,153));
			lc[i].setActionCommand(String.valueOf(i));
			lc[i].addActionListener(a);
			
			JPanel jp121 = new JPanel();
			jp121.setLayout(new GridLayout(2,1));
			jp121.setBackground(new Color(153,153,153));
			jp121.add(lc[i]);                                             //左边桌子
			jp121.add(lcn[i]);                                            //椅子下面的人物名字
			
			rcn[i] = new JLabel(name.getText());
			rcn[i].setVisible(false);
			rc[i] = new JButton(new ImageIcon("./res/img/noone.gif"));
			rc[i].setBackground(new Color(153,153,153));
			rc[i].setActionCommand(String.valueOf(i + 10));
			rc[i].addActionListener(a);
			
			JPanel jp122 = new JPanel();
			jp122.setLayout(new GridLayout(2,1));
			jp122.setBackground(new Color(153,153,153));
			jp122.add(rc[i]);
			jp122.add(rcn[i]);
			
			desk[i]= new JLabel(new ImageIcon("./res/img/xqnoone.gif"));
			JLabel deskNum = new JLabel( "      -" + count + "-      ");
			
			JPanel jp123 = new JPanel();
			jp123.setLayout(new GridLayout(2,1));
			jp123.add(desk[i]);
			jp123.add(deskNum);
			jp123.setBackground(new Color(153,153,153));


			JPanel jpdesk = new JPanel();
			jpdesk.add(jp121);
			jpdesk.add(jp123);
			jpdesk.add(jp122);
			jpdesk.setBackground(new Color(153,153,153));
			jp12.add(jpdesk);
			count++;
		}
		jp1.setLayout(new BoxLayout(jp1, BoxLayout.Y_AXIS));                  //合式布局
		jp1.add(jp11);
		jp1.add(jp12);
		
		jl22 = new JLabel(headicon);                                           //将人物图标和名字添加进面板
		jl23 = new JLabel(name.getText());


		jp22.add(jl22);
		jp22.add(jl23);
		
		jp2.add(jl21,BorderLayout.NORTH);
		jp2.add(jp22,BorderLayout.SOUTH);
		jtp2.addTab("个人信息",new ImageIcon("./res/img/star.gif"),jp2,"个人信息");
		
		JScrollPane jsp = new JScrollPane(jta1);
		jtp3.addTab("服务器信息", null, jsp, "服务器信息");
		
		rightSplitPane.setBottomComponent(jtp3);
		rightSplitPane.setTopComponent(jtp2);
		rightSplitPane.setDividerLocation(310);                                //分割面板
		rightSplitPane.setDividerSize(5);
		
		mainSplitPane.setRightComponent(rightSplitPane);
		mainSplitPane.setLeftComponent(jp1);
		mainSplitPane.setDividerLocation(450);
		mainSplitPane.setDividerSize(5);
		
		return mainSplitPane;	
	}


	class J_Action implements ActionListener                                //第二个界面的事件响应
	{
		public void actionPerformed(ActionEvent e) 
		{
			JButton b =(JButton)e.getSource();
			if(b == jb1)
			{
				String iconstring = headicon.toString();			
				String desknum = String.valueOf(1);
				try
				{
					dos.writeUTF("sit");                                  
					dos.writeUTF(desknum);            
					dos.writeUTF(iconstring);
				}
				catch(Exception ee)
				{
					ee.printStackTrace();
				}
			}
			else if(b == jb2)
			{
				try
				{
					dos.writeUTF("exit");
					System.exit(0);
				}
				catch(Exception ee)
				{
					ee.printStackTrace();
			    }
			}
			else
			{
				String s = e.getActionCommand();
				int num = Integer.parseInt(s);
				String iconstring = headicon.toString();			
				String desknum = String.valueOf(num + 1);
				try
				{
					dos.writeUTF("sit");
					dos.writeUTF(desknum);
					dos.writeUTF(iconstring);
				}
				catch(Exception ee)
				{
					ee.printStackTrace();
				}
			} 
		}
	}


	public DataInputStream getDataInputStream()
	{
		return dis;
	}
	
	public JTextArea getTextArea()
	{
		return jta1;
	}


	class Client extends Thread
	{
		private Second client;
		public Client(Second client)
		{
			this.client = client;
		}
		
		public void run()
		{
			DataInputStream dis = client.getDataInputStream();
			try{
				while(true){
					String info = dis.readUTF();
					if(info.equals("comein"))
					{
						String desknum = dis.readUTF();
						int num = Integer.parseInt(desknum);
						for(int i = 0; i < num; ++i)
						{			
							try{
							String DeskNum = dis.readUTF();
							String ClientName = dis.readUTF();
							String IconString = dis.readUTF();
							int Num = Integer.parseInt(DeskNum);
							if (Num >= 11)
							{
								rc[Num-11].setIcon(new ImageIcon(IconString));
								rcn[Num-11].setText(ClientName);
								rcn[Num-11].setVisible(true);	
							}
							else
							{
								lc[Num-1].setIcon(new ImageIcon(IconString));
								lcn[Num-1].setText(ClientName);
								lcn[Num-1].setVisible(true);
							}
							
							}catch(Exception e){
								e.printStackTrace();
							}
						}
						String ServerInfo = dis.readUTF();
						client.getTextArea().append(ServerInfo+"\n");					
					}
					else if (info.equals("sit"))
					{						
						try{
							String desknum = dis.readUTF();
							String name = dis.readUTF();
							String iconstring = dis.readUTF();			
							int num = Integer.parseInt(desknum);
		
							for(int i=0;i<10;i++)
							{
								if(lc[i].getIcon().toString().equals(iconstring)&&lcn[i].getText().equals(name))
								{
									lc[i].setIcon(new ImageIcon("./res/img/noone.gif"));
									lcn[i].setVisible(false);						 
								}
								if(rc[i].getIcon().toString().equals(iconstring)&&rcn[i].getText().equals(name))
								{
									rc[i].setIcon(new ImageIcon("./res/img/noone.gif"));
									rcn[i].setVisible(false);
								}													
							}
							
							if (num<11)
							{
								lc[num-1].setIcon(new ImageIcon(iconstring));
								lcn[num-1].setText(name);
								lcn[num-1].setVisible(true);
								lnum = num;
							}
							else
							{
								rc[num-11].setIcon(new ImageIcon(iconstring));
								rcn[num-11].setText(name);
								rcn[num-11].setVisible(true);
								rnum = num-10;
							}
							
						}
						catch(Exception e)
						{
							e.printStackTrace();
						}
						 
					}
					else if (info.equals("exit"))
					{
						try
						{
		                	int num = Integer.parseInt(dis.readUTF());
		                	if (num<11)
	               		{
		                		lc[num - 1].setIcon(new ImageIcon("./res/img/noone.gif"));
		               		lcn[num - 1].setVisible(false);
		               	}
		               	else
		               	{
		               		num = num-10;
             		       	rc[num - 1].setIcon(new ImageIcon("./res/img/noone.gif"));
             		       	rcn[num - 1].setVisible(false);	
		                	}	
		               }
						catch(Exception e)
		               {
		                   e.printStackTrace();
	                   }							
					}
					else if(info.equals("play"))
					{
						try
						{
							String name2 = dis.readUTF();
							String iconstring2 = dis.readUTF();
							Third.headicon2 = iconstring2;
							Third.name2 = name2;
							Third.talk.append(Third.name2+"进入房间"+"\n");
							chessboard.dispaySelect = 1;
							jf3 = new JFrame("象棋游戏");
							th = new Third(th,jf3,chessboard,name,jl22);
							JSplitPane jsp = th.createPane();
							JTabbedPane jtp = new JTabbedPane();
							jtp.addTab("象棋游戏", null, jsp, "象棋游戏");
							jf3.setContentPane(jtp);
							jf3.setLocationRelativeTo(null);
							jf3.setSize(800,700);
							jf3.setVisible(true);
						}
						catch(Exception e)
						{
							e.printStackTrace();
						}
					}
					else if(info.equals("Move"))//当对手有移动棋子时
					{
						int H_YS = dis.readInt();//得到对手移动的坐标
						int H_XS = dis.readInt();//得到对手移动的坐标
						int B_YS = dis.readInt();//得到对手移动的坐标
						int B_XS = dis.readInt();//得到对手移动的坐标
						boolean Rule = dis.readBoolean();
						
						H_YS = 9 - H_YS;//转换成对方的移动
						H_XS = 8 - H_XS;
						B_YS = 9 - B_YS;
						B_XS = 8 - B_XS;
						chessboard.MoveMethod(H_YS, H_XS, B_YS, B_XS, Rule);//运用下一个类方法重画棋盘
					}
					else if(info.equals("Chat"))
					{
						String Chat = dis.readUTF();
						Third.talk.append("对手:"+Chat+"\n");
					}
					else if(info.equals("Start"))
					{
						try
						{
							JOptionPane.showMessageDialog(null,"游戏开始", "开始游戏", JOptionPane.INFORMATION_MESSAGE);
						}
						catch(Exception ee)
						{
							ee.printStackTrace();
						}
						chessboard.HaveStart = true;
						chessboard.repaint();
					}
					else
					{
						client.getTextArea().append(info+"\n");
					}					
				}
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}
}

















 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值