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

📄 wuzhiapplet.java

📁 1.用applet实现一个简单的网络五子棋
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
///05.07.06实现两人下玩一局后点”开始“来新一局,且上一局后一个下的先下
public class WuzhiApplet extends Applet // implements MouseListener
{
	Button button1=new Button("连接");
	Button button2=new Button("离开");
	Button button3=new Button("一号房间");
	Button button4=new Button("二号房间");
	Button button5=new Button("三号房间");
	Button button6=new Button("四号房间");
	TextField name_txt=new TextField(20);
	Panel  panel1=new Panel();
	Panel  panel2=new Panel();
	Panel  panel3=new Panel();
	Panel  panel4=new Panel();
	Panel  panel5=new Panel();
	Label  label_nam=new Label("用户名");
	Label  label_nam1=new Label("在线用户");
	  wuWindow wuwindow=new wuWindow();
	java.awt.List   list_user=new java.awt.List(10);
	  /*以下定义数据流和网络变量*/
  Socket soc=null;                   //定义连接套接字
  static PrintStream ps=null;                 //定义打印流 
 Listen listen=null;                 //定义一个客户端线程
 String userinfo;
 static String userinfo1;

	public void init()
	{
		//resize(800,600);
		//setSize(800,600);
		this.setLayout(new BorderLayout());
		this.add(panel1,BorderLayout.NORTH);
		this.add(panel2,BorderLayout.WEST);
		this.add(panel3,BorderLayout.CENTER);
		this.add(panel4,BorderLayout.EAST);
		panel1.setBackground(Color.yellow);
		panel2.setBackground(Color.red);
		panel3.setBackground(Color.green);
		panel4.setBackground(Color.blue);
		panel1.setLayout(new FlowLayout());
		panel1.add(label_nam);panel1.add(name_txt);
		panel1.add(button1);panel1.add(button2);
		panel2.setLayout(new BorderLayout());
		panel2.add(label_nam1,"North");panel2.add(list_user,"Center");
		panel3.setLayout(new GridLayout(2,2));
		panel3.add(button3);panel3.add(button4);
		panel3.add(button5);panel3.add(button6);

	}
	public boolean action(Event event,Object object)
	{
			String str=wuwindow.getTitle();
		if(event.target instanceof Button)
		{
			String label=(String)object;
						     //String label=(String) obj;
		 if(label.equals("连接")&&!(name_txt.getText()).equals(null))                //如果点击连接后
	
				{
		     if(soc==null)
			 	{
			     try
					{
					 soc=new Socket(InetAddress.getLocalHost(),4983);     
					 //使用端口4983实例化一个本地套接字
					 System.out.println(soc);                             
					 //在控制台打印实例化的结果
					 ps=new PrintStream(soc.getOutputStream());           
					 //将ps指向soc的输出流
					 StringBuffer info=new StringBuffer("INFO: ");        
					 //定义一个字符缓冲存储发送信息
					                                                      
					//其中INFO为关键字让服务器识别为连接信息
					//并将name和ip用":"分开,在服务器端将用一个
			//StringTokenizer类来读取数据
					  userinfo=name_txt.getText()+":"+InetAddress.getLocalHost().toString()+
						 ":"+soc.getLocalPort();
   userinfo1=name_txt.getText()+","+InetAddress.getLocalHost().toString()+
						 ","+soc.getLocalPort();

					 ps.println(info.append(userinfo));
					 ps.flush();
			 listen=new Listen(this,name_txt.getText(),soc,wuwindow);    
					 //将客户端线程实例化  
					 listen.start();                                    
					 //启动线程
				 }
				 catch(IOException e)
					{
				     System.out.println("Error你好:"+e);
					 disconnect();
				 }
			 }   //end of if
		 }//end of if
	   else if(label.equals("离开"))                               
		   //如果点击断开连接按钮则运行disconnect()
			{
	         disconnect();
	   }

			if(soc!=null&&label.equals("一号房间")&&str.equals("五子棋")
				&&wuwindow.white_user.getText().equals("--------"))
			{  
				 ps.println("ROOM:"+userinfo1+":"+"ONE");
				 ps.flush();///发送我进的房间号//////not deall!!!!
				str=str.concat("----一号房间");
				wuwindow.setTitle(str);
				//wuwindow.black_user.setText(userinfo);
				//wuwindow.setVisible(true);
				
			}//else{System.out.println("It is palying !!!!!");}
			if(soc!=null&&label.equals("二号房间")&&str.equals("五子棋"))
			{  
				str=str.concat("----二号房间");
				wuwindow.setTitle(str);
				wuwindow.setVisible(true);

				//wuwindow.setVisible(true);
			}
			if(soc!=null&&label.equals("三号房间")&&str.equals("五子棋"))
			{
				str=str.concat("----三号房间");
				wuwindow.setTitle(str);
				wuwindow.setVisible(true);
			}
			if(soc!=null&&label.equals("四号房间")&&str.equals("五子棋"))
			{  
				str=str.concat("----四号房间");
				wuwindow.setTitle(str);
				wuwindow.setVisible(true);

				//wuwindow.setVisible(true);
			}

		}
		return (true);
	}/////action() over!!!!
	
