java绘制C/S架构的网络绘图版
1. 服务端程序
- Server.java
import java.net.ServerSocket;
import java.net.Socket;
public class Server extends Thread {
public void run(){
createServer();
}
public void createServer(){
try {
ServerSocket serverSocket = new ServerSocket(7070);
System.out.println("Server created successfully!");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("client connection !");
IoThread ioThread = new IoThread(socket);
SeverThreadList.SeverList.add(ioThread);
ioThread.start();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Server creation failed!");
}
}
public static void main(String [] args){
Server server = new Server();
server.run();
}
}
- IoThread.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class IoThread extends Thread{
private Socket socket ;
private InputStream ins;
public OutputStream out;
private DataInputStream dins;
public DataOutputStream dout;
public IoThread(Socket socket) {
this.socket =socket;
}
public void run(){
process();
}
public void process(){
try {
ins = this.socket.getInputStream();
out = this.socket.getOutputStream();
dins = new DataInputStream(ins);
dout = new DataOutputStream(out);
while (true) {
int x1 = dins.readInt();
int y1 = dins.readInt();
int x2= dins.readInt();
int y2 = dins.readInt();
int color = dins.readInt();
transDate(x1, y1, x2, y2,color);
}
}catch (IOException e) {
System.out.println("Client connect failed!");
}
}
public void transDate(int x1,int y1,int x2,int y2, int color){
int t = SeverThreadList.SeverList.size();
for(int i=0;i<t;i++){
try {
SeverThreadList.SeverList.get(i).dout.writeInt(x1);
SeverThreadList.SeverList.get(i).dout.writeInt(y1);
SeverThreadList.SeverList.get(i).dout.writeInt(x2);
SeverThreadList.SeverList.get(i).dout.writeInt(y2);
SeverThreadList.SeverList.get(i).dout.writeInt(color);
} catch (IOException e) {
//e.printStackTrace();
System.out.println("Client connect failed!");
}
}
}
}
- SeverThreadList.java
import java.util.ArrayList;
public class SeverThreadList {
public static ArrayList<IoThread> SeverList = new ArrayList<IoThread>();
}
客户端程序
- Client.java
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Client extends JFrame{
private Graphics g;
public void DrawingBoard(){
this.setTitle("Client interface");
this.setSize(700,600);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
JPanel centralPanel = new JPanel();
centralPanel.setPreferredSize(new Dimension(500,0));
this.add(centralPanel,BorderLayout.CENTER);
this.setVisible(true);
g = centralPanel.getGraphics();
ClientSocket clientSocket = new ClientSocket(g);
clientSocket.start();
ClientMouseListener clientMouseListener = new ClientMouseListener(g,clientSocket);
centralPanel.addMouseListener(clientMouseListener);
centralPanel.addMouseMotionListener(clientMouseListener);
}
public static void main(String [] args){
Client client = new Client();
client.DrawingBoard();
}
}
- ClientMouseListener.java
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.awt.event.MouseAdapter;
public class ClientMouseListener extends MouseAdapter {
private int x1;
private int y1;
private int x2;
private int y2;
private int color=0;
private Graphics g;
private ClientSocket clientSocket;
public ClientMouseListener(Graphics g, ClientSocket clientSocket) {
this.g = g;
this.clientSocket = clientSocket;
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
color+=1;
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
int x2=e.getX();
int y2=e.getY();
try {
this.clientSocket.dout.writeInt(x1);
this.clientSocket.dout.writeInt(y1);
this.clientSocket.dout.writeInt(x2);
this.clientSocket.dout.writeInt(y2);
this.clientSocket.dout.writeInt(color);
} catch (IOException e1) {
e1.printStackTrace();
}
g.drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;
}
}
- ClientSocket.java
import java.awt.Graphics;
import java.awt.Color;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class ClientSocket extends Thread{
private Socket socket;
private InputStream ins;
public OutputStream out;
private DataInputStream dins;
public DataOutputStream dout;
private Graphics g;
public ClientSocket(Graphics g) {
this.g = g;
}
public void run(){
process();
}
public void process(){
try {
socket = new Socket("127.0.0.1",7070);
ins = socket.getInputStream();
out = socket.getOutputStream();
dins = new DataInputStream(ins);
dout = new DataOutputStream(out);
while (true) {
readDate();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void readDate(){
try {
int x1 = dins.readInt();
int y1 = dins.readInt();
int x2 = dins.readInt();
int y2 = dins.readInt();
int color=dins.readInt();
if(color>6) color%=6;
if(color==1) g.setColor(Color.BLUE);
else if(color==2) g.setColor(Color.CYAN);
else if(color==3) g.setColor(Color.GREEN);
else if(color==4) g.setColor(Color.PINK);
else if(color==5) g.setColor(Color.MAGENTA);
else if(color==6) g.setColor(Color.RED);
g.drawLine(x1, y1, x2, y2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果