用JavaSocket编程实现带图形界面的白板程序的代码
时间: 2024-03-25 20:35:42 浏览: 83
基于Java Socket 通信实现的(图形界面)聊天室【100012620】
5星 · 资源好评率100%
以下是一个简单的基于Java Socket编程的带图形界面的白板程序的代码:
```java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Whiteboard extends JFrame implements ActionListener {
private JButton connectButton, clearButton, colorButton;
private JLabel statusLabel;
private JPanel canvasPanel;
private JTextField hostField, portField;
private String host;
private int port;
private Graphics2D g2d;
private Color color = Color.BLACK;
private Socket socket;
private ObjectOutputStream out;
private ObjectInputStream in;
private boolean connected = false;
public Whiteboard() {
super("Whiteboard");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
connectButton = new JButton("Connect");
connectButton.addActionListener(this);
clearButton = new JButton("Clear");
clearButton.addActionListener(this);
colorButton = new JButton("Color");
colorButton.addActionListener(this);
statusLabel = new JLabel("Not connected");
hostField = new JTextField("localhost", 10);
portField = new JTextField("8000", 4);
JPanel controlPanel = new JPanel();
controlPanel.add(connectButton);
controlPanel.add(clearButton);
controlPanel.add(colorButton);
controlPanel.add(new JLabel("Host:"));
controlPanel.add(hostField);
controlPanel.add(new JLabel("Port:"));
controlPanel.add(portField);
controlPanel.add(statusLabel);
canvasPanel = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g2d = (Graphics2D) g;
}
};
canvasPanel.setPreferredSize(new Dimension(600, 400));
canvasPanel.setBackground(Color.WHITE);
canvasPanel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (connected) {
try {
out.writeObject(new Point(e.getX(), e.getY()));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});
canvasPanel.addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
if (connected) {
try {
out.writeObject(new Point(e.getX(), e.getY()));
} catch (IOException ex) {
ex.printStackTrace();
}
}
g2d.setColor(color);
g2d.drawLine(e.getX(), e.getY(), e.getX(), e.getY());
canvasPanel.repaint();
}
});
getContentPane().add(canvasPanel, BorderLayout.CENTER);
getContentPane().add(controlPanel, BorderLayout.SOUTH);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == connectButton) {
host = hostField.getText();
port = Integer.parseInt(portField.getText());
connect();
} else if (e.getSource() == clearButton) {
clear();
} else if (e.getSource() == colorButton) {
color = JColorChooser.showDialog(this, "Choose a color", color);
}
}
public void connect() {
try {
socket = new Socket(host, port);
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
connected = true;
statusLabel.setText("Connected");
new Thread(new ReceiveThread()).start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void clear() {
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, canvasPanel.getWidth(), canvasPanel.getHeight());
canvasPanel.repaint();
if (connected) {
try {
out.writeObject("clear");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void receive(Point p) {
g2d.setColor(color);
g2d.drawLine(p.x, p.y, p.x, p.y);
canvasPanel.repaint();
}
class ReceiveThread implements Runnable {
public void run() {
try {
while (true) {
Object obj = in.readObject();
if (obj instanceof Point) {
receive((Point) obj);
} else if (obj instanceof String) {
if (obj.equals("clear")) {
clear();
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Whiteboard();
}
}
```
这个程序创建了一个带有连接、清除和颜色选择按钮和一个绘图区域的窗口,可以通过Socket连接到另一个Whiteboard程序,然后在两个程序之间共享绘图。连接成功后,用户可以在绘图区域上绘制,这些绘图会被发送到另一个Whiteboard程序,并在其绘图区域上显示。用户可以使用清除按钮删除所有绘图,可以使用颜色选择器选择不同的颜色来绘制。
阅读全文