网络编程第三步(搞一个low版的网络画图板)

  • 实现功能,传入颜色,能画多种图形
  • 其实,通信的本质基本就是
Socket st = new Socket("127.0.0.1",4455);

ServerSocket sst = new ServerSocket(4455);
sst.accept();
  • 只要确保以上代码,那么客户端和服务器端基本就是可以连通的,剩下的基本就是传数据的问题,怎么传,怎么接收的问题。比如本次我是要画一个直线和一个矩形,其实本质就是怎么将直线和矩形的坐标传到客户端而已。
  • 客户端代码
  • 首先,我们要有一个图形界面,其上面至少有三个按钮,直线,矩形,和蓝色背景的按钮,点击按钮之后是要画出对应图形的,所以要给按钮加动作监听器,给界面加鼠标监听器,
public void showUI() {
		
		//界面
		JFrame jf = new JFrame();
		jf.setSize(600,800);
		jf.setTitle("Cli Meetting by gzh4869--0.1");
		jf.setLocationRelativeTo(null);
		jf.setDefaultCloseOperation(3);
		
		FlowLayout fl = new FlowLayout();
		jf.setLayout(fl);
		
		JButton jub = new JButton("直线");
		jf.add(jub);
		
		JButton jub1 = new JButton("矩形");
		jf.add(jub1);
		
		//颜色按钮
		Color color = Color.BLUE;
		JButton jub2 = new JButton();
		jub2.setBackground(color);
		jub2.setPreferredSize(new Dimension(30, 30));
		jf.add(jub2);

		
		jf.setVisible(true);
		
		MyClient mc = new MyClient();
		
		if(mc.conn2Server()) {
			//画笔
			Graphics g = jf.getGraphics();
			
			//创建监听对象
			MousePic mp = new MousePic(g,mc);
			
			
			jub.addActionListener(mp);
			jub1.addActionListener(mp);
			jub2.addActionListener(mp);
			
			//给界面添加监听
			jf.addMouseListener(mp);
		}		
	}
	
	public static void main(String[] args) {
		DrawPic dp = new DrawPic();
		dp.showUI();
	}
}
  • 监听代码
public class MousePic implements MouseListener, ActionListener {

	String name;
	MyClient mc;
	Graphics g;
	int x1, y1, x2, y2;

	public MousePic(Graphics g, MyClient mc) {
		this.g = g;
		this.mc = mc;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		//就是要获的无文本按钮的背景颜色,将其设置给画笔
		if ("".equals(e.getActionCommand())) {
			// 获取当前事件源对象(按钮)
			JButton jbu = (JButton) e.getSource();
			// 获取按钮的背景色,设置给画笔
			Color color = jbu.getBackground();
			g.setColor(color);
			try {
			//如果点了这个按钮,就发送一个字节4,证明有颜色传过来了
				mc.dos.writeByte(4);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} else {
			// 获取按钮上的字符串
			name = e.getActionCommand();
		}
		//System.out.println("name = " + name);
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		//获得坐标
		x1 = e.getX();
		y1 = e.getY();

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		//获得坐标
		try {
			x2 = e.getX();
			y2 = e.getY();
			if ("直线".equals(name)) {
				g.drawLine(x1, y1, x2, y2);
				//将坐标发送给客户端,这个sendLine方法在下一个类中
				mc.sendLine(x1, y1, x2, y2);
				//发送一个1代表画的是直线
				mc.dos.writeByte(1);
			}
			if ("矩形".equals(name)) {
				g.drawRect(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
				//将坐标发送给客户端
				mc.sendLine(x1, y1, x2, y2);
				//发送一个2代表画的是矩形
				mc.dos.writeByte(2);
			}
		} catch (Exception ef) {
			ef.printStackTrace();
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}
}
  • 建立客户端
public class MyClient {
	
	OutputStream ops;
	DataOutputStream dos;
	
	public void sendLine(int x1,int y1,int x2,int y2) {
		try{	
			dos.writeInt(x1);			
			System.out.println("写入第1个数据 "+x1);
			dos.writeInt(y1);
			System.out.println("写入第2个数据 "+y1);
			dos.writeInt(x2);
			System.out.println("写入第3个数据 "+x2);
			dos.writeInt(y2);
			System.out.println("写入第4个数据 "+y2);
			}catch(Exception ef){
				ef.printStackTrace();
			}
	}
	
	public boolean conn2Server() {
		try {
			//建立客户端
			Socket st = new Socket("127.0.0.1",4455);
			System.out.println("连接成功");
			//建立输出
			ops = st.getOutputStream();
			dos= new DataOutputStream(ops);
			
			return true;
		}catch(Exception ef) {
			ef.printStackTrace();
		}
		return false;
	}
	
	public static void main(String[] args) {
		MyClient mc = new MyClient();
		mc.conn2Server();
	}
}
  • 服务器代码(注释)
public class DrawServer {
	public static void main(String[] args) {
		try {
			// 建立一个服务端
			ServerSocket sst = new ServerSocket(4455);

			// 等待接收
			Socket st = sst.accept();

			// 建立输入流
			InputStream ips = st.getInputStream();
			DataInputStream dis = new DataInputStream(ips);
			// 服务端界面
			JFrame jf = new JFrame();
			jf.setSize(600, 800);
			jf.setTitle("Ser  Meetting by gzh4869--0.1");
			jf.setLocationRelativeTo(null);
			jf.setDefaultCloseOperation(3);
			jf.setVisible(true);

			Graphics g = jf.getGraphics();
			//先读一个字节,看看有没有颜色传过来,如果有,就设置成蓝色,当然可以多种颜色,用不同的报文头表示就好
			byte v1 = dis.readByte();
			if(v1 == 4) {
				g.setColor(new Color(0,128,255));
			}
			
			//接收坐标(注意,顺序不能乱,要和客户端传的顺序一致)
			while (true) {
				// 从输入流上,读取4个数字,
				System.out.println("等待客户机发来的数据...");
				int x1 = dis.readInt(); // 阻塞
				System.out.println("*****服务器读第1个数据 " + x1);
				int y1 = dis.readInt();
				System.out.println("*****服务器读第2个数据 " + y1);
				int x2 = dis.readInt();
				System.out.println("*****服务器读第3个数据 " + x2);
				int y2 = dis.readInt();
				System.out.println("*****服务器读第4个数据 " + y2);
				
				//报文头		
				byte v = dis.readByte();
				//在客户端里1代表直线,2代表矩形
				if(v == 1) {
				//画直线
					g.drawLine(x1, y1, x2, y2);
				}else if(v == 2) {
				//画矩形
					g.drawRect(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
				}
							
				System.out.println("收到坐标数据 " + x1 + " y1: " + y1 + " x2 " + x2 + " y2 " + y2);
			}

		} catch (Exception ef) {
			ef.printStackTrace();
		}
	}
}

  • 通过以上代码,我们知道了,其实只要客户端和服务器端连上了,剩下的就是传数据的问题。
  • 接下来可以弄一个你画我猜的小游戏。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值