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

📄 shootplaneclient.java

📁 我的一个java大作业 是一个可以联机的打飞机的游戏 附带聊天功能
💻 JAVA
字号:
/*
 * @(#)Shootplane.java 1.0 06/11/05
 *
 * You can modify the template of this file in the
 * directory ..\JCreator\Templates\Template_1\Project_Name.java
 *
 * You can also create your own project template by making a new
 * folder in the directory ..\JCreator\Template\. Use the other
 * templates as examples.
 *
 */


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
import javax.swing.border.*;
import javax.swing.ImageIcon.*;
import java.net.*;                                              
import java.io.*;

public class Shootplaneclient extends Frame {
	public int flag=1;
	 Socket svr=null;
	 PrintWriter out;
	 BufferedReader in;
	 int hurt=0;
	 PrintWriter outchat;
	 BufferedReader inchat;
     Socket chat;
     Socket   chatSocket=null;
	 BufferedReader  inchatSocket;
	 PrintWriter  outchatSocket;
     int shoot=4;
     int planes=0;
     int count=0;
    
	Panel pane1 = new Panel();
	Panel pane2 = new Panel();
	Panel pane3 = new Panel();
	Panel pane4 = new Panel();	
	JPanel pane21 = new JPanel();
	JPanel pane22 = new JPanel();
	JPanel pane23 = new JPanel();
	ImageIcon icon1= new ImageIcon ("pic1.gif");
	ImageIcon icon2= new ImageIcon ("pic2.gif");
	ImageIcon icon3= new ImageIcon ("pic3.gif");
	ImageIcon icon4= new ImageIcon ("pic4.gif");
	JButton icons=new JButton();
	JTextArea textarea = new JTextArea (19,18);
	Button B[]=new Button[5];
	JTextField msg1=new JTextField(20);
	Button D1=new Button("发送");
	 Button D2=new Button("新一局");
	Button D3=new Button("连接");
	JTextField ip=new JTextField("localhost");
	JPanel pane41=new JPanel();
	JPanel pane42=new JPanel();
	 mypanel[] p=new mypanel[121];
	 mypanel[] q=new mypanel[121]; 
 
	
	public Shootplaneclient() {
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				dispose();
				System.exit(0);
			}
		});
	setLayout(new GridLayout(2,2,20,20));
		
	pane1.setLayout(new GridLayout(11,11,0,0));
	pane3.setLayout(new GridLayout(11,11,0,0));
	pane2.setLayout(new GridLayout(1,2,10,10));
	pane4.setLayout(new GridLayout(2,1,1,1));
	
	pane21.setLayout(new GridLayout(2,1,10,10));
	textarea.setEditable(false);
	textarea.setLineWrap(true);
	textarea.setText("填入对方主机并连接!");
	icons.setIcon(icon1);
	pane21.add(icons);
	pane22.add (textarea);
	String s[]={"UP","DOWN","LEFT","RIGHT","RESET"};
	int i ;
    
	for (i=0;i<5;i++)
	 {B[i]=new Button(s[i]);
	 pane23.add (B[i]);
	 }
	 pane21.add(pane23);
	 pane2.add(pane21);
	 pane2.add(pane22);
	 
	 
	 pane41.add(msg1);
	 pane41.add(D1);
	 pane42.add(D2);
	 pane42.add(ip);
	 pane42.add(D3);
	 pane4.add(pane41);
	 pane4.add(pane42);
	 D1.addMouseListener(new MouseAdapter(){
	 	public void mouseClicked(MouseEvent e){
	 		String chatword=msg1.getText();
	 		textarea.append("\n蓝方:"+chatword);
	 	try{	outchat.println(chatword);
	 		outchatSocket.println("蓝方:"+chatword);
	 	    outchatSocket.flush();
	 	   }catch(Exception a){
	         		textarea.append("\n连接出错!!");}
	 		}
	 	}
      )	;
      
       D2.addMouseListener(new MouseAdapter(){
     	public void mouseClicked(MouseEvent e){
     		if (hurt!=3&&count!=3) return;
     		try{
     			out.println(500);
     			out.flush();
     		}catch(Exception a){
	         		textarea.append("\n连接出错!!");}
     	   
	       for (int i=0;i<121;i++)
	       {
	       	p[i].ip=0;
	       	p[i].setBackground(Color.lightGray);
	       	q[i].setBackground(Color.lightGray);
	       	hurt=0;
         	shoot=4;
	        planes=0;
	        count=0;
	       	}  		
     	}
     	}); 
     	
     	
     D3.addMouseListener(new MouseAdapter(){
	 	public void mouseClicked(MouseEvent e){
	 		if (planes!=3)	{
	 		textarea.append("\n请设置飞机!!");
	 		return;}
	 		if (!(svr==null)) return;
         	if (!(chat==null)) return;
	 			
	 			try{
	 			  svr=new Socket(ip.getText(),5000);
	 			  chat=new Socket(ip.getText(),6000);
	 				if (svr!=null)
	 				{
	 					textarea.append("\n与"+svr.getInetAddress()+"连接成功!");
	 				    in = new BufferedReader(new InputStreamReader(
	 					svr.getInputStream()));	 				
	 				    out =new PrintWriter(svr.getOutputStream());	 			
	 					out.println("ready");
	 					out.flush();
	 					inchat = new BufferedReader(new InputStreamReader(chat.getInputStream()));	
	 				    outchat =new PrintWriter(chat.getOutputStream());
	 					ShootClient shot=new ShootClient();
	                 	Thread client=new Thread(shot);
	                 	client.start();	
	                 	chatClient newchat=new chatClient();
	                 	Thread newchatthread=new Thread(newchat);
	                 	newchatthread.start();	
	 					}
	 				
	 				}catch(Exception a){
	         		textarea.append("\n连接出错!!");}
	 
	 		}
	 	}
	 		);
	 		
	 		
	 		
	 		 
	 for (i=0;i<121;i++)
	 {
	 mypanel u=new mypanel();
	 p[i]=u; 
	 p[i].ic=i;
	 mypanel v=new mypanel();
	 q[i]=v;
	 q[i].ic=i; 
	 pane1.add(p[i]);
	 pane3.add(q[i]);
	 	}
	 for (i=12;i<121;i++)
	 {
	 	q[i].addMouseListener(new MouseAdapter(){
	 		public void mouseClicked(MouseEvent e){
	 		if (shoot==4)	return ;
	 		if (hurt>=3||count==3) return;
	 		mypanel me=new mypanel();
			me=(mypanel)e.getSource();
			int i=me.ic;
			try{
			outchat.println(Integer.toString(i));
			outchat.flush();
		
			int j=Integer.parseInt(inchat.readLine());
			me.ip=j;
			if (j==2){
				count++;
				me.setBackground(Color.red);
				if (count==3)
				{textarea.append("\n您赢了!!");}
				     }
			else if (j==1){
				me.setBackground(Color.blue);}
			else 
			    {me.setBackground(Color.darkGray);}	
			 shoot--;  
	 					
	 			}catch(Exception a){
	         		textarea.append("\n发送出错!!");}
	 		}
	 		});
	 	}	
	 String a;
	 
	for (i=1;i<11;i++)
	{
	 a=Integer.toString(i-1);	
	p[i].add(new Label(a));
	p[i*11].add(new Label(a));
	q[i].add(new Label(a));
	q[i*11].add(new Label(a));}
	add(pane1);
	add(pane2);
	add(pane3);
	add(pane4);
	
	B[0].addMouseListener(new MouseAdapter(){
	     public void mouseClicked(MouseEvent e){
	        flag=1;
	       icons.setIcon(icon1); 
	     }
	});
	B[1].addMouseListener(new MouseAdapter(){
	     public void mouseClicked(MouseEvent e){
	        flag=2;
	       icons.setIcon(icon2); 
	     }
	});
	B[2].addMouseListener(new MouseAdapter(){
	     public void mouseClicked(MouseEvent e){
	        flag=3;
	       icons.setIcon(icon3); 
	     }
	});
	B[3].addMouseListener(new MouseAdapter(){
	     public void mouseClicked(MouseEvent e){
	        flag=4;
	       icons.setIcon(icon4); 
	     }
	});
	B[4].addMouseListener(new MouseAdapter(){
	     public void mouseClicked(MouseEvent e){
	     		if (!(svr==null)) {
	     		textarea.append("\n正在游戏中!!!");
	     		return;}
	     	int i;
	        for (i=0;i<121;i++)
	         {p[i].setBackground(Color.lightGray);
	         p[i].ip=0;
	         } 
	         planes=0;
	     }
	});
	 for (i=0;i<121;i++)
	  {
	  p[i].addMouseListener(new MouseAdapter(){
	      
		public void mouseClicked(MouseEvent e){
			if (planes==3){
				textarea.append("\n只允许放置三架飞机!");
				return;}
			mypanel me=new mypanel();
			me=(mypanel)e.getSource();
			int i=me.ic;
		    int r=i/11+1;
		    int l=i-(r-1)*11+1;
		    switch (flag)
		    { 
		      case 1:
		        {
		          if (r <=9&&r>=3&&l<=9&&l>=4&&p[i].ip==0&&p[i-11].ip==0&&p[i-2].ip==0&&
		          p[i-1].ip==0&&p[i+1].ip==0&&p[i+2].ip==0&&p[i+21].ip==0&&p[i+11].ip==0&&
		          p[i+23].ip==0&&p[i+22].ip==0){
		          	p[i].ip=1;p[i-11].ip=2;
		          	p[i-2].ip=1;p[i-1].ip=1;
		          	p[i+1].ip=1;p[i+2].ip=1;
		          	p[i+22].ip=1;p[i+11].ip=1;
		          	p[i+23].ip=1;p[i+21].ip=1;
		          	p[i].setBackground(Color.blue);
		          	p[i-11].setBackground(Color.blue);
		          	p[i-2].setBackground(Color.blue);
		          	p[i-1].setBackground(Color.blue);
		          	p[i+1].setBackground(Color.blue);
		          	p[i+2].setBackground(Color.blue);
		          	p[i+21].setBackground(Color.blue);
		          	p[i+11].setBackground(Color.blue);
		          	p[i+22].setBackground(Color.blue);
		          	p[i+23].setBackground(Color.blue);
		          	planes++;
		        	}
		        	break;
		    	}
		    	
		    case 2:
		        {
		          if (r <=9&&r>=4&&l<=9&&l>=4&&p[i].ip==0&&p[i-11].ip==0&&p[i-2].ip==0&&
		          p[i-1].ip==0&&p[i+1].ip==0&&p[i+2].ip==0&&p[i-22].ip==0&&p[i+11].ip==0&&
		          p[i-23].ip==0&&p[i-21].ip==0){
		          	p[i].ip=1;p[i-11].ip=1;
		          	p[i-2].ip=1;p[i-1].ip=1;
		          	p[i+1].ip=1;p[i+2].ip=1;
		          	p[i-19].ip=1;p[i+11].ip=2;
		          	p[i-21].ip=1;p[i-20].ip=1;
		          	p[i].setBackground(Color.blue);
		          	p[i-11].setBackground(Color.blue);
		          	p[i-2].setBackground(Color.blue);
		          	p[i-1].setBackground(Color.blue);
		          	p[i+1].setBackground(Color.blue);
		          	p[i+2].setBackground(Color.blue);
		          	p[i-21].setBackground(Color.blue);
		          	p[i+11].setBackground(Color.blue);
		          	p[i-23].setBackground(Color.blue);
		          	p[i-22].setBackground(Color.blue);
		          	planes++;
		        	}
		    	
		    	break;
		    }
		    case 3:
		        {
		          if (r <=9&&r>=4&&l<=9&&l>=3&&p[i].ip==0&&p[i-1].ip==0&&p[i+11].ip==0&&
		          p[i+22].ip==0&&p[i-11].ip==0&&p[i-22].ip==0&&p[i+1].ip==0&&p[i+2].ip==0&&
		          p[i-9].ip==0&&p[i+13].ip==0){
		          	p[i].ip=1;p[i-11].ip=1;
		          	p[i+22].ip=1;p[i-1].ip=2;
		          	p[i+1].ip=1;p[i+2].ip=1;
		          	p[i-9].ip=1;p[i+11].ip=1;
		          	p[i+13].ip=1;p[i-22].ip=1;
		          	p[i].setBackground(Color.blue);
		          	p[i-1].setBackground(Color.blue);
		          	p[i+11].setBackground(Color.blue);
		          	p[i+22].setBackground(Color.blue);
		          	p[i-11].setBackground(Color.blue);
		          	p[i-22].setBackground(Color.blue);
		          	p[i+1].setBackground(Color.blue);
		          	p[i+2].setBackground(Color.blue);
		          	p[i-9].setBackground(Color.blue);
		          	p[i+13].setBackground(Color.blue);
		          	planes++;
		        	}
		    	
		    	break;
		    }
		    case 4:
		        {
		          if (r <=9&&r>=4&&l<=10&&l>=4&&p[i].ip==0&&p[i-11].ip==0&&p[i-2].ip==0&&
		          p[i-22].ip==0&&p[i+1].ip==0&&p[i-1].ip==0&&p[i+9].ip==0&&p[i+11].ip==0&&
		          p[i-13].ip==0&&p[i+22].ip==0){
		          	p[i].ip=1;p[i-11].ip=1;
		          	p[i-2].ip=1;p[i-22].ip=1;
		          	p[i+9].ip=1;p[i+11].ip=1;
		          	p[i-13].ip=1;p[i+22].ip=1;
		          	p[i+1].ip=2;p[i-1].ip=1;
		          	p[i].setBackground(Color.blue);
		          	p[i-11].setBackground(Color.blue);
		          	p[i-2].setBackground(Color.blue);
		          	p[i-22].setBackground(Color.blue);
		          	p[i+1].setBackground(Color.blue);
		          	p[i-1].setBackground(Color.blue);
		          	p[i+9].setBackground(Color.blue);
		          	p[i+11].setBackground(Color.blue);
		          	p[i-13].setBackground(Color.blue);
		          	p[i+22].setBackground(Color.blue);
		          	planes++;
		        	}
		    	
		    	break;	
		    	}		
		}
	  }
	});
	  	}
	}
	
	class mypanel extends JButton{
		public int ip;
		public int ic;
		mypanel(){
		  setBorder(new LineBorder(Color.black,1));
		  setBackground(Color.lightGray);
		}
	}
	
	
	class ShootClient implements Runnable{
    public ShootClient() {
  
      }
   
    public void run(){
        
	 			
	 			try{
	 				    String str=in.readLine();
	 					if (str.equals("ready")){
	 						textarea.append("\n游戏开始!");}
	 						
	 					while(true)	{
	 					int Bullet=Integer.parseInt(in.readLine());
	 					if (Bullet==500) 
	 					{
	 						textarea.append("\n新一局开始请设置飞机!");
	 						 for (int i=0;i<121;i++)
						       {
						       	p[i].ip=0;
						       	p[i].setBackground(Color.lightGray);
						       	q[i].setBackground(Color.lightGray);
						       	hurt=0;
					         	shoot=4;
						        planes=0;
						        count=0;
						       	}  	
						       	continue;	
	 						}
	 					out.println(Integer.toString(p[Bullet].ip));
	 					out.flush();
	 					shoot++;
	 					if (p[Bullet].ip==2){hurt++;}
	 					if (hurt>=3){
	 						textarea.append("\n您输了!!");
	 						        }
	 					}	 			        	 							 							 						 				
	         	}catch(Exception a){
	         		textarea.append("\n连接出错!!");}
	 	
     }
  }

class chatClient implements Runnable{
    public chatClient() {
    	
    }
 
    public void run(){
    	
    	if (!(chatSocket==null)) return;
       
	 			try{
	 				chatSocket=new Socket(ip.getText(),5500);
	 				inchatSocket = new BufferedReader(new InputStreamReader(chatSocket.getInputStream()));
	 				
	 				    outchatSocket =new PrintWriter(chatSocket.getOutputStream());	 					 				
	 				while(true){	 				
	 					textarea.append("\n"+inchatSocket.readLine());
	 					}
	 				
	         	}catch(Exception a){
	         		textarea.append("\n连接出错!!");}
	 		
     }
  }

	public static void main(String args[]) {
		System.out.println("Starting Shootplaneclient...");
		Shootplaneclient mainFrame = new Shootplaneclient();
		mainFrame.setSize(900, 700);
		mainFrame.setTitle("Shootplaneclient");
		mainFrame.setVisible(true);
	}
}

⌨️ 快捷键说明

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