Java将服务器绘制的线条同步到客户端的代码
时间: 2024-03-24 15:37:30 浏览: 43
以下是一个简单的 Java 代码示例,演示了如何将服务器绘制的线条同步到客户端:
服务端代码:
```java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server {
private List<ClientHandler> clients = new ArrayList<>();
public void start(int port) throws IOException {
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server started on port " + port);
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("New client connected: " + clientSocket);
ClientHandler clientHandler = new ClientHandler(clientSocket, this);
clients.add(clientHandler);
clientHandler.start();
}
}
public void broadcast(String message) {
for (ClientHandler client : clients) {
client.sendMessage(message);
}
}
public static void main(String[] args) throws IOException {
Server server = new Server();
server.start(8080);
}
}
class ClientHandler extends Thread {
private Socket clientSocket;
private Server server;
private String name;
private PrintWriter out;
public ClientHandler(Socket clientSocket, Server server) {
this.clientSocket = clientSocket;
this.server = server;
}
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println("Enter your name:");
name = in.readLine();
String welcomeMessage = "Welcome, " + name + "!";
server.broadcast(welcomeMessage);
while (true) {
String input = in.readLine();
if (input == null) {
return;
}
server.broadcast(name + ": " + input);
}
} catch (IOException e) {
System.out.println("Error handling client: " + e);
} finally {
try {
clientSocket.close();
} catch (IOException e) {
}
}
}
public void sendMessage(String message) {
out.println(message);
}
}
```
客户端代码:
```java
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Client {
private String serverAddress;
private int serverPort;
private String name;
private PrintWriter out;
private JFrame frame;
private DrawPanel drawPanel;
private List<Point> points = new ArrayList<>();
public Client(String serverAddress, int serverPort) {
this.serverAddress = serverAddress;
this.serverPort = serverPort;
}
public void start() throws Exception {
Socket socket = new Socket(serverAddress, serverPort);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
while (true) {
String line = in.readLine();
if (line.startsWith("Enter your name")) {
out.println(name);
break;
}
}
frame = new JFrame("Drawing");
drawPanel = new DrawPanel();
frame.getContentPane().add(drawPanel);
frame.setSize(400, 400);
frame.setVisible(true);
frame.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
Point point = e.getPoint();
points.add(point);
drawPanel.repaint();
}
});
while (true) {
String line = in.readLine();
if (line == null) {
return;
}
drawPanel.addMessage(line);
}
}
public static void main(String[] args) throws Exception {
Client client = new Client("127.0.0.1", 8080);
client.name = JOptionPane.showInputDialog("Enter your name:");
client.start();
}
class DrawPanel extends JPanel {
private List<String> messages = new ArrayList<>();
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(2));
for (int i = 0; i < points.size() - 1; i++) {
Point p1 = points.get(i);
Point p2 = points.get(i + 1);
g2.drawLine(p1.x, p1.y, p2.x, p2.y);
}
}
public void addMessage(String message) {
messages.add(message);
repaint();
}
public void paintMessages(Graphics g) {
g.setColor(Color.BLUE);
int y = 15;
for (String message : messages) {
g.drawString(message, 5, y);
y += 15;
}
}
}
}
```
这个示例演示了一个简单的多人在线协同绘制程序。客户端可以绘制线条,并将其同步到服务器和其他客户端。服务器维护一个客户端列表,可以将收到的消息广播给所有连接的客户端。客户端使用 Java AWT 绘制线条和消息。
阅读全文