用JavaSocket编程实现白板程序
时间: 2024-03-21 07:38:29 浏览: 172
实现白板程序需要使用Java Socket编程和图形用户界面(GUI)开发。下面是一个简单的示例:
1. 创建服务器端
```java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class WhiteboardServer {
private ServerSocket serverSocket;
private Socket clientSocket;
public void start(int port) {
try {
serverSocket = new ServerSocket(port);
System.out.println("Server started on port " + port);
while (true) {
clientSocket = serverSocket.accept();
System.out.println("New client connected: " + clientSocket.getInetAddress().getHostName());
// 处理客户端连接
Thread clientThread = new Thread(new WhiteboardClientHandler(clientSocket));
clientThread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
WhiteboardServer server = new WhiteboardServer();
server.start(8888);
}
}
```
2. 创建客户端处理程序
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class WhiteboardClientHandler implements Runnable {
private Socket clientSocket;
private ObjectInputStream inputStream;
private ObjectOutputStream outputStream;
private List<Point> points = new ArrayList<>();
public WhiteboardClientHandler(Socket clientSocket) {
this.clientSocket = clientSocket;
}
@Override
public void run() {
try {
// 获取输入和输出流
outputStream = new ObjectOutputStream(clientSocket.getOutputStream());
inputStream = new ObjectInputStream(clientSocket.getInputStream());
// 处理客户端请求
while (true) {
Object inputObject = inputStream.readObject();
if (inputObject instanceof String) {
String command = (String) inputObject;
// 清空画板
if (command.equals("clear")) {
points.clear();
}
} else if (inputObject instanceof Point) {
Point point = (Point) inputObject;
// 绘制线条
if (!points.isEmpty()) {
Point lastPoint = points.get(points.size() - 1);
Graphics graphics = WhiteboardApp.getWhiteboardPanel().getGraphics();
graphics.setColor(Color.BLACK);
graphics.drawLine(lastPoint.x, lastPoint.y, point.x, point.y);
}
points.add(point);
}
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
```
3. 创建客户端
```java
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
public class WhiteboardApp {
private static JPanel whiteboardPanel;
private static ObjectOutputStream outputStream;
private static ObjectInputStream inputStream;
public static void main(String[] args) {
JFrame frame = new JFrame("Whiteboard");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建画板面板
whiteboardPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
}
};
whiteboardPanel.setPreferredSize(new Dimension(800, 600));
whiteboardPanel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
try {
outputStream.writeObject(e.getPoint());
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
whiteboardPanel.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
try {
outputStream.writeObject(e.getPoint());
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
// 创建清空按钮
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
outputStream.writeObject("clear");
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
// 将画板面板和清空按钮添加到面板中
JPanel contentPanel = new JPanel(new BorderLayout());
contentPanel.add(whiteboardPanel, BorderLayout.CENTER);
contentPanel.add(clearButton, BorderLayout.SOUTH);
// 创建客户端连接
try {
Socket clientSocket = new Socket("localhost", 8888);
outputStream = new ObjectOutputStream(clientSocket.getOutputStream());
inputStream = new ObjectInputStream(clientSocket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
// 启动线程处理服务器响应
new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
Object inputObject = inputStream.readObject();
if (inputObject instanceof Point) {
Point point = (Point) inputObject;
// 绘制线条
Graphics graphics = whiteboardPanel.getGraphics();
graphics.setColor(Color.BLACK);
graphics.drawLine(point.x, point.y, point.x + 1, point.y + 1);
} else if (inputObject instanceof String) {
String command = (String) inputObject;
// 清空画板
if (command.equals("clear")) {
whiteboardPanel.repaint();
}
}
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}).start();
// 设置面板内容和显示窗口
frame.getContentPane().add(contentPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static JPanel getWhiteboardPanel() {
return whiteboardPanel;
}
}
```
这个示例程序中,服务器端监听客户端连接,并启动一个新线程处理每个客户端的请求。客户端与服务器端通过套接字连接,并使用对象输入输出流来传输绘图命令和数据。客户端使用鼠标事件来捕获用户的绘图动作,并将数据发送到服务器端。服务器端接收到数据后,将绘图命令和数据转发给所有客户端。客户端接收到服务器响应后,将绘图结果显示在画板上。
阅读全文