  public void disconnect()                                         
	  //客户端点击断开连接要运行的方法
	{
     if(soc!=null)
		{
			ps.println("QUIT");                                    
			//用打印流发送QUIT信息通知服务器断开此次通信
			ps.flush();		     
	 }// end of if
  }////disconnect()  over!!!!


	public static void main(String[] args) 
	{
		System.out.println("Hello World!");
	}
}////WuzhiApplet over!!!

//////////////////////////////////////////////////

  class Listen extends Thread       
	  //客户端线程类用来监听服务器传来的信息
{
 String name=null;                   
 //用来存储客户端连接后的name信息
 DataInputStream dis=null;          
 //用来实现客户端接受服务器数据的输入流
 static PrintStream ps=null;               
 //用来实现从客户端发送数据到服务器的打印流
 Socket socket=null;                
 //用来存储客户端的socket信息
 WuzhiApplet parent=null;             
 //用来存储当前运行的chatApplet实例
 wuWindow window=null;
 //用来存储棋盘实例
	public static void send(StringBuffer msg)                   
		//实现想客户端发送信息的方法
	  { 
	  ps.println(msg);                                  
	   //用打印流发送信息
	  ps.flush();
	}


 public Listen(WuzhiApplet p,String n,Socket s,wuWindow wuwindow)   //Listen类的构造器
	{
     //接受参数
	 parent=p;
	 name=n;
	 socket=s;
	 window=wuwindow;
	

	 try
		{
		 //实例化两个数据流
	     dis=new DataInputStream(s.getInputStream());
		 ps=new PrintStream(s.getOutputStream());

	 }
	 catch(IOException e)
		{
	     System.out.println("Error:"+e);
		 parent.disconnect();
	   }
    }   //end of Listen constractor
  
 public void run()                               //线程运行方法
	{
      String msg=null;
	  while(true)
		{
	     try{msg=dis.readLine();}                
		 //读取从服务器传来的信息
		 catch(IOException e)
			{////如果服务器突然断开将捕获一个异常
		     System.out.println("Error QUIT:"+e);
			 parent.disconnect();
		 }
		 if (msg==null)                           
			 //如果从服务器传来的信息为空则断开此次连接
		 {
		   parent.listen=null;              
		   parent.soc=null;
		   parent.list_user.clear();
		   return;
		 }
		 StringTokenizer st=new StringTokenizer(msg,":");   
		 //用StringTokenizer类来实现读取分段字符
		 String keyword=st.nextToken();                     
		 //读取信息头即关键字用来识别是何种信息

		 if(keyword.equals("PEOPLE"))                      
			 //如果是PEOPLE则是服务器发来的客户连接信息
		                                                   
			//主要用来刷新客户端的用户列表
			{
		     parent.list_user.clear();
			 while(st.hasMoreTokens())                     
				 //遍历st取得目前所连接的客户
				{
			     String str=st.nextToken();
				 parent.list_user.addItem(str);
			 }
		 }
		 else if(keyword.equals("ROOM"))
			{////处理房间  not deal!!!
		      String a,b,c;
			//	while(st.hasMoreTokens())
		//	{ 
			a=st.nextToken();b=st.nextToken();
				//String nam=st.nextToken();
				//  String s=st.nextToken();
				if(b.equals("ONE"))
					{window.black_user.setText(a);
				      if(WuzhiApplet.userinfo1.equals(a))
						   ///如果我是黑方,这要改善当白方收到时置1
						{window.f=1;window.side=1;window.setVisible(true);}
					}else if(b.equals("TWO"))
				{///
						window.white_user.setText(a);//window.f=2;
						window.setVisible(true);
					}
					else{//window.white_user.setText("00000000");
						System.out.println("Tt is playing !!");}
		//	}
		}
		else if(keyword.equals("QIZI"))
			{
				String a,b,c,d;Point point=new Point();int f,x,y;
				a=st.nextToken();b=st.nextToken();
				c=st.nextToken();d=st.nextToken();
				//window.socket=a;
				f=Integer.parseInt(b);
				x=Integer.parseInt(c);y=Integer.parseInt(d);
//				point.x=x*30+50;
//				point.y=y*30+50;
				window.huaYuan(x,y,f);////处理对方的棋子
				if(window.f!=3&&window.f!=4)
				{//如果没有哪一方胜出
				if(f==1){window.f=2;}////改变本地的f的值可以下子
				if(f==2){window.f=1;}
				}
		}
		 else if(keyword.equals("QUIT"))                
			 //如果关键字是QUIT则是服务器关闭的信息
		                                                
			//用来切断此次连接
			{
		     System.out.println("Quit");

⌨️ 快捷键说明

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