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

📄 gobang_server.java

📁 可联机对战的五子棋java源代码 包含服务器文件和客户端文件 详细的技术文档和用户手册
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************
*Gobang_Server.java                                                        ×
*开发者:周卿                                                              ×
*简介:五子棋服务器端文件                                                  ×
***************************************************************************/

import java.awt.*;
import javax.swing.*;
import java.net.URL;
import java.net.Socket;
import java.net.ServerSocket;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

class Z_Frame_Server extends JFrame						//服务器端
{		
	private int i,j;
	private Container c;
	private static int stoneColor[][];  				//储存每个位置棋子的颜色(0为没有棋子,1为黑,2为白)
	private static int xTemp,yTemp;							//用于传输临时坐标
	private static int steps_B = 0;							//黑棋走的步数
  private static int steps_W = 0;							//白棋走的步数
	private static boolean connected;						//是否连接成功
	private static Socket s;                    
	private static ServerSocket server;         
	private ImageIcon[] background;             //存放一组背景图片
	private AudioClip[] music;                  //存放一组背景音乐
	private AudioClip musicCurrent;             //当前播放的音乐
	private JLabel back_ground;                 //用于设置背景图片
	private JLabel stone[][];                   //用于放置棋子
	private JLabel face1;
	private JLabel face2;
	private JLabel Name1;
	private JLabel Name2;
	private JTextField z_enter;                 //用于输入聊天信息
	private JTextArea z_display;                //显示聊天信息
	private JScrollPane z_Display;
	private ObjectInputStream z_input;
	private ObjectOutputStream z_output;

	
	public Z_Frame_Server()											//构造方法
	{
		super("五子棋Online服务器端");
		 
		try																				
		{ 
			server = new ServerSocket(5000);
		}
		catch(Exception e)
		{
			System.out.println("发生异常"+e);
			e.printStackTrace();
		}
		
		stone = new JLabel[15][15];								//初始化
		stoneColor = new int[15][15];
		for(i=0;i<15;i++)
		  for(j=0;j<15;j++)
		  {
		  	stoneColor[i][j] = 0;
		  	stone[i][j] = new JLabel();
		  }
		
		background = new ImageIcon[5];            //读取背景图片
		background[0] = new ImageIcon("Background/默认.jpg");
		background[1] = new ImageIcon("Background/朗月孤星.jpg");
		background[2] = new ImageIcon("Background/飘零如叶.jpg");
		background[3] = new ImageIcon("Background/生命之蓝.jpg");
		background[4] = new ImageIcon("Background/雪月同辉.jpg");
						
		try                                       //读取背景音乐
		{
				music = new AudioClip[11];
				music[0] = Applet.newAudioClip(new URL("file:Music/东邪西毒.mid"));
				music[1] = Applet.newAudioClip(new URL("file:Music/华山论剑.mid"));
				music[2] = Applet.newAudioClip(new URL("file:Music/美丽的神话.mid"));
				music[3] = Applet.newAudioClip(new URL("file:Music/千千阙歌.mid"));
				music[4] = Applet.newAudioClip(new URL("file:Music/水晶.mid"));
				music[5] = Applet.newAudioClip(new URL("file:Music/铁血丹心.mid"));
				music[6] = Applet.newAudioClip(new URL("file:Music/万水千山总是情.mid"));
				music[7] = Applet.newAudioClip(new URL("file:Music/我想有个家.mid"));
				music[8] = Applet.newAudioClip(new URL("file:Music/我只在乎你.mid"));
				music[9] = Applet.newAudioClip(new URL("file:Music/小城故事.mid"));
		}
		catch(Exception e)
		{
			System.err.println("发生异常:" + e);
      e.printStackTrace( );
		}
				
		c = getContentPane();   
  	c.setLayout(null);
  	
  	z_enter = new JTextField();								//初始化聊天信息输入框
  	z_enter.setEnabled(false);
  	z_enter.addActionListener(new ActionListener()	//添加监听器
  		{
  			public void actionPerformed(ActionEvent event1)
  			{
  				try
	  			{
	  				String s = event1.getActionCommand();
	  				z_output.writeObject(s);
	  				z_output.flush();
						z_displayAppend("服务器端:"+s);
	  				z_enter.setText("");
	  			}
	  			catch (Exception e)
	  			{
	  				System.err.println("发生异常:"+e);
	  				e.printStackTrace();
	  			}
	  		}
	  	});
	  
	  c.add(z_enter);
	  z_enter.setBounds(650,580,300,30);
	  
	  z_display = new JTextArea();
	  z_Display = new JScrollPane(z_display);
	  c.add(z_Display);
	  z_Display.setBounds(650,260,300,320);
	   	
  	face1 = new JLabel(new ImageIcon("Face/1.png"));   //读取客户端和服务器端的头像   
  	face2 = new JLabel(new ImageIcon("Face/2.png"));
		Name1 = new JLabel("服务器端:黑棋");
		Name2 = new JLabel("客户端:白棋");
		
			
		JLabel checkerboard = new JLabel(new ImageIcon("Picture/checkerboard.jpg"));	//设置棋盘
		
		for(i=0;i<15;i++)																					//初始化棋子位置
			for(j=0;j<15;j++)
			{
				c.add(stone[i][j]);
				stone[i][j].setBounds(74-16+i*37,74-16+j*37,32,32);	
			}
			
		c.add(checkerboard);                                      //添加组件
		c.add(face1);
		c.add(face2);
		c.add(Name1);
		c.add(Name2);
		checkerboard.setBounds(50,50,564,564);
		face1.setBounds(650,50,128,128);
		face2.setBounds(825,50,128,128);
		Name1.setBounds(670,180,128,30);
		Name2.setBounds(850,180,128,30);
		face1.setVisible(true);
		face2.setVisible(false);
		Name1.setVisible(true);
		Name2.setVisible(false);
			
		((JPanel)c).setOpaque(false);                              //设置背景图层
		back_ground = new JLabel(background[0]);                                               
		getLayeredPane().add(back_ground, new Integer(Integer.MIN_VALUE)); 
		back_ground.setBounds(0, 0,1024,768);
		
		musicCurrent = music[0];                                   //设置当前播放音乐和音乐模式
		music[0].loop();
		
		JMenuBar mBar = new JMenuBar( );                           //设置菜单项
		setJMenuBar(mBar); 
		JMenu[] m = {new JMenu("选项"),new JMenu("背景图片"),new JMenu("背景音乐")};
		JMenuItem[][] mI = 
		{
			{new JMenuItem("作者信息"),new JMenuItem("退出")},
			{new JMenuItem("默认"),new JMenuItem("朗月孤星"),
			 new JMenuItem("飘零如叶"),new JMenuItem("生命之蓝"),
			 new JMenuItem("雪月同辉")},
			{new JMenuItem("东邪西毒"),new JMenuItem("华山论剑"),
			 new JMenuItem("美丽的神话"),new JMenuItem("千千阙歌"),
			 new JMenuItem("水晶"),new JMenuItem("铁血丹心"),
			 new JMenuItem("万水千山总是情"),new JMenuItem("我想有个家"),
			new JMenuItem("我只在乎你"),new JMenuItem("小城故事"),new JMenuItem("静音")}
		};
				
		for(i=0;i<m.length;i++)
		{
			mBar.add(m[i]);
			for(j=0;j<mI[i].length;j++)
			{
				m[i].add(mI[i][j]);
			}
		}
		
		mI[0][0].addActionListener(new ActionListener()              //为各个菜单项添加监听器
	    {
	    	public void actionPerformed(ActionEvent e)
	    	{
	    		JMenuItem mItem = (JMenuItem)e.getSource();
	    		JOptionPane.showMessageDialog(Z_Frame_Server.this,"作者:周卿\n学号:2007013221",
	    																	"作者信息",JOptionPane.INFORMATION_MESSAGE);
	    	}
	    });
				
	  mI[0][1].addActionListener(new ActionListener()
	    {
	    	public void actionPerformed(ActionEvent e)
	    	{
	    		JMenuItem mItem = (JMenuItem)e.getSource();
	    		System.exit(0);
	    	}
	    });
		
		mI[1][0].addActionListener(new ActionListener()                            //设置背景图片
			{
			  public void actionPerformed(ActionEvent e)
			  {
			  	JMenuItem mItem = (JMenuItem)e.getSource();
					back_ground.setIcon(background[0]);
			  }
			});
		mI[1][1].addActionListener(new ActionListener()
			{
			  public void actionPerformed(ActionEvent e)
			  {
			  	JMenuItem mItem = (JMenuItem)e.getSource();
					back_ground.setIcon(background[1]);
			  }
			});
		mI[1][2].addActionListener(new ActionListener()
			{
			  public void actionPerformed(ActionEvent e)
			  {
			  	JMenuItem mItem = (JMenuItem)e.getSource();
					back_ground.setIcon(background[2]);
			  }
			});
		mI[1][3].addActionListener(new ActionListener()
			{
			  public void actionPerformed(ActionEvent e)
			  {
			  	JMenuItem mItem = (JMenuItem)e.getSource();
					back_ground.setIcon(background[3]);
			  }
			});
		mI[1][4].addActionListener(new ActionListener()
			{
			  public void actionPerformed(ActionEvent e)
			  {
			  	JMenuItem mItem = (JMenuItem)e.getSource();
					back_ground.setIcon(background[4]);
			  }
			});		
		
		mI[2][0].addActionListener(new ActionListener()           //设置音乐
			{
				public void actionPerformed(ActionEvent e)
				{
					JMenuItem mItem = (JMenuItem)e.getSource();
					musicCurrent.stop();
					musicCurrent = music[0];
					musicCurrent.loop();
				}
			});	
		mI[2][1].addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					JMenuItem mItem = (JMenuItem)e.getSource();
					musicCurrent.stop();
					musicCurrent = music[1];
					musicCurrent.loop();
				}
			});	
		mI[2][3].addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					JMenuItem mItem = (JMenuItem)e.getSource();
					musicCurrent.stop();
					musicCurrent = music[3];
					musicCurrent.loop();

⌨️ 快捷键说明

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