1. 编写图形用户界面,教师为服务器端,学生为客户端,一个服务端可以接受多个客户端连接。 2. 要求完成教师白板的建立,实现教师在白板上画图,学生端能够实时刷新。 3. 教师可以传输文件给学生。 4. 学生可以向教师提问,问答内容所有学生都能看到。实现以上要求
时间: 2024-03-13 18:44:08 浏览: 53
这是一个比较复杂的项目,需要涉及到网络编程、GUI编程、多线程编程等方面的知识。下面是一个简单的实现方案供参考:
1. 编写服务器端代码:
```java
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
public class WhiteboardServer {
private List<ClientHandler> clients = new ArrayList<>();
private Whiteboard whiteboard = new Whiteboard();
public static void main(String[] args) throws IOException {
new WhiteboardServer().start();
}
public void start() throws IOException {
ServerSocket serverSocket = new ServerSocket(4444);
System.out.println("Server started.");
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket);
ClientHandler clientHandler = new ClientHandler(clientSocket);
clients.add(clientHandler);
clientHandler.start();
}
}
public synchronized void broadcastMessage(String message) {
for (ClientHandler client : clients) {
client.sendMessage(message);
}
}
public synchronized void broadcastImage(Image image) {
for (ClientHandler client : clients) {
client.sendImage(image);
}
}
public synchronized void broadcastFile(File file) throws IOException {
for (ClientHandler client : clients) {
client.sendFile(file);
}
}
public synchronized void removeClient(ClientHandler client) {
clients.remove(client);
}
private class ClientHandler extends Thread {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public ClientHandler(Socket socket) throws IOException {
clientSocket = socket;
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}
public void run() {
try {
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("Received message from client: " + inputLine);
if (inputLine.startsWith("DRAW")) {
whiteboard.draw(inputLine.substring(5));
broadcastMessage(inputLine);
} else if (inputLine.startsWith("FILE")) {
File file = new File(inputLine.substring(5));
if (file.exists()) {
broadcastFile(file);
}
} else if (inputLine.equals("QUESTION")) {
String question = in.readLine();
System.out.println("Received question from client: " + question);
broadcastMessage("ANSWER " + question);
}
}
} catch (IOException e) {
System.err.println("Error handling client: " + e);
} finally {
out.close();
try {
in.close();
clientSocket.close();
} catch (IOException e) {
System.err.println("Error closing client socket: " + e);
}
removeClient(this);
}
}
public void sendMessage(String message) {
out.println(message);
}
public void sendImage(Image image) {
// TODO: implement this method
}
public void sendFile(File file) throws IOException {
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
clientSocket.getOutputStream().write(buffer, 0, bytesRead);
}
fis.close();
}
}
}
```
2. 编写客户端代码:
```java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class WhiteboardClient {
private Socket socket;
private PrintWriter out;
private BufferedReader in;
private WhiteboardCanvas canvas;
public static void main(String[] args) throws IOException {
new WhiteboardClient().start();
}
public void start() throws IOException {
String serverAddress = JOptionPane.showInputDialog("Enter server IP address:");
socket = new Socket(serverAddress, 4444);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
canvas = new WhiteboardCanvas();
JFrame frame = new JFrame("Whiteboard");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(canvas, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
new Thread(new ServerListener()).start();
canvas.addMouseListener(new DrawingListener());
canvas.addMouseMotionListener(new DrawingListener());
canvas.requestFocus();
}
private void draw(String message) {
String[] parts = message.split(" ");
int x1 = Integer.parseInt(parts[0]);
int y1 = Integer.parseInt(parts[1]);
int x2 = Integer.parseInt(parts[2]);
int y2 = Integer.parseInt(parts[3]);
canvas.drawLine(x1, y1, x2, y2);
}
private void receiveImage() {
// TODO: implement this method
}
private void receiveFile() throws IOException {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showSaveDialog(canvas);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = socket.getInputStream().read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
fos.close();
}
}
private void askQuestion() throws IOException {
String question = JOptionPane.showInputDialog("Enter your question:");
out.println("QUESTION");
out.println(question);
}
private class ServerListener implements Runnable {
public void run() {
try {
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("Received message from server: " + inputLine);
if (inputLine.startsWith("DRAW")) {
draw(inputLine.substring(5));
} else if (inputLine.startsWith("IMAGE")) {
receiveImage();
} else if (inputLine.startsWith("FILE")) {
receiveFile();
} else if (inputLine.startsWith("ANSWER")) {
String question = inputLine.substring(7);
JOptionPane.showMessageDialog(canvas, "The answer is: " + question);
}
}
} catch (IOException e) {
System.err.println("Error receiving message from server: " + e);
} finally {
try {
in.close();
out.close();
socket.close();
} catch (IOException e) {
System.err.println("Error closing client socket: " + e);
}
}
}
}
private class DrawingListener extends MouseAdapter {
private int x, y;
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
public void mouseDragged(MouseEvent e) {
int x2 = e.getX();
int y2 = e.getY();
out.println("DRAW " + x + " " + y + " " + x2 + " " + y2);
canvas.drawLine(x, y, x2, y2);
x = x2;
y = y2;
}
}
}
```
3. 编写画板代码:
```java
import java.awt.*;
import javax.swing.*;
public class WhiteboardCanvas extends JPanel {
private Image image;
private Graphics2D g2;
public WhiteboardCanvas() {
setPreferredSize(new Dimension(800, 600));
setBackground(Color.WHITE);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (image == null) {
image = createImage(getWidth(), getHeight());
g2 = (Graphics2D) image.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
clear();
}
g.drawImage(image, 0, 0, null);
}
public void clear() {
g2.setPaint(Color.WHITE);
g2.fillRect(0, 0, getWidth(), getHeight());
repaint();
}
public void drawLine(int x1, int y1, int x2, int y2) {
g2.drawLine(x1, y1, x2, y2);
repaint();
}
}
```
在实现以上代码之后,就可以在教师端启动服务器,学生端启动客户端,然后进行画图、发送文件、提问等操作了。需要注意的是,在实现传输图片的功能时,可以使用Java的ImageIO类来将图像转换成字节数组,然后在网络上传输,接收方可以将字节数组转换成图像。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)