一、服务器端
package 白板画图;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.StringTokenizer;
@SuppressWarnings("serial")
public class Server extends JFrame {
class DrawPane extends JPanel {
int x1, y1;
public DrawPane() {
x1=y1=0;
}
}
DrawPane p;
JButton btn=new JButton("清除");
public Server() throws IOException {
p = new DrawPane();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p,"Center");
getContentPane().add(btn,"North");
setTitle("服务器画板");
setSize(600,400);
setVisible(true);
udpServer();
}
@SuppressWarnings("resource")
public void udpServer() throws IOException {
Graphics g = getGraphics();//定义画笔
int x = 0, y = 0;//初始化坐标
DatagramSocket sSocket = null;
DatagramPacket receivePacket = null;
int port = 8000; //端口号
byte[] receive = new byte[1000];//接收客户端消息
String cin;//接受客户端消息
sSocket = new DatagramSocket(port);
receivePacket = new DatagramPacket(receive,receive.length);
while(true){
sSocket.receive(receivePacket);
cin = new String(receivePacket.getData(),0,receivePacket.getLength());
StringTokenizer scin = new StringTokenizer(cin, " ");
String orderStr = scin.nextToken();//接受指令
String X = scin.nextToken();
x = Integer.valueOf(X);
String Y = scin.nextToken();
y = Integer.valueOf(Y);
int a = 3;
if(orderStr.equals("BUTTON1"))
a = 1;
switch (a){
case 1:
System.out.println("画圆"+"X= "+x+"Y= "+y);
g.drawRect(x+10,y+55,20,20);
break;
case 3:
System.out.println("方块"+"X= "+x+"Y= "+y);
g.drawOval(x+10,y+55,20,20);
}
}
}
public static void main(String[] args) throws IOException {
new Server();
}
}
二、客户端
package 白板画图;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
@SuppressWarnings("serial")
public class Client extends JFrame{
DrawPane p;
JButton btn=new JButton("清除");
public Client() {
p=new DrawPane();
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
p.repaint();
try {
p.udpClient("btn", 0, 0);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p,"Center");
getContentPane().add(btn,"North");
setTitle("客户端画板");
setSize(600,400);
setVisible(true);
}
public static void main(String[] args) throws IOException {
new Client();
}
}
@SuppressWarnings("serial")
class DrawPane extends JPanel {
int x1,y1,x2,y2;
public DrawPane() {
x1=x2=y1=y2=0;
MouseAdapter adapter=new MouseAdapter() {
public void mousePressed(MouseEvent e) {
String button;
Graphics g = getGraphics();
if(e.getButton()==MouseEvent.BUTTON1) {
x1=e.getX();
y1=e.getY();
button = "BUTTON1";
try {
udpClient(button, x1, y1);
} catch (IOException e1) {
e1.printStackTrace();
}
draw1(g);
}else if(e.getButton()==MouseEvent.BUTTON3){
x2=e.getX();
y2=e.getY();
button = "BUTTON3";
try {
udpClient(button, x2, y2);
} catch (IOException e1) {
e1.printStackTrace();
}
draw2(g);
}
}
};
addMouseListener(adapter);
addMouseMotionListener(adapter);
}
@SuppressWarnings({ "resource", "unused" })
public void udpClient(String button, int x, int y) throws IOException {
String host; //接受ip地址输入的字符串
int port = 8000;//服务器端口
byte[] data ;//发送数据缓冲区
DatagramPacket cPacket = null;
DatagramSocket cSocket = null;
InputStreamReader out = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(out);
host = "127.0.0.1"; //服务器ip
InetAddress IPAddress = InetAddress.getByName(host);//获取ip地址
cSocket = new DatagramSocket(); //创建DatagramSocket套接字
String s = (button + " " + String.valueOf(x) + " " + String.valueOf(y));
data = s.getBytes();
int length = data.length;
cPacket = new DatagramPacket(data, data.length, IPAddress, port);
cSocket.send(cPacket);
}
public void draw1(Graphics g) {
g.drawRect(x1,y1,20,20);
}
public void draw2(Graphics g) {
g.drawOval(x2, y2, 20, 20);
}
}