无聊做了一个TCP的我画你猜

先说实现。

两个文件,一个draw,一个guess

draw作为服务器,guess作为客户端

用MouseMouseMotionListener 鼠标拖拽事件绘图

绘制之后发送。

图片的发送:

DataOutputStream dos = new DataOutputStream(s.getOutputStream())

;ByteArrayOutputStream out = new ByteArrayOutputStream();
boolean flag = ImageIO.write(bi, "PNG", out);
System.out.println(flag);
byte[] b = out.toByteArray();
dos.write(b);

图片接收:

dis = new DataInputStream(s.getInputStream());
dis.read(b);
ByteArrayInputStream in = new ByteArrayInputStream(b);
Bufferedimage image = ImageIO.read(in);

 

代码:draw.java

package drawguess;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

 

 

public class Draw extends JFrame implements MouseListener,MouseMotionListener{
 Point p = null;
 static BufferedImage bi = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
 
 TextField tfTxt = new TextField();
 Button sendButton = new Button("send");
 Panel panel = new Panel();
 String rcvString = null;
 List<Client> clients = new ArrayList<Client>();
 ServerSocket ss = null;
 Socket s = null;
 DataInputStream dis;
 DataOutputStream dos;

 public Draw(){
  this.setTitle("draw");
  this.setBounds(300, 200, 500, 500);
  this.add(tfTxt, BorderLayout.SOUTH);
  this.add(sendButton, BorderLayout.EAST);
  sendButton.addActionListener(new SendButtonListener());
  this.setVisible(true);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.addMouseListener(this);
  this.addMouseMotionListener(this);
  this.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent we){
    save();
    System.exit(0);
   }
  });
 }
 public void start(){
  ServerSocket ss = null;
  boolean started = false;
  
  try {
   ss = new ServerSocket(8888);
  } catch (BindException e){
   System.out.println("端口使用中...");
   System.exit(0);
  } catch (IOException e1) {
   e1.printStackTrace();
  }
  try {
   started = true;
   while(started){
    s = ss.accept();
    Client c = new Client(s);
    clients.add(c);
    System.out.println("A client connect");
    new Thread(c).start();
   }
  } catch (IOException e) { 
   e.printStackTrace();
  } finally{
   try {
    ss.close();
   } catch (IOException e1) {
    e1.printStackTrace();
   } 
  }
 }
 public static void main(String[] args){
  Draw drawFrame = new Draw();
  drawFrame.start();
  
 }
 void save(){
   try {
    ImageIO.write(bi, "png", new File("e:\\yourImageName.png"));
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
 }
 public void paint(Graphics g){
  if(p!=null){
   g.fillOval(p.x, p.y, 10, 10);
  if(rcvString!=null)
   g.drawString(rcvString, 30, 30);
  Graphics2D g2 = bi.createGraphics();
  g2.setBackground(Color.WHITE);
  g2.setColor(Color.BLACK);
     g2.fillOval(p.x, p.y, 10, 10);
  }
 }
 public void mouseClicked(MouseEvent e) {
  // TODO Auto-generated method stub
  p=e.getPoint();
  repaint();
 }
 @Override
 public void mousePressed(MouseEvent e) {//按下
  // TODO Auto-generated method stub
  p=e.getPoint();
  repaint();
 }
 @Override
 public void mouseReleased(MouseEvent e) {//释放
  // TODO Auto-generated method stub
  p=e.getPoint();
  repaint();
 }
 @Override
 public void mouseEntered(MouseEvent e) {
  // TODO Auto-generated method stub
 }
 @Override
 public void mouseExited(MouseEvent e) {
  // TODO Auto-generated method stub
 }
 
 @Override
 public void mouseDragged(MouseEvent e) {//拖拽
  // TODO Auto-generated method stub
  p=e.getPoint();
  //System.out.println("Mousedrgged");
  repaint();
 }
 @Override
 public void mouseMoved(MouseEvent e) {//移动
  // TODO Auto-generated method stub

 }
 class SendButtonListener implements ActionListener{
  public void actionPerformed(ActionEvent event) {
   try {
   dos = new DataOutputStream(s.getOutputStream());
   //BufferedImage image = ImageIO.read(new File("1.gif"));//bi
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   boolean flag = ImageIO.write(bi, "PNG", out);
   System.out.println(flag);
   byte[] b = out.toByteArray();
   
   dos.write(b);
   
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  System.out.println("button");
  }
  
 }
 class Client implements Runnable{
  private Socket s;
  private DataInputStream dis;
  private DataOutputStream dos;
  
  public Client(Socket s){
   this.s = s;
   try {
    dis = new DataInputStream(s.getInputStream());
    dos = new DataOutputStream(s.getOutputStream());
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  
  public void send(String string){
   try {
    dos.writeUTF(string);
   } catch (IOException e) {
    clients.remove(this);
    //e.printStackTrace();
   }
  }

  public void run() {
   Client c = null;
   try {
    while(true){  
     rcvString = dis.readUTF();
     System.out.println(rcvString);
     tfTxt.setText(rcvString);
//     for(int i=0;i<clients.size();i++){
//      c = clients.get(i);
//      c.send(string);
//     }
    }
   } catch (SocketException e){
    System.out.println("对方退出");
   } catch (EOFException e) { 
    e.printStackTrace();
   } catch (IOException e) { 
    e.printStackTrace();
   } finally{
    try {
     if(dis!=null)
      dis.close();
     if(dos!=null)
      dos.close();
     if(s!=null)
      s.close();

    } catch (IOException e) {
     e.printStackTrace();
    } 
   }
  }
 }
}

 

Guess.java

package drawguess;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Guess extends JFrame{
 BufferedImage image = null;
 
 Socket s;
 DataInputStream dis;
 DataOutputStream dos;
 
 TextField tfTxt = new TextField();
 
 boolean connected = false;
 Thread tRcv = new Thread(new RcvThread());
 public Guess(){
  this.setTitle("Guess");
  this.setBounds(300, 200, 500, 500);
  
  this.add(tfTxt, BorderLayout.SOUTH);
  tfTxt.addActionListener(new TFListener());
  this.setVisible(true);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  this.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent we){
    System.exit(0);
   }
  });
 }
 public void connect(){
  try {
   s = new Socket("127.0.0.1",8888);
   dos = new DataOutputStream(s.getOutputStream());
   dis = new DataInputStream(s.getInputStream());
   connected = true;
   //System.out.println("connected");
  } catch (UnknownHostException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  tRcv.start();
 }
 void accept(){
  try {                   
   Socket s = new Socket("localhost",8888);
   PrintWriter out = new PrintWriter(s.getOutputStream());
   out.print("a");
   DataInputStream in = new DataInputStream(s.getInputStream());
   byte[]b = new byte[1000000];
   in.read(b);
   ByteArrayInputStream bin = new ByteArrayInputStream(b);
   BufferedImage image = ImageIO.read(bin);
   ImageIO.write(image, "gif", new File("2.gif"));
   s.close();
   } catch (Exception e) {
    
   }
 }
 public static void main(String[] args) throws Exception{
  Guess GuessFrame = new Guess();
  GuessFrame.connect();
  
 }
 public void paint(Graphics g){
  if(image!=null)
   g.drawImage(image, 0, 0, 500, 500, this);
 }
 private class TFListener implements ActionListener{
  public void actionPerformed(ActionEvent event) {
   try {
   dos.writeUTF(tfTxt.getText());
   dos.flush();
   } catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   tfTxt.setText("");
  }
  
 }
 class RcvThread implements Runnable{
  byte[] b = new byte[1000000];
  public void run() {
   try {
    while(connected){
     //String string = dis.readUTF();
     dis = new DataInputStream(s.getInputStream());
     dis.read(b);
     ByteArrayInputStream in = new ByteArrayInputStream(b);
     image = ImageIO.read(in);
     ImageIO.write(image, "png", new File("F:\\image.png"));
     repaint();
    }
   } catch (SocketException e){
    System.out.println("退出,bye");
   } catch (EOFException e){
    System.out.println("退出,bye");
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
}

 





 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值