⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chessframe.java

📁 我的课程设计的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   		}
   		if(arg.equals("40*30"))
   		{
   			this.width = 40;
   			this.height = 30;
   			cm = new ChessModel(3);
   			MapSize(this.width,this.height);
   			SwingUtilities.updateComponentTreeUI(this);
   		}
   		
   		//本地对战 菜单中各菜单的处理,此菜单在网络对战时不可用
   		//选中某一项后,刷新棋盘,开始新的一局
   		 if(arg.equals("人机对弈"))
   		 {
   		 	this.checkcomputer = true;
   		 	this.iscomputer = true;
   		 	cm = new ChessModel(cm.getDegree());
   		 	MapSize(cm.getWidth(),cm.getHeight());
   		 	SwingUtilities.updateComponentTreeUI(this);
   		 }
         if(arg.equals("双人对弈"))
         {
         	this.checkcomputer = true;
         	this.iscomputer = true;
         	cm = new ChessModel(cm.getDegree());
         	MapSize(cm.getWidth(),cm.getHeight());
         	SwingUtilities.updateComponentTreeUI(this);
         }   		
   		
   		//网络对弈的菜单处理
   		//启动服务器菜单项的处理
   		if(arg.equals("启动服务器"))
   		{
   			if(JOptionPane.showConfirmDialog(this,"确定要启动服务器,允许其他玩家连接本机吗?","启动服务器",
   			JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
   			{
   				try
   				{
   					serversocket = new ServerSocket(DEFAULT_PORT);
   					//新建ServerSocket类的实例serversocket
   					System.out.println("服务器启动,正在监听>>>>>>>>>>");
   				}
   				catch(IOException ex)
   				{
   					System.out.println(ex);
   				}
   				try
   				{
   					clientsocket = serversocket.accept();
   					//阻塞进程,直到获得客户端的连接,
   					//然后返回客服端Socket实例clientsocket
   					System.out.println("正在接收连接>>>>>>>>>>>");
   					in = clientsocket.getInputStream();   //获得clientsocket实例的输入流
   					out = clientsocket.getOutputStream();
   					this.isserver = true;
   					this.isnet = true;
   					this.istoken = true;
   					socketthread = new SocketThread(this,mp);//新建SocketThread线程类的实例,用来监听来自对方的信息
   					socketthread.start();   //线程开始运行
   					JOptionPane.showMessageDialog(this,"连接建立成功,可以开始游戏!","连接成功",
   					JOptionPane.INFORMATION_MESSAGE);
   					
   					
   					//此处应该有使“启动服务器”菜单项无效,“连接服务器”菜单项无效,
   					//“中断连接”菜单项有效,“棋盘”菜单项无效,
   					//“本地对战”菜单无效的处理
   					serverMenuItem.setEnabled(false);
   					connectMenuItem.setEnabled(false);
   					boardMenuItem.setEnabled(false);
   					disconnectMenuItem.setEnabled(true);
   					localchessMenu.setEnabled(false);
   				}
   				catch(IOException ex)
   				{
   					System.out.println(ex);
   				}
   				
   				try
   				{
   					serversocket.close();
   					System.out.println("服务器关闭>>>>>>>>>>>>");
   				}
   				catch(IOException ioe)
   				{
   			       System.out.println(ioe);
   				}
   			}
   			MapSize(cm.getWidth(),cm.getHeight());
   			SwingUtilities.updateComponentTreeUI(this);
   		}
   		
   		//“连接服务器”菜单项的处理
   		if(arg.equals("连接服务器"))
   		{
   			String input = null;
   			InetAddress ia;
   			input = JOptionPane.showInputDialog(this,"请输入服务器的IP地址:","连接服务器",
   			JOptionPane.QUESTION_MESSAGE);
   			if(input!=null)
   			{
   				if(input.length()==0)
   					JOptionPane.showMessageDialog(this,"错误!输入的IP地址不能为空!","错误",
   					JOptionPane.ERROR_MESSAGE);
   				else
   				{
   					try
   					{
   						ia = InetAddress.getByName(input);
   						//得到输入的IP地址InetAddress类的实例ia
   						clientsocket = new Socket(ia,DEFAULT_PORT);
   						//新建连接到远程主机的固定端口5454的Socket类的实例clientsocket
   						in = clientsocket.getInputStream(); //得到clientsocket的输入流
   						out = clientsocket.getOutputStream();//得到clientsocket的输入流
   						this.isserver = false;               //设置本机客户端
   						this.isnet = true;                   //设置为网络对站
   						this.istoken = false;
   						//开始时令牌在服务器端
   						//客户端待服务器端下棋后才可以得到令牌下棋子
   						socketthread = new SocketThread(this,mp);
   						//新建SocketThead线程类的实例,用来监听来自对方的信息
   						socketthread.start();         //线程开始运行
   						JOptionPane.showMessageDialog(this,"连接建立成功,可以开始游戏!",
   							"连接成功",JOptionPane.INFORMATION_MESSAGE);
   							//此处应该有使”启动服务器“菜单项无效,”连接服务器“菜单项无效,
   							//”中断连接“菜单项有效,”棋盘“菜单项无效,
   							//”本地对弈“菜单无效的处理
   							serverMenuItem.setEnabled(false);
   							connectMenuItem.setEnabled(false);
   							disconnectMenuItem.setEnabled(true);
   							boardMenuItem.setEnabled(false);
   						    localchessMenu.setEnabled(false);
   					}
   					catch(UnknownHostException ex)
   					{
   						System.out.println(ex);
   						JOptionPane.showMessageDialog(this,"输入的IP地址有误,或输入的IP地址不可连接!","错误",
   						JOptionPane.ERROR_MESSAGE);
   					}
   					catch(IOException ex)
   					{
   						System.out.println(ex);
   						JOptionPane.showMessageDialog(this,"连接建立失败,请重试!",
   						"失败",JOptionPane.WARNING_MESSAGE);
   					}
   				}
   			}
   			
   			MapSize(cm.getWidth(),cm.getHeight());
   			SwingUtilities.updateComponentTreeUI(this);
   		}
   		
   		
   		//“中断连接”菜单项的处理
   		if(arg.equals("中断连接"))
   		{
   			byte msg[] = new byte[2];
   			msg[0] = -1;
   			msg[1] = -1;
   			try{
   				out.write(msg);
   			}catch (IOException ex){
   				System.out.println(ex);
   			}
   			try{
   				Thread.sleep(500);
   				socketthread.StopRun();//停止接受信息的线程
   				this.isnet = false;
   				JOptionPane.showMessageDialog(this,"连接中断!","网络中断",
   						JOptionPane.ERROR_MESSAGE);
   				
   				//此处应该有使“启动服务器”菜单项有效,“连接服务器”菜单项有效,
   				//“中断连接”菜单项无效,“棋盘”菜单项有效,“本地对战”菜单有效的处理
   				serverMenuItem.setEnabled(true);
   				connectMenuItem.setEnabled(true);
   				disconnectMenuItem.setEnabled(false);
   				boardMenuItem.setEnabled(true);
   				localchessMenu.setEnabled(true);  				
   			}catch(InterruptedException ito){
   				System.out.println(ito);
   			}
   		
   		MapSize(cm.getWidth(),cm.getHeight());
   		SwingUtilities.updateComponentTreeUI(this);
      }
   		
   		//"开局"菜单项的处理
   		if(arg.equals("开局")){
   			if(!this.isnet)        //如果是本地对弈,那么直接调用方法restart()开始新的一局
   			restart();           
   			else                   //如果是网路对战,那么首先给对方发送一个新开局命令,
   				                    //然后调用restart()方法
   			{
   				 byte msg[] = new byte[2];
   				 msg[0] = -1;
   				 msg[1] = 0;     //新开局命令,第一个字节为-1,第二个字节为0
   				 
   				 try{
   					 out.write(msg); //将开局命令传送给对方
   					 System.out.println("开局信息发送出>>>>>>>>>>>>"); 
   				 }catch(IOException ex){
   					 System.out.println(ex);
   				 }
   				 try{
   					 Thread.sleep(1000);
   				 }catch(InterruptedException ito){
   					 System.out.println(ito);
   				 }
   				 if(this.isserver)
   					 this.istoken = true;
   				 else
   					 this.istoken = false;
   				 restart();
   			}
   		}
   		
   		//“音乐”菜单项的处理
   		if(arg.equals("音乐")){
   			if(backsound.hasPlayed){
   				backsound.stop();
   			}
   			else{
   				backsound.loop();
   			}
   		}
   		
   		
   		//“关于”菜单的处理
   		if(arg.equals("关于"))
   			JOptionPane.showMessageDialog(this,"五子棋V2.0 CopyRight 2004(c) 小组 版权所有\n作者:小组成员\n邮箱:absd13885@163.com","关于",JOptionPane.INFORMATION_MESSAGE);
   		if(arg.equals("退出"))
   		    	System.exit(0);
          }
   		
   		//返回当前连接套接字的输入流
   		public InputStream getin(){
   			return in;
   		}
   		
   		//返回当前连接套接字的输出流
   		public 	OutputStream getout(){
   			return out;
   		}
   		
   		//返回当前的连接套接字
   		
   		public Socket getsocket(){
   			return clientsocket;
   		}
   		
   		public void setEnableMenuItems(){
   			serverMenuItem.setEnabled(true);
   			connectMenuItem.setEnabled(true);
   			disconnectMenuItem.setEnabled(false);
   			boardMenuItem.setEnabled(true);
   			localchessMenu.setEnabled(true);
   			
   		}
}		 		
   		
   		
  
    
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -