Java服务器的白板内容可以在客户端同步显示的代码
时间: 2024-03-23 08:40:31 浏览: 163
要实现Java服务器的白板内容可以在客户端同步显示,可以使用Java的网络编程和图形编程技术。以下是一个简单的代码示例:
服务器端代码:
```
import java.net.*;
import java.io.*;
import java.util.*;
public class Server {
private ArrayList<PrintWriter> clientOutputStreams;
public static void main(String[] args) {
new Server().go();
}
public void go() {
clientOutputStreams = new ArrayList<PrintWriter>();
try {
ServerSocket serverSock = new ServerSocket(5000);
while (true) {
Socket clientSocket = serverSock.accept();
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
clientOutputStreams.add(writer);
Thread t = new Thread(new ClientHandler(clientSocket));
t.start();
System.out.println("got a connection");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void tellEveryone(String message) {
Iterator<PrintWriter> it = clientOutputStreams.iterator();
while (it.hasNext()) {
try {
PrintWriter writer = (PrintWriter) it.next();
writer.println(message);
writer.flush();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public class ClientHandler implements Runnable {
BufferedReader reader;
Socket sock;
public ClientHandler(Socket clientSocket) {
try {
sock = clientSocket;
InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(isReader);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void run() {
String message;
try {
while ((message = reader.readLine()) != null) {
System.out.println("server read " + message);
tellEveryone(message);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
```
客户端代码:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class Client {
private BufferedReader reader;
private PrintWriter writer;
private Socket sock;
private Whiteboard whiteboard;
public static void main(String[] args) {
new Client().go();
}
public void go() {
whiteboard = new Whiteboard();
whiteboard.getCanvas().addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int startX = e.getX();
int startY = e.getY();
whiteboard.setStartX(startX);
whiteboard.setStartY(startY);
writer.println(startX + "," + startY);
writer.flush();
}
});
whiteboard.getCanvas().addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
int endX = e.getX();
int endY = e.getY();
Graphics2D g = (Graphics2D) whiteboard.getCanvas().getGraphics();
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(2));
g.drawLine(whiteboard.getStartX(), whiteboard.getStartY(), endX, endY);
whiteboard.setStartX(endX);
whiteboard.setStartY(endY);
writer.println(endX + "," + endY);
writer.flush();
}
});
setUpNetworking();
JFrame frame = new JFrame("Whiteboard Client");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(whiteboard, BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setVisible(true);
}
private void setUpNetworking() {
try {
sock = new Socket("127.0.0.1", 5000);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
System.out.println("networking established");
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public class IncomingReader implements Runnable {
public void run() {
String message;
try {
while ((message = reader.readLine()) != null) {
System.out.println("client read " + message);
String[] coords = message.split(",");
int endX = Integer.parseInt(coords[0]);
int endY = Integer.parseInt(coords[1]);
Graphics2D g = (Graphics2D) whiteboard.getCanvas().getGraphics();
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(2));
g.drawLine(whiteboard.getStartX(), whiteboard.getStartY(), endX, endY);
whiteboard.setStartX(endX);
whiteboard.setStartY(endY);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
```
这个代码示例是一个简单的Java白板程序,它使用了Java的Swing库来创建GUI组件和事件监听器,并使用了Java的网络编程技术和图形编程技术实现了服务器和客户端之间的同步显示。服务器端代码接受来自客户端的坐标信息,并将它们广播到所有客户端。客户端代码接收来自服务器的坐标信息,并在画布上绘制线条。你可以根据自己的需求对代码进行修改和扩展。
阅读全文