帮我用用JavaSocket编程实现白板程序
时间: 2024-03-24 19:38:09 浏览: 87
好的,以下是一个简单的JavaSocket白板程序的实现:
1. 服务器端的实现:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class WhiteboardServer extends JFrame {
private static final long serialVersionUID = -2440534260699815775L;
private final List<Point> points = new ArrayList<>();
private final JPanel panel;
private final ServerSocket serverSocket;
public WhiteboardServer() throws IOException {
super("Whiteboard Server");
panel = new JPanel() {
private static final long serialVersionUID = -116422336173639844L;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
for (int i = 0; i < points.size() - 1; i++) {
Point p1 = points.get(i);
Point p2 = points.get(i + 1);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
}
}
};
panel.setBackground(Color.WHITE);
add(panel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(800, 600);
setVisible(true);
serverSocket = new ServerSocket(8000);
while (true) {
System.out.println("Waiting for a client to connect...");
Socket socket = serverSocket.accept();
System.out.println("Client connected from " + socket.getInetAddress().getHostAddress());
new WhiteboardServerThread(socket).start();
}
}
private class WhiteboardServerThread extends Thread {
private final Socket socket;
public WhiteboardServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
while (true) {
int x = socket.getInputStream().read();
int y = socket.getInputStream().read();
System.out.println("Received point: " + x + ", " + y);
points.add(new Point(x, y));
panel.repaint();
}
} catch (IOException e) {
System.err.println("Error reading from client: " + e.getMessage());
}
}
}
public static void main(String[] args) throws IOException {
new WhiteboardServer();
}
}
```
2. 客户端的实现:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class WhiteboardClient extends JFrame {
private static final long serialVersionUID = -2440534260699815775L;
private final JPanel panel;
private final Socket socket;
public WhiteboardClient() throws IOException {
super("Whiteboard Client");
panel = new JPanel() {
private static final long serialVersionUID = -116422336173639844L;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawLine(0, 0, getWidth(), getHeight());
g.drawLine(0, getHeight(), getWidth(), 0);
}
};
panel.setBackground(Color.WHITE);
panel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
try {
socket.getOutputStream().write(e.getX());
socket.getOutputStream().write(e.getY());
System.out.println("Sent point: " + e.getX() + ", " + e.getY());
} catch (IOException ex) {
System.err.println("Error sending point: " + ex.getMessage());
}
}
});
add(panel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(800, 600);
setVisible(true);
socket = new Socket("localhost", 8000);
}
public static void main(String[] args) throws IOException {
new WhiteboardClient();
}
}
```
这个白板程序的实现比较简单,服务器端和客户端通过Socket进行通信,当鼠标在客户端的面板上按下时,客户端会发送该点的坐标信息给服务器端,服务器端接收到该信息后,将该点的坐标加入到点列表中,并更新服务器端面板的绘制。
阅读全文