用Java 写一个基于 Socket 通信,使用四个窗口拼接绘制四叶玫瑰线,每个窗口绘制一瓣图形
时间: 2023-07-15 14:13:11 浏览: 387
以下是一个基于Socket通信的Java程序,用于在四个窗口中绘制四叶玫瑰线:
Server.java:
```java
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
Socket[] sockets = new Socket[4];
PrintWriter[] writers = new PrintWriter[4];
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(1);
}
System.out.println("Waiting for clients to connect...");
for (int i = 0; i < 4; i++) {
sockets[i] = serverSocket.accept();
writers[i] = new PrintWriter(sockets[i].getOutputStream(), true);
writers[i].println(i + 1);
}
System.out.println("All clients connected.");
for (int i = 0; i < 4; i++) {
writers[i].println("start");
writers[i].flush();
}
serverSocket.close();
}
}
```
Client.java:
```java
import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Client extends JFrame {
private static final long serialVersionUID = 1L;
private int clientNumber;
private PrintWriter out;
private JTextArea textArea;
public Client(int clientNumber) {
this.clientNumber = clientNumber;
textArea = new JTextArea(20, 40);
add(new JScrollPane(textArea), BorderLayout.CENTER);
setTitle("Client " + clientNumber);
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void connectToServer() throws IOException {
Socket socket = new Socket("localhost", 4444);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
String serverResponse = in.readLine();
System.out.println("Connected to server as client " + serverResponse);
while (true) {
String input = in.readLine();
if (input == null) {
break;
}
if (input.equals("start")) {
drawRose();
}
}
socket.close();
}
public void drawRose() {
JFrame frame = new JFrame("Rose");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel panel = new JPanel() {
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int r = 150;
double k = 4.0 / 3.0;
double a = 0.0;
double d = 0.05;
double x0 = getWidth() / 2.0;
double y0 = getHeight() / 2.0;
g2d.setColor(Color.RED);
for (int i = 0; i < 180; i++) {
double t = Math.toRadians(i);
double r1 = r * Math.sin(k * t + a);
double x1 = x0 + r1 * Math.cos(t);
double y1 = y0 - r1 * Math.sin(t);
g2d.drawLine((int) x0, (int) y0, (int) x1, (int) y1);
}
a += d;
}
};
frame.add(panel);
frame.setLocation((clientNumber - 1) % 2 * 400, (clientNumber - 1) / 2 * 400);
}
public static void main(String[] args) throws IOException {
Client client = new Client(Integer.parseInt(args[0]));
client.connectToServer();
}
}
```
该程序中,Server.java用于监听客户端的连接,并向客户端发送该客户端的编号。Client.java用于连接服务器,接收自己的编号并绘制四叶玫瑰线。在绘制图形前,客户端向服务器发送一个start消息,告诉服务器准备开始绘制。服务器接收到四个start消息后,向四个客户端发送绘制命令,客户端收到命令后开始绘制。
程序运行时,需要启动四个客户端,每个客户端传入一个从1到4的数字作为参数,表示该客户端的编号。例如:
```bash
java Client 1
java Client 2
java Client 3
java Client 4
```
客户端启动后,会连接到服务器并接收自己的编号。当所有客户端都连接成功后,服务器会向所有客户端发送start消息,客户端接收到消息后开始绘制四叶玫瑰线。最终,四个窗口中的图形会拼接成一个完整的四叶玫瑰线。
阅读全